1- use crate :: utils:: {
2- constants, snippet_opt, snippet_with_applicability, span_lint, span_lint_and_help, span_lint_and_sugg,
3- span_lint_and_then,
4- } ;
5- use if_chain:: if_chain;
1+ use crate :: utils:: { constants, snippet_opt, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then} ;
62use rustc_ast:: ast:: {
7- BindingMode , Block , Expr , ExprKind , GenericParamKind , Generics , Lit , LitFloatType , LitIntType , LitKind , Mutability ,
8- NodeId , Pat , PatKind , StmtKind , UnOp ,
3+ BindingMode , Expr , ExprKind , GenericParamKind , Generics , Lit , LitFloatType , LitIntType , LitKind , Mutability ,
4+ NodeId , Pat , PatKind , UnOp ,
95} ;
10- use rustc_ast:: visit:: { walk_expr , FnKind , Visitor } ;
6+ use rustc_ast:: visit:: FnKind ;
117use rustc_data_structures:: fx:: FxHashMap ;
128use rustc_errors:: Applicability ;
139use rustc_lint:: { EarlyContext , EarlyLintPass , LintContext } ;
1410use rustc_middle:: lint:: in_external_macro;
1511use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
1612use rustc_span:: source_map:: Span ;
17- use rustc_span:: symbol:: Ident ;
1813
1914declare_clippy_lint ! {
2015 /// **What it does:** Checks for structure field patterns bound to wildcards.
@@ -71,28 +66,6 @@ declare_clippy_lint! {
7166 "function arguments having names which only differ by an underscore"
7267}
7368
74- declare_clippy_lint ! {
75- /// **What it does:** Detects closures called in the same expression where they
76- /// are defined.
77- ///
78- /// **Why is this bad?** It is unnecessarily adding to the expression's
79- /// complexity.
80- ///
81- /// **Known problems:** None.
82- ///
83- /// **Example:**
84- /// ```rust,ignore
85- /// // Bad
86- /// let a = (|| 42)()
87- ///
88- /// // Good
89- /// let a = 42
90- /// ```
91- pub REDUNDANT_CLOSURE_CALL ,
92- complexity,
93- "throwaway closures called in the expression they are defined"
94- }
95-
9669declare_clippy_lint ! {
9770 /// **What it does:** Detects expressions of the form `--x`.
9871 ///
@@ -279,7 +252,6 @@ declare_clippy_lint! {
279252declare_lint_pass ! ( MiscEarlyLints => [
280253 UNNEEDED_FIELD_PATTERN ,
281254 DUPLICATE_UNDERSCORE_ARGUMENT ,
282- REDUNDANT_CLOSURE_CALL ,
283255 DOUBLE_NEG ,
284256 MIXED_CASE_HEX_LITERALS ,
285257 UNSEPARATED_LITERAL_SUFFIX ,
@@ -289,30 +261,6 @@ declare_lint_pass!(MiscEarlyLints => [
289261 UNNEEDED_WILDCARD_PATTERN ,
290262] ) ;
291263
292- // Used to find `return` statements or equivalents e.g., `?`
293- struct ReturnVisitor {
294- found_return : bool ,
295- }
296-
297- impl ReturnVisitor {
298- #[ must_use]
299- fn new ( ) -> Self {
300- Self { found_return : false }
301- }
302- }
303-
304- impl < ' ast > Visitor < ' ast > for ReturnVisitor {
305- fn visit_expr ( & mut self , ex : & ' ast Expr ) {
306- if let ExprKind :: Ret ( _) = ex. kind {
307- self . found_return = true ;
308- } else if let ExprKind :: Try ( _) = ex. kind {
309- self . found_return = true ;
310- }
311-
312- walk_expr ( self , ex)
313- }
314- }
315-
316264impl EarlyLintPass for MiscEarlyLints {
317265 fn check_generics ( & mut self , cx : & EarlyContext < ' _ > , gen : & Generics ) {
318266 for param in & gen. params {
@@ -454,30 +402,6 @@ impl EarlyLintPass for MiscEarlyLints {
454402 return ;
455403 }
456404 match expr. kind {
457- ExprKind :: Call ( ref paren, _) => {
458- if let ExprKind :: Paren ( ref closure) = paren. kind {
459- if let ExprKind :: Closure ( _, _, _, ref decl, ref block, _) = closure. kind {
460- let mut visitor = ReturnVisitor :: new ( ) ;
461- visitor. visit_expr ( block) ;
462- if !visitor. found_return {
463- span_lint_and_then (
464- cx,
465- REDUNDANT_CLOSURE_CALL ,
466- expr. span ,
467- "Try not to call a closure in the expression where it is declared." ,
468- |diag| {
469- if decl. inputs . is_empty ( ) {
470- let mut app = Applicability :: MachineApplicable ;
471- let hint =
472- snippet_with_applicability ( cx, block. span , ".." , & mut app) . into_owned ( ) ;
473- diag. span_suggestion ( expr. span , "Try doing something like: " , hint, app) ;
474- }
475- } ,
476- ) ;
477- }
478- }
479- }
480- } ,
481405 ExprKind :: Unary ( UnOp :: Neg , ref inner) => {
482406 if let ExprKind :: Unary ( UnOp :: Neg , _) = inner. kind {
483407 span_lint (
@@ -492,54 +416,6 @@ impl EarlyLintPass for MiscEarlyLints {
492416 _ => ( ) ,
493417 }
494418 }
495-
496- 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-
520- for w in block. stmts . windows ( 2 ) {
521- if_chain ! {
522- if let StmtKind :: Local ( ref local) = w[ 0 ] . kind;
523- if let Option :: Some ( ref t) = local. init;
524- if let ExprKind :: Closure ( ..) = t. kind;
525- if let PatKind :: Ident ( _, ident, _) = local. pat. kind;
526- if let StmtKind :: Semi ( ref second) = w[ 1 ] . kind;
527- if let ExprKind :: Assign ( _, ref call, _) = second. kind;
528- if let ExprKind :: Call ( ref closure, _) = call. kind;
529- if let ExprKind :: Path ( _, ref path) = closure. kind;
530- if ident == path. segments[ 0 ] . ident;
531- if count_closure_usage( block, & ident) == 1 ;
532- then {
533- span_lint(
534- cx,
535- REDUNDANT_CLOSURE_CALL ,
536- second. span,
537- "Closure called just once immediately after it was declared" ,
538- ) ;
539- }
540- }
541- }
542- }
543419}
544420
545421impl MiscEarlyLints {
0 commit comments