@@ -481,17 +481,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Loops {
481
481
}
482
482
483
483
// check for never_loop
484
- match expr. node {
485
- ExprKind :: While ( _, ref block, _) | ExprKind :: Loop ( ref block, _, _) => {
486
- match never_loop_block ( block, expr. hir_id ) {
487
- NeverLoopResult :: AlwaysBreak => {
488
- span_lint ( cx, NEVER_LOOP , expr. span , "this loop never actually loops" )
489
- } ,
490
- NeverLoopResult :: MayContinueMainLoop | NeverLoopResult :: Otherwise => ( ) ,
491
- }
492
- } ,
493
- _ => ( ) ,
494
- }
484
+ if let ExprKind :: Loop ( ref block, _, _) = expr. node {
485
+ match never_loop_block ( block, expr. hir_id ) {
486
+ NeverLoopResult :: AlwaysBreak => {
487
+ span_lint ( cx, NEVER_LOOP , expr. span , "this loop never actually loops" )
488
+ } ,
489
+ NeverLoopResult :: MayContinueMainLoop | NeverLoopResult :: Otherwise => ( ) ,
490
+ }
491
+ } ;
495
492
496
493
// check for `loop { if let {} else break }` that could be `while let`
497
494
// (also matches an explicit "match" instead of "if let")
@@ -590,9 +587,16 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Loops {
590
587
}
591
588
}
592
589
593
- // check for while loops which conditions never change
594
- if let ExprKind :: While ( ref cond, _, _) = expr. node {
595
- check_infinite_loop ( cx, cond, expr) ;
590
+ if_chain ! {
591
+ if let ExprKind :: Loop ( block, _, LoopSource :: While ) = & expr. node;
592
+ if let Block { expr: Some ( expr) , .. } = & * * block;
593
+ if let ExprKind :: Match ( cond, arms, MatchSource :: WhileDesugar ) = & expr. node;
594
+ if let ExprKind :: DropTemps ( cond) = & cond. node;
595
+ if let [ arm, ..] = & arms[ ..] ;
596
+ if let Arm { body, .. } = arm;
597
+ then {
598
+ check_infinite_loop( cx, cond, body) ;
599
+ }
596
600
}
597
601
598
602
check_needless_collect ( expr, cx) ;
@@ -701,12 +705,6 @@ fn never_loop_expr(expr: &Expr, main_loop_id: HirId) -> NeverLoopResult {
701
705
// Break can come from the inner loop so remove them.
702
706
absorb_break ( & never_loop_block ( b, main_loop_id) )
703
707
} ,
704
- ExprKind :: While ( ref e, ref b, _) => {
705
- let e = never_loop_expr ( e, main_loop_id) ;
706
- let result = never_loop_block ( b, main_loop_id) ;
707
- // Break can come from the inner loop so remove them.
708
- combine_seq ( e, absorb_break ( & result) )
709
- } ,
710
708
ExprKind :: Match ( ref e, ref arms, _) => {
711
709
let e = never_loop_expr ( e, main_loop_id) ;
712
710
if arms. is_empty ( ) {
@@ -2202,7 +2200,7 @@ fn var_def_id(cx: &LateContext<'_, '_>, expr: &Expr) -> Option<HirId> {
2202
2200
2203
2201
fn is_loop ( expr : & Expr ) -> bool {
2204
2202
match expr. node {
2205
- ExprKind :: Loop ( ..) | ExprKind :: While ( .. ) => true ,
2203
+ ExprKind :: Loop ( ..) => true ,
2206
2204
_ => false ,
2207
2205
}
2208
2206
}
@@ -2239,11 +2237,10 @@ fn is_loop_nested(cx: &LateContext<'_, '_>, loop_expr: &Expr, iter_expr: &Expr)
2239
2237
return false ;
2240
2238
}
2241
2239
match cx. tcx . hir ( ) . find ( parent) {
2242
- Some ( Node :: Expr ( expr) ) => match expr . node {
2243
- ExprKind :: Loop ( .. ) | ExprKind :: While ( ..) => {
2240
+ Some ( Node :: Expr ( expr) ) => {
2241
+ if let ExprKind :: Loop ( ..) = expr . node {
2244
2242
return true ;
2245
- } ,
2246
- _ => ( ) ,
2243
+ } ;
2247
2244
} ,
2248
2245
Some ( Node :: Block ( block) ) => {
2249
2246
let mut block_visitor = LoopNestVisitor {
0 commit comments