]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/eta_reduction.rs
Merge pull request #2821 from mati865/rust-2018-migration
[rust.git] / clippy_lints / src / eta_reduction.rs
1 use rustc::lint::*;
2 use rustc::ty;
3 use rustc::hir::*;
4 use crate::utils::{is_adjusted, iter_input_pats, snippet_opt, span_lint_and_then};
5
6 #[allow(missing_copy_implementations)]
7 pub struct EtaPass;
8
9
10 /// **What it does:** Checks for closures which just call another function where
11 /// the function can be called directly. `unsafe` functions or calls where types
12 /// get adjusted are ignored.
13 ///
14 /// **Why is this bad?** Needlessly creating a closure adds code for no benefit
15 /// and gives the optimizer more work.
16 ///
17 /// **Known problems:** None.
18 ///
19 /// **Example:**
20 /// ```rust
21 /// xs.map(|x| foo(x))
22 /// ```
23 /// where `foo(_)` is a plain function that takes the exact argument type of
24 /// `x`.
25 declare_clippy_lint! {
26     pub REDUNDANT_CLOSURE,
27     style,
28     "redundant closures, i.e. `|a| foo(a)` (which can be written as just `foo`)"
29 }
30
31 impl LintPass for EtaPass {
32     fn get_lints(&self) -> LintArray {
33         lint_array!(REDUNDANT_CLOSURE)
34     }
35 }
36
37 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EtaPass {
38     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
39         match expr.node {
40             ExprCall(_, ref args) | ExprMethodCall(_, _, ref args) => for arg in args {
41                 check_closure(cx, arg)
42             },
43             _ => (),
44         }
45     }
46 }
47
48 fn check_closure(cx: &LateContext, expr: &Expr) {
49     if let ExprClosure(_, ref decl, eid, _, _) = expr.node {
50         let body = cx.tcx.hir.body(eid);
51         let ex = &body.value;
52         if let ExprCall(ref caller, ref args) = ex.node {
53             if args.len() != decl.inputs.len() {
54                 // Not the same number of arguments, there
55                 // is no way the closure is the same as the function
56                 return;
57             }
58             if is_adjusted(cx, ex) || args.iter().any(|arg| is_adjusted(cx, arg)) {
59                 // Are the expression or the arguments type-adjusted? Then we need the closure
60                 return;
61             }
62             let fn_ty = cx.tables.expr_ty(caller);
63             match fn_ty.sty {
64                 // Is it an unsafe function? They don't implement the closure traits
65                 ty::TyFnDef(..) | ty::TyFnPtr(_) => {
66                     let sig = fn_ty.fn_sig(cx.tcx);
67                     if sig.skip_binder().unsafety == Unsafety::Unsafe || sig.skip_binder().output().sty == ty::TyNever {
68                         return;
69                     }
70                 },
71                 _ => (),
72             }
73             for (a1, a2) in iter_input_pats(decl, body).zip(args) {
74                 if let PatKind::Binding(_, _, ident, _) = a1.pat.node {
75                     // XXXManishearth Should I be checking the binding mode here?
76                     if let ExprPath(QPath::Resolved(None, ref p)) = a2.node {
77                         if p.segments.len() != 1 {
78                             // If it's a proper path, it can't be a local variable
79                             return;
80                         }
81                         if p.segments[0].name != ident.node {
82                             // The two idents should be the same
83                             return;
84                         }
85                     } else {
86                         return;
87                     }
88                 } else {
89                     return;
90                 }
91             }
92             span_lint_and_then(cx, REDUNDANT_CLOSURE, expr.span, "redundant closure found", |db| {
93                 if let Some(snippet) = snippet_opt(cx, caller.span) {
94                     db.span_suggestion(expr.span, "remove closure as shown", snippet);
95                 }
96             });
97         }
98     }
99 }