@@ -3,7 +3,7 @@ use rustc_hir::intravisit::{walk_expr, Visitor};
3
3
use rustc_hir:: { Block , BlockCheckMode , Closure , Expr , ExprKind , Stmt , StmtKind } ;
4
4
use rustc_lint:: { LateContext , LateLintPass } ;
5
5
use rustc_session:: declare_lint_pass;
6
- use rustc_span:: { sym, Span , Symbol } ;
6
+ use rustc_span:: { sym, Span } ;
7
7
8
8
use clippy_utils:: diagnostics:: span_lint_and_then;
9
9
use clippy_utils:: is_trait_method;
@@ -55,23 +55,17 @@ declare_lint_pass!(NeedlessForEach => [NEEDLESS_FOR_EACH]);
55
55
56
56
impl < ' tcx > LateLintPass < ' tcx > for NeedlessForEach {
57
57
fn check_stmt ( & mut self , cx : & LateContext < ' tcx > , stmt : & ' tcx Stmt < ' _ > ) {
58
- let ( StmtKind :: Expr ( expr) | StmtKind :: Semi ( expr) ) = stmt. kind else {
59
- return ;
60
- } ;
61
-
62
- if let ExprKind :: MethodCall ( method_name, for_each_recv, [ for_each_arg] , _) = expr. kind
63
- // Check the method name is `for_each`.
64
- && method_name. ident . name == Symbol :: intern ( "for_each" )
65
- // Check `for_each` is an associated function of `Iterator`.
66
- && is_trait_method ( cx, expr, sym:: Iterator )
67
- // Checks the receiver of `for_each` is also a method call.
58
+ if let StmtKind :: Expr ( expr) | StmtKind :: Semi ( expr) = stmt. kind
59
+ && let ExprKind :: MethodCall ( method_name, for_each_recv, [ for_each_arg] , _) = expr. kind
68
60
&& let ExprKind :: MethodCall ( _, iter_recv, [ ] , _) = for_each_recv. kind
69
61
// Skip the lint if the call chain is too long. e.g. `v.field.iter().for_each()` or
70
62
// `v.foo().iter().for_each()` must be skipped.
71
63
&& matches ! (
72
64
iter_recv. kind,
73
65
ExprKind :: Array ( ..) | ExprKind :: Call ( ..) | ExprKind :: Path ( ..)
74
66
)
67
+ && method_name. ident . name . as_str ( ) == "for_each"
68
+ && is_trait_method ( cx, expr, sym:: Iterator )
75
69
// Checks the type of the `iter` method receiver is NOT a user defined type.
76
70
&& has_iter_method ( cx, cx. typeck_results ( ) . expr_ty ( iter_recv) ) . is_some ( )
77
71
// Skip the lint if the body is not block because this is simpler than `for` loop.
0 commit comments