Skip to content

Commit b3980d8

Browse files
committed
catch never loops through diverging functions
1 parent 39b316d commit b3980d8

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

clippy_lints/src/loops/never_loop.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ fn never_loop_expr<'tcx>(
148148
local_labels: &mut Vec<(HirId, bool)>,
149149
main_loop_id: HirId,
150150
) -> NeverLoopResult {
151-
match expr.kind {
151+
let result = match expr.kind {
152152
ExprKind::Unary(_, e)
153153
| ExprKind::Cast(e, _)
154154
| ExprKind::Type(e, _)
@@ -262,7 +262,14 @@ fn never_loop_expr<'tcx>(
262262
| ExprKind::ConstBlock(_)
263263
| ExprKind::Lit(_)
264264
| ExprKind::Err(_) => NeverLoopResult::Normal,
265-
}
265+
};
266+
combine_seq(result, || {
267+
if cx.typeck_results().expr_ty(expr).is_never() {
268+
NeverLoopResult::Diverging
269+
} else {
270+
NeverLoopResult::Normal
271+
}
272+
})
266273
}
267274

268275
fn never_loop_expr_all<'tcx, T: Iterator<Item = &'tcx Expr<'tcx>>>(

tests/ui/never_loop.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,13 @@ pub fn test31(b: bool) {
385385
}
386386
}
387387

388+
pub fn test32(b: bool) {
389+
loop {
390+
//~^ ERROR: this loop never actually loops
391+
panic!("oh no");
392+
}
393+
}
394+
388395
fn main() {
389396
test1();
390397
test2();

tests/ui/never_loop.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,5 +161,14 @@ LL | | if b { break 'c } else { break 'b }
161161
LL | | }
162162
| |_____________^
163163

164-
error: aborting due to 14 previous errors
164+
error: this loop never actually loops
165+
--> $DIR/never_loop.rs:389:5
166+
|
167+
LL | / loop {
168+
LL | |
169+
LL | | panic!("oh no");
170+
LL | | }
171+
| |_____^
172+
173+
error: aborting due to 15 previous errors
165174

0 commit comments

Comments
 (0)