@@ -4,35 +4,41 @@ use clippy_utils::diagnostics::span_lint_and_then;
4
4
use clippy_utils:: higher:: ForLoop ;
5
5
use clippy_utils:: source:: snippet;
6
6
use rustc_errors:: Applicability ;
7
- use rustc_hir:: { Block , Expr , ExprKind , HirId , InlineAsmOperand , LoopSource , Node , Pat , Stmt , StmtKind } ;
7
+ use rustc_hir:: { Block , Expr , ExprKind , HirId , InlineAsmOperand , Pat , Stmt , StmtKind } ;
8
8
use rustc_lint:: LateContext ;
9
+ use rustc_span:: Span ;
9
10
use std:: iter:: { once, Iterator } ;
10
11
11
- pub ( super ) fn check ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
12
- if let ExprKind :: Loop ( block, _, source, _) = expr. kind {
13
- match never_loop_block ( block, expr. hir_id ) {
14
- NeverLoopResult :: AlwaysBreak => {
15
- span_lint_and_then ( cx, NEVER_LOOP , expr. span , "this loop never actually loops" , |diag| {
16
- if_chain ! {
17
- if source == LoopSource :: ForLoop ;
18
- if let Some ( ( _, Node :: Expr ( parent_match) ) ) = cx. tcx. hir( ) . parent_iter( expr. hir_id) . nth( 1 ) ;
19
- if let Some ( ForLoop { arg: iterator, pat, span: for_span, .. } ) = ForLoop :: hir( parent_match) ;
20
- then {
21
- // Suggests using an `if let` instead. This is `Unspecified` because the
22
- // loop may (probably) contain `break` statements which would be invalid
23
- // in an `if let`.
24
- diag. span_suggestion_verbose(
25
- for_span. with_hi( iterator. span. hi( ) ) ,
26
- "if you need the first element of the iterator, try writing" ,
27
- for_to_if_let_sugg( cx, iterator, pat) ,
28
- Applicability :: Unspecified ,
29
- ) ;
30
- }
31
- } ;
32
- } ) ;
33
- } ,
34
- NeverLoopResult :: MayContinueMainLoop | NeverLoopResult :: Otherwise => ( ) ,
35
- }
12
+ pub ( super ) fn check (
13
+ cx : & LateContext < ' tcx > ,
14
+ block : & ' tcx Block < ' _ > ,
15
+ loop_id : HirId ,
16
+ span : Span ,
17
+ for_loop : Option < & ForLoop < ' _ > > ,
18
+ ) {
19
+ match never_loop_block ( block, loop_id) {
20
+ NeverLoopResult :: AlwaysBreak => {
21
+ span_lint_and_then ( cx, NEVER_LOOP , span, "this loop never actually loops" , |diag| {
22
+ if let Some ( ForLoop {
23
+ arg : iterator,
24
+ pat,
25
+ span : for_span,
26
+ ..
27
+ } ) = for_loop
28
+ {
29
+ // Suggests using an `if let` instead. This is `Unspecified` because the
30
+ // loop may (probably) contain `break` statements which would be invalid
31
+ // in an `if let`.
32
+ diag. span_suggestion_verbose (
33
+ for_span. with_hi ( iterator. span . hi ( ) ) ,
34
+ "if you need the first element of the iterator, try writing" ,
35
+ for_to_if_let_sugg ( cx, iterator, pat) ,
36
+ Applicability :: Unspecified ,
37
+ ) ;
38
+ }
39
+ } ) ;
40
+ } ,
41
+ NeverLoopResult :: MayContinueMainLoop | NeverLoopResult :: Otherwise => ( ) ,
36
42
}
37
43
}
38
44
0 commit comments