Skip to content

fix infinite loop #15157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions clippy_lints/src/loops/infinite_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use hir::intravisit::{Visitor, walk_expr};
use hir::{Expr, ExprKind, FnRetTy, FnSig, Node, TyKind};
use rustc_ast::Label;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::{self as hir, Closure, ClosureKind, CoroutineDesugaring, CoroutineKind};
use rustc_lint::{LateContext, LintContext};
use rustc_span::sym;

Expand All @@ -29,6 +29,10 @@ pub(super) fn check<'tcx>(
return;
}

if is_inside_unawaited_async_block(cx, expr) {
return;
}

if expr.span.in_external_macro(cx.sess().source_map()) || is_from_proc_macro(cx, expr) {
return;
}
Expand Down Expand Up @@ -60,15 +64,48 @@ pub(super) fn check<'tcx>(
}
}

/// Check if the given expression is inside an async block that is not being awaited.
/// This helps avoid false positives when async blocks are spawned or assigned to variables.
fn is_inside_unawaited_async_block(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
let current_hir_id = expr.hir_id;
for (_, parent_node) in cx.tcx.hir_parent_iter(current_hir_id) {
if let Node::Expr(Expr {
kind:
ExprKind::Closure(Closure {
kind: ClosureKind::Coroutine(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)),
..
}),
..
}) = parent_node
{
return !is_async_block_awaited(cx, expr);
}
}
false
}

fn is_async_block_awaited(cx: &LateContext<'_>, async_expr: &Expr<'_>) -> bool {
for (_, parent_node) in cx.tcx.hir_parent_iter(async_expr.hir_id) {
if let Node::Expr(Expr {
kind: ExprKind::Match(_, _, hir::MatchSource::AwaitDesugar),
..
}) = parent_node
{
return true;
}
}
false
}

fn get_parent_fn_ret_ty<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) -> Option<FnRetTy<'tcx>> {
for (_, parent_node) in cx.tcx.hir_parent_iter(expr.hir_id) {
match parent_node {
// Skip `Coroutine` closures, these are the body of `async fn`, not async closures.
// This is because we still need to backtrack one parent node to get the `OpaqueDef` ty.
Node::Expr(Expr {
kind:
ExprKind::Closure(hir::Closure {
kind: hir::ClosureKind::Coroutine(_),
ExprKind::Closure(Closure {
kind: ClosureKind::Coroutine(_),
..
}),
..
Expand All @@ -90,7 +127,7 @@ fn get_parent_fn_ret_ty<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) -> Option
..
})
| Node::Expr(Expr {
kind: ExprKind::Closure(hir::Closure { fn_decl: decl, .. }),
kind: ExprKind::Closure(Closure { fn_decl: decl, .. }),
..
}) => return Some(decl.output),
_ => (),
Expand Down
71 changes: 71 additions & 0 deletions tests/ui/infinite_loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,75 @@ mod issue_12338 {
}
}

#[allow(clippy::let_underscore_future, clippy::empty_loop)]
mod issue_14000 {
use super::do_something;

async fn foo() {
let _ = async move {
loop {
//~^ infinite_loop
do_something();
}
}
.await;
let _ = async move {
loop {
//~^ infinite_loop
continue;
}
}
.await;
}

fn bar() {
let _ = async move {
loop {
do_something();
}
};

let _ = async move {
loop {
continue;
}
};
}
}

#[allow(clippy::let_underscore_future)]
mod tokio_spawn_test {
use super::do_something;

fn install_ticker() {
// This should NOT trigger the lint because the async block is spawned, not awaited
std::thread::spawn(move || {
async move {
loop {
// This loop should not trigger infinite_loop lint
do_something();
}
}
});
}

fn spawn_async_block() {
// This should NOT trigger the lint because the async block is not awaited
let _handle = async move {
loop {
do_something();
}
};
}

fn await_async_block() {
// This SHOULD trigger the lint because the async block is awaited
let _ = async move {
loop {
do_something();
}
};
}
}

fn main() {}
24 changes: 23 additions & 1 deletion tests/ui/infinite_loops.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -311,5 +311,27 @@ help: if this is intentional, consider specifying `!` as function return
LL | fn continue_outer() -> ! {
| ++++

error: aborting due to 21 previous errors
error: infinite loop detected
--> tests/ui/infinite_loops.rs:459:13
|
LL | / loop {
LL | |
LL | | do_something();
LL | | }
| |_____________^
|
= help: if this is not intended, try adding a `break` or `return` condition in the loop

error: infinite loop detected
--> tests/ui/infinite_loops.rs:466:13
|
LL | / loop {
LL | |
LL | | continue;
LL | | }
| |_____________^
|
= help: if this is not intended, try adding a `break` or `return` condition in the loop

error: aborting due to 23 previous errors