1 use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
2 use clippy_utils::source::snippet_with_applicability;
3 use if_chain::if_chain;
5 use rustc_ast::visit as ast_visit;
6 use rustc_ast::visit::Visitor as AstVisitor;
7 use rustc_errors::Applicability;
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::map::Map;
13 use rustc_middle::lint::in_external_macro;
14 use rustc_session::{declare_lint_pass, declare_tool_lint};
16 declare_clippy_lint! {
18 /// Detects closures called in the same expression where they
21 /// ### Why is this bad?
22 /// It is unnecessarily adding to the expression's
33 pub REDUNDANT_CLOSURE_CALL,
35 "throwaway closures called in the expression they are defined"
38 declare_lint_pass!(RedundantClosureCall => [REDUNDANT_CLOSURE_CALL]);
40 // Used to find `return` statements or equivalents e.g., `?`
41 struct ReturnVisitor {
48 Self { found_return: false }
52 impl<'ast> ast_visit::Visitor<'ast> for ReturnVisitor {
53 fn visit_expr(&mut self, ex: &'ast ast::Expr) {
54 if let ast::ExprKind::Ret(_) = ex.kind {
55 self.found_return = true;
56 } else if let ast::ExprKind::Try(_) = ex.kind {
57 self.found_return = true;
60 ast_visit::walk_expr(self, ex);
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) {
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(_, _, _, ref decl, ref block, _) = closure.kind;
74 let mut visitor = ReturnVisitor::new();
75 visitor.visit_expr(block);
76 if !visitor.found_return {
79 REDUNDANT_CLOSURE_CALL,
81 "try not to call a closure in the expression where it is declared",
83 if decl.inputs.is_empty() {
84 let mut app = Applicability::MachineApplicable;
86 snippet_with_applicability(cx, block.span, "..", &mut app).into_owned();
87 diag.span_suggestion(expr.span, "try doing something like", hint, app);
97 impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
98 fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx hir::Block<'_>) {
99 fn count_closure_usage<'a, 'tcx>(
100 cx: &'a LateContext<'tcx>,
101 block: &'tcx hir::Block<'_>,
102 path: &'tcx hir::Path<'tcx>,
104 struct ClosureUsageCount<'a, 'tcx> {
105 cx: &'a LateContext<'tcx>,
106 path: &'tcx hir::Path<'tcx>,
109 impl<'a, 'tcx> hir_visit::Visitor<'tcx> for ClosureUsageCount<'a, 'tcx> {
110 type Map = Map<'tcx>;
112 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
114 if let hir::ExprKind::Call(closure, _) = expr.kind;
115 if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = closure.kind;
116 if self.path.segments[0].ident == path.segments[0].ident;
117 if self.path.res == path.res;
122 hir_visit::walk_expr(self, expr);
125 fn nested_visit_map(&mut self) -> hir_visit::NestedVisitorMap<Self::Map> {
126 hir_visit::NestedVisitorMap::OnlyBodies(self.cx.tcx.hir())
129 let mut closure_usage_count = ClosureUsageCount { cx, path, count: 0 };
130 closure_usage_count.visit_block(block);
131 closure_usage_count.count
134 for w in block.stmts.windows(2) {
136 if let hir::StmtKind::Local(local) = w[0].kind;
137 if let Option::Some(t) = local.init;
138 if let hir::ExprKind::Closure(..) = t.kind;
139 if let hir::PatKind::Binding(_, _, ident, _) = local.pat.kind;
140 if let hir::StmtKind::Semi(second) = w[1].kind;
141 if let hir::ExprKind::Assign(_, call, _) = second.kind;
142 if let hir::ExprKind::Call(closure, _) = call.kind;
143 if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = closure.kind;
144 if ident == path.segments[0].ident;
145 if count_closure_usage(cx, block, path) == 1;
149 REDUNDANT_CLOSURE_CALL,
151 "closure called just once immediately after it was declared",