1 use crate::utils::{snippet_with_applicability, span_lint, span_lint_and_then};
2 use if_chain::if_chain;
4 use rustc_ast::visit as ast_visit;
5 use rustc_ast::visit::Visitor as AstVisitor;
6 use rustc_errors::Applicability;
8 use rustc_hir::intravisit as hir_visit;
9 use rustc_hir::intravisit::Visitor as HirVisitor;
10 use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
11 use rustc_middle::hir::map::Map;
12 use rustc_middle::lint::in_external_macro;
13 use rustc_session::{declare_lint_pass, declare_tool_lint};
15 declare_clippy_lint! {
16 /// **What it does:** Detects closures called in the same expression where they
19 /// **Why is this bad?** It is unnecessarily adding to the expression's
22 /// **Known problems:** None.
32 pub REDUNDANT_CLOSURE_CALL,
34 "throwaway closures called in the expression they are defined"
37 declare_lint_pass!(RedundantClosureCall => [REDUNDANT_CLOSURE_CALL]);
39 // Used to find `return` statements or equivalents e.g., `?`
40 struct ReturnVisitor {
47 Self { found_return: false }
51 impl<'ast> ast_visit::Visitor<'ast> for ReturnVisitor {
52 fn visit_expr(&mut self, ex: &'ast ast::Expr) {
53 if let ast::ExprKind::Ret(_) = ex.kind {
54 self.found_return = true;
55 } else if let ast::ExprKind::Try(_) = ex.kind {
56 self.found_return = true;
59 ast_visit::walk_expr(self, ex)
63 impl EarlyLintPass for RedundantClosureCall {
64 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
65 if in_external_macro(cx.sess(), expr.span) {
69 if let ast::ExprKind::Call(ref paren, _) = expr.kind;
70 if let ast::ExprKind::Paren(ref closure) = paren.kind;
71 if let ast::ExprKind::Closure(_, _, _, ref decl, ref block, _) = closure.kind;
73 let mut visitor = ReturnVisitor::new();
74 visitor.visit_expr(block);
75 if !visitor.found_return {
78 REDUNDANT_CLOSURE_CALL,
80 "try not to call a closure in the expression where it is declared",
82 if decl.inputs.is_empty() {
83 let mut app = Applicability::MachineApplicable;
85 snippet_with_applicability(cx, block.span, "..", &mut app).into_owned();
86 diag.span_suggestion(expr.span, "try doing something like", hint, app);
96 impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
97 fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx hir::Block<'_>) {
98 fn count_closure_usage<'a, 'tcx>(
99 cx: &'a LateContext<'tcx>,
100 block: &'tcx hir::Block<'_>,
101 path: &'tcx hir::Path<'tcx>,
103 struct ClosureUsageCount<'a, 'tcx> {
104 cx: &'a LateContext<'tcx>,
105 path: &'tcx hir::Path<'tcx>,
108 impl<'a, 'tcx> hir_visit::Visitor<'tcx> for ClosureUsageCount<'a, 'tcx> {
109 type Map = Map<'tcx>;
111 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
113 if let hir::ExprKind::Call(ref closure, _) = expr.kind;
114 if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = closure.kind;
115 if self.path.segments[0].ident == path.segments[0].ident
116 && self.path.res == path.res;
121 hir_visit::walk_expr(self, expr);
124 fn nested_visit_map(&mut self) -> hir_visit::NestedVisitorMap<Self::Map> {
125 hir_visit::NestedVisitorMap::OnlyBodies(self.cx.tcx.hir())
128 let mut closure_usage_count = ClosureUsageCount { cx, path, count: 0 };
129 closure_usage_count.visit_block(block);
130 closure_usage_count.count
133 for w in block.stmts.windows(2) {
135 if let hir::StmtKind::Local(ref local) = w[0].kind;
136 if let Option::Some(ref t) = local.init;
137 if let hir::ExprKind::Closure(..) = t.kind;
138 if let hir::PatKind::Binding(_, _, ident, _) = local.pat.kind;
139 if let hir::StmtKind::Semi(ref second) = w[1].kind;
140 if let hir::ExprKind::Assign(_, ref call, _) = second.kind;
141 if let hir::ExprKind::Call(ref closure, _) = call.kind;
142 if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = closure.kind;
143 if ident == path.segments[0].ident;
144 if count_closure_usage(cx, block, path) == 1;
148 REDUNDANT_CLOSURE_CALL,
150 "closure called just once immediately after it was declared",