|
| 1 | +use crate::rustc_lint::LintContext; |
1 | 2 | use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
|
| 3 | +use clippy_utils::get_parent_expr; |
2 | 4 | use clippy_utils::sugg::Sugg;
|
3 | 5 | 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 | 6 | use rustc_errors::Applicability;
|
8 | 7 | use rustc_hir as hir;
|
9 | 8 | use rustc_hir::intravisit as hir_visit;
|
10 | 9 | use rustc_hir::intravisit::Visitor as HirVisitor;
|
11 |
| -use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext}; |
| 10 | +use rustc_hir::intravisit::Visitor; |
| 11 | +use rustc_lint::{LateContext, LateLintPass}; |
12 | 12 | use rustc_middle::hir::nested_filter;
|
13 | 13 | use rustc_middle::lint::in_external_macro;
|
14 | 14 | use rustc_session::{declare_lint_pass, declare_tool_lint};
|
@@ -51,59 +51,136 @@ impl ReturnVisitor {
|
51 | 51 | }
|
52 | 52 | }
|
53 | 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 { |
| 54 | +impl<'tcx> Visitor<'tcx> for ReturnVisitor { |
| 55 | + fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) { |
| 56 | + if let hir::ExprKind::Ret(_) | hir::ExprKind::Match(.., hir::MatchSource::TryDesugar) = ex.kind { |
57 | 57 | self.found_return = true;
|
| 58 | + } else { |
| 59 | + hir_visit::walk_expr(self, ex); |
58 | 60 | }
|
| 61 | + } |
| 62 | +} |
59 | 63 |
|
60 |
| - ast_visit::walk_expr(self, ex); |
| 64 | +/// Checks if the body is owned by an async closure |
| 65 | +fn is_async_closure(body: &hir::Body<'_>) -> bool { |
| 66 | + if let hir::ExprKind::Closure(closure) = body.value.kind |
| 67 | + && let [resume_ty] = closure.fn_decl.inputs |
| 68 | + && let hir::TyKind::Path(hir::QPath::LangItem(hir::LangItem::ResumeTy, ..)) = resume_ty.kind |
| 69 | + { |
| 70 | + true |
| 71 | + } else { |
| 72 | + false |
61 | 73 | }
|
62 | 74 | }
|
63 | 75 |
|
64 |
| -impl EarlyLintPass for RedundantClosureCall { |
65 |
| - fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) { |
| 76 | +/// Tries to find the innermost closure: |
| 77 | +/// ```rust,ignore |
| 78 | +/// (|| || || || 42)()()()() |
| 79 | +/// ^^^^^^^^^^^^^^ given this nested closure expression |
| 80 | +/// ^^^^^ we want to return this closure |
| 81 | +/// ``` |
| 82 | +/// It also has a parameter for how many steps to go in at most, so as to |
| 83 | +/// not take more closures than there are calls. |
| 84 | +fn find_innermost_closure<'tcx>( |
| 85 | + cx: &LateContext<'tcx>, |
| 86 | + mut expr: &'tcx hir::Expr<'tcx>, |
| 87 | + mut steps: usize, |
| 88 | +) -> Option<(&'tcx hir::Expr<'tcx>, &'tcx hir::FnDecl<'tcx>, hir::IsAsync)> { |
| 89 | + let mut data = None; |
| 90 | + |
| 91 | + while let hir::ExprKind::Closure(closure) = expr.kind |
| 92 | + && let body = cx.tcx.hir().body(closure.body) |
| 93 | + && { |
| 94 | + let mut visitor = ReturnVisitor::new(); |
| 95 | + visitor.visit_expr(body.value); |
| 96 | + !visitor.found_return |
| 97 | + } |
| 98 | + && steps > 0 |
| 99 | + { |
| 100 | + expr = body.value; |
| 101 | + data = Some((body.value, closure.fn_decl, if is_async_closure(body) { |
| 102 | + hir::IsAsync::Async |
| 103 | + } else { |
| 104 | + hir::IsAsync::NotAsync |
| 105 | + })); |
| 106 | + steps -= 1; |
| 107 | + } |
| 108 | + |
| 109 | + data |
| 110 | +} |
| 111 | + |
| 112 | +/// "Walks up" the chain of calls to find the outermost call expression, and returns the depth: |
| 113 | +/// ```rust,ignore |
| 114 | +/// (|| || || 3)()()() |
| 115 | +/// ^^ this is the call expression we were given |
| 116 | +/// ^^ this is what we want to return (and the depth is 3) |
| 117 | +/// ``` |
| 118 | +fn get_parent_call_exprs<'tcx>( |
| 119 | + cx: &LateContext<'tcx>, |
| 120 | + mut expr: &'tcx hir::Expr<'tcx>, |
| 121 | +) -> (&'tcx hir::Expr<'tcx>, usize) { |
| 122 | + let mut depth = 1; |
| 123 | + while let Some(parent) = get_parent_expr(cx, expr) |
| 124 | + && let hir::ExprKind::Call(recv, _) = parent.kind |
| 125 | + && expr.span == recv.span |
| 126 | + { |
| 127 | + expr = parent; |
| 128 | + depth += 1; |
| 129 | + } |
| 130 | + (expr, depth) |
| 131 | +} |
| 132 | + |
| 133 | +impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall { |
| 134 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) { |
66 | 135 | if in_external_macro(cx.sess(), expr.span) {
|
67 | 136 | return;
|
68 | 137 | }
|
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); |
| 138 | + |
| 139 | + if let hir::ExprKind::Call(recv, _) = expr.kind |
| 140 | + // don't lint if the receiver is a call, too. |
| 141 | + // we do this in order to prevent linting multiple times; consider: |
| 142 | + // `(|| || 1)()()` |
| 143 | + // ^^ we only want to lint for this call (but we walk up the calls to consider both calls). |
| 144 | + // without this check, we'd end up linting twice. |
| 145 | + && !matches!(recv.kind, hir::ExprKind::Call(..)) |
| 146 | + && let (full_expr, call_depth) = get_parent_call_exprs(cx, expr) |
| 147 | + && let Some((body, fn_decl, generator_kind)) = find_innermost_closure(cx, recv, call_depth) |
| 148 | + { |
| 149 | + span_lint_and_then( |
| 150 | + cx, |
| 151 | + REDUNDANT_CLOSURE_CALL, |
| 152 | + full_expr.span, |
| 153 | + "try not to call a closure in the expression where it is declared", |
| 154 | + |diag| { |
| 155 | + if fn_decl.inputs.is_empty() { |
| 156 | + let mut applicability = Applicability::MachineApplicable; |
| 157 | + let mut hint = Sugg::hir_with_context(cx, body, full_expr.span.ctxt(), "..", &mut applicability); |
| 158 | + |
| 159 | + if generator_kind.is_async() |
| 160 | + && let hir::ExprKind::Closure(closure) = body.kind |
| 161 | + { |
| 162 | + let async_closure_body = cx.tcx.hir().body(closure.body); |
| 163 | + |
| 164 | + // `async x` is a syntax error, so it becomes `async { x }` |
| 165 | + if !matches!(async_closure_body.value.kind, hir::ExprKind::Block(_, _)) { |
| 166 | + hint = hint.blockify(); |
97 | 167 | }
|
98 |
| - }, |
99 |
| - ); |
| 168 | + |
| 169 | + hint = hint.asyncify(); |
| 170 | + } |
| 171 | + |
| 172 | + diag.span_suggestion( |
| 173 | + full_expr.span, |
| 174 | + "try doing something like", |
| 175 | + hint.maybe_par(), |
| 176 | + applicability |
| 177 | + ); |
| 178 | + } |
100 | 179 | }
|
101 |
| - } |
| 180 | + ); |
102 | 181 | }
|
103 | 182 | }
|
104 |
| -} |
105 | 183 |
|
106 |
| -impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall { |
107 | 184 | fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx hir::Block<'_>) {
|
108 | 185 | fn count_closure_usage<'tcx>(
|
109 | 186 | cx: &LateContext<'tcx>,
|
|
0 commit comments