@@ -14,6 +14,7 @@ use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
1414use rustc_middle:: lint:: in_external_macro;
1515use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
1616use rustc_span:: source_map:: Span ;
17+ use rustc_span:: symbol:: Ident ;
1718
1819declare_clippy_lint ! {
1920 /// **What it does:** Checks for structure field patterns bound to wildcards.
@@ -493,6 +494,29 @@ impl EarlyLintPass for MiscEarlyLints {
493494 }
494495
495496 fn check_block ( & mut self , cx : & EarlyContext < ' _ > , block : & Block ) {
497+ fn count_closure_usage ( block : & Block , ident : & Ident ) -> usize {
498+ struct ClosureUsageCount < ' ast > {
499+ ident : & ' ast Ident ,
500+ count : usize ,
501+ } ;
502+ impl < ' ast > Visitor < ' ast > for ClosureUsageCount < ' ast > {
503+ fn visit_expr ( & mut self , expr : & ' ast Expr ) {
504+ if_chain ! {
505+ if let ExprKind :: Call ( ref closure, _) = expr. kind;
506+ if let ExprKind :: Path ( _, ref path) = closure. kind;
507+ if self . ident == & path. segments[ 0 ] . ident;
508+ then {
509+ self . count += 1 ;
510+ }
511+ }
512+ walk_expr ( self , expr) ;
513+ }
514+ }
515+ let mut closure_usage_count = ClosureUsageCount { ident, count : 0 } ;
516+ closure_usage_count. visit_block ( block) ;
517+ closure_usage_count. count
518+ }
519+
496520 for w in block. stmts . windows ( 2 ) {
497521 if_chain ! {
498522 if let StmtKind :: Local ( ref local) = w[ 0 ] . kind;
@@ -503,15 +527,15 @@ impl EarlyLintPass for MiscEarlyLints {
503527 if let ExprKind :: Assign ( _, ref call, _) = second. kind;
504528 if let ExprKind :: Call ( ref closure, _) = call. kind;
505529 if let ExprKind :: Path ( _, ref path) = closure. kind;
530+ if ident == path. segments[ 0 ] . ident;
531+ if count_closure_usage( block, & ident) == 1 ;
506532 then {
507- if ident == path. segments[ 0 ] . ident {
508- span_lint(
509- cx,
510- REDUNDANT_CLOSURE_CALL ,
511- second. span,
512- "Closure called just once immediately after it was declared" ,
513- ) ;
514- }
533+ span_lint(
534+ cx,
535+ REDUNDANT_CLOSURE_CALL ,
536+ second. span,
537+ "Closure called just once immediately after it was declared" ,
538+ ) ;
515539 }
516540 }
517541 }
0 commit comments