]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/redundant_closure_call.rs
New upstream version 1.67.1+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / redundant_closure_call.rs
1 use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
2 use clippy_utils::sugg::Sugg;
3 use if_chain::if_chain;
4 use rustc_ast::ast;
5 use rustc_ast::visit as ast_visit;
6 use rustc_ast::visit::Visitor as AstVisitor;
7 use rustc_errors::Applicability;
8 use rustc_hir as hir;
9 use rustc_hir::intravisit as hir_visit;
10 use rustc_hir::intravisit::Visitor as HirVisitor;
11 use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
12 use rustc_middle::hir::nested_filter;
13 use rustc_middle::lint::in_external_macro;
14 use rustc_session::{declare_lint_pass, declare_tool_lint};
15
16 declare_clippy_lint! {
17 /// ### What it does
18 /// Detects closures called in the same expression where they
19 /// are defined.
20 ///
21 /// ### Why is this bad?
22 /// It is unnecessarily adding to the expression's
23 /// complexity.
24 ///
25 /// ### Example
26 /// ```rust
27 /// let a = (|| 42)();
28 /// ```
29 ///
30 /// Use instead:
31 /// ```rust
32 /// let a = 42;
33 /// ```
34 #[clippy::version = "pre 1.29.0"]
35 pub REDUNDANT_CLOSURE_CALL,
36 complexity,
37 "throwaway closures called in the expression they are defined"
38 }
39
40 declare_lint_pass!(RedundantClosureCall => [REDUNDANT_CLOSURE_CALL]);
41
42 // Used to find `return` statements or equivalents e.g., `?`
43 struct ReturnVisitor {
44 found_return: bool,
45 }
46
47 impl ReturnVisitor {
48 #[must_use]
49 fn new() -> Self {
50 Self { found_return: false }
51 }
52 }
53
54 impl<'ast> ast_visit::Visitor<'ast> for ReturnVisitor {
55 fn visit_expr(&mut self, ex: &'ast ast::Expr) {
56 if let ast::ExprKind::Ret(_) | ast::ExprKind::Try(_) = ex.kind {
57 self.found_return = true;
58 }
59
60 ast_visit::walk_expr(self, ex);
61 }
62 }
63
64 impl EarlyLintPass for RedundantClosureCall {
65 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
66 if in_external_macro(cx.sess(), expr.span) {
67 return;
68 }
69 if_chain! {
70 if let ast::ExprKind::Call(ref paren, _) = expr.kind;
71 if let ast::ExprKind::Paren(ref closure) = paren.kind;
72 if let ast::ExprKind::Closure(box ast::Closure { ref asyncness, ref fn_decl, ref body, .. }) = closure.kind;
73 then {
74 let mut visitor = ReturnVisitor::new();
75 visitor.visit_expr(body);
76 if !visitor.found_return {
77 span_lint_and_then(
78 cx,
79 REDUNDANT_CLOSURE_CALL,
80 expr.span,
81 "try not to call a closure in the expression where it is declared",
82 |diag| {
83 if fn_decl.inputs.is_empty() {
84 let mut app = Applicability::MachineApplicable;
85 let mut hint = Sugg::ast(cx, body, "..", closure.span.ctxt(), &mut app);
86
87 if asyncness.is_async() {
88 // `async x` is a syntax error, so it becomes `async { x }`
89 if !matches!(body.kind, ast::ExprKind::Block(_, _)) {
90 hint = hint.blockify();
91 }
92
93 hint = hint.asyncify();
94 }
95
96 diag.span_suggestion(expr.span, "try doing something like", hint.to_string(), app);
97 }
98 },
99 );
100 }
101 }
102 }
103 }
104 }
105
106 impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
107 fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx hir::Block<'_>) {
108 fn count_closure_usage<'tcx>(
109 cx: &LateContext<'tcx>,
110 block: &'tcx hir::Block<'_>,
111 path: &'tcx hir::Path<'tcx>,
112 ) -> usize {
113 struct ClosureUsageCount<'a, 'tcx> {
114 cx: &'a LateContext<'tcx>,
115 path: &'tcx hir::Path<'tcx>,
116 count: usize,
117 }
118 impl<'a, 'tcx> hir_visit::Visitor<'tcx> for ClosureUsageCount<'a, 'tcx> {
119 type NestedFilter = nested_filter::OnlyBodies;
120
121 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
122 if_chain! {
123 if let hir::ExprKind::Call(closure, _) = expr.kind;
124 if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = closure.kind;
125 if self.path.segments[0].ident == path.segments[0].ident;
126 if self.path.res == path.res;
127 then {
128 self.count += 1;
129 }
130 }
131 hir_visit::walk_expr(self, expr);
132 }
133
134 fn nested_visit_map(&mut self) -> Self::Map {
135 self.cx.tcx.hir()
136 }
137 }
138 let mut closure_usage_count = ClosureUsageCount { cx, path, count: 0 };
139 closure_usage_count.visit_block(block);
140 closure_usage_count.count
141 }
142
143 for w in block.stmts.windows(2) {
144 if_chain! {
145 if let hir::StmtKind::Local(local) = w[0].kind;
146 if let Option::Some(t) = local.init;
147 if let hir::ExprKind::Closure { .. } = t.kind;
148 if let hir::PatKind::Binding(_, _, ident, _) = local.pat.kind;
149 if let hir::StmtKind::Semi(second) = w[1].kind;
150 if let hir::ExprKind::Assign(_, call, _) = second.kind;
151 if let hir::ExprKind::Call(closure, _) = call.kind;
152 if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = closure.kind;
153 if ident == path.segments[0].ident;
154 if count_closure_usage(cx, block, path) == 1;
155 then {
156 span_lint(
157 cx,
158 REDUNDANT_CLOSURE_CALL,
159 second.span,
160 "closure called just once immediately after it was declared",
161 );
162 }
163 }
164 }
165 }
166 }