Skip to content

Commit 36ef395

Browse files
authored
Detect infinite loop in async fn not returning ! (#15545)
This fixes an overzealous change made to avoid signaling infinite loops in anonymous blocks that may never be used. Fixes rust-lang/rust-clippy#15541 r? @dswij changelog: none
2 parents 7d43319 + 8a98865 commit 36ef395

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

clippy_lints/src/loops/infinite_loop.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use hir::intravisit::{Visitor, walk_expr};
44
use rustc_ast::Label;
55
use rustc_errors::Applicability;
66
use rustc_hir::{
7-
self as hir, Closure, ClosureKind, CoroutineDesugaring, CoroutineKind, Expr, ExprKind, FnRetTy, FnSig, Node, TyKind,
7+
self as hir, Closure, ClosureKind, CoroutineDesugaring, CoroutineKind, CoroutineSource, Expr, ExprKind, FnRetTy,
8+
FnSig, Node, TyKind,
89
};
910
use rustc_lint::{LateContext, LintContext};
1011
use rustc_span::sym;
@@ -73,7 +74,11 @@ fn is_inside_unawaited_async_block(cx: &LateContext<'_>, expr: &Expr<'_>) -> boo
7374
if let Node::Expr(Expr {
7475
kind:
7576
ExprKind::Closure(Closure {
76-
kind: ClosureKind::Coroutine(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)),
77+
kind:
78+
ClosureKind::Coroutine(CoroutineKind::Desugared(
79+
CoroutineDesugaring::Async,
80+
CoroutineSource::Block | CoroutineSource::Closure,
81+
)),
7782
..
7883
}),
7984
..

tests/ui/infinite_loops.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,4 +521,19 @@ mod tokio_spawn_test {
521521
}
522522
}
523523

524+
mod issue15541 {
525+
async fn good() -> ! {
526+
loop {
527+
std::future::pending().await
528+
}
529+
}
530+
531+
async fn bad() {
532+
//~v infinite_loop
533+
loop {
534+
std::future::pending().await
535+
}
536+
}
537+
}
538+
524539
fn main() {}

tests/ui/infinite_loops.stderr

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,5 +333,15 @@ LL | | }
333333
|
334334
= help: if this is not intended, try adding a `break` or `return` condition in the loop
335335

336-
error: aborting due to 23 previous errors
336+
error: infinite loop detected
337+
--> tests/ui/infinite_loops.rs:533:9
338+
|
339+
LL | / loop {
340+
LL | | std::future::pending().await
341+
LL | | }
342+
| |_________^
343+
|
344+
= help: if this is not intended, try adding a `break` or `return` condition in the loop
345+
346+
error: aborting due to 24 previous errors
337347

0 commit comments

Comments
 (0)