]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/map_clone.rs
Rustfmtup to 0.7.1
[rust.git] / clippy_lints / src / map_clone.rs
1 use rustc::lint::*;
2 use rustc::hir::*;
3 use syntax::ast;
4 use utils::{is_adjusted, match_path, match_trait_method, match_type, remove_blocks, paths, snippet, span_help_and_lint,
5             walk_ptrs_ty, walk_ptrs_ty_depth, iter_input_pats};
6
7 /// **What it does:** Checks for mapping `clone()` over an iterator.
8 ///
9 /// **Why is this bad?** It makes the code less readable than using the
10 /// `.cloned()` adapter.
11 ///
12 /// **Known problems:** None.
13 ///
14 /// **Example:**
15 /// ```rust
16 /// x.map(|e| e.clone());
17 /// ```
18 declare_lint! {
19     pub MAP_CLONE,
20     Warn,
21     "using `.map(|x| x.clone())` to clone an iterator or option's contents"
22 }
23
24 #[derive(Copy, Clone)]
25 pub struct Pass;
26
27 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
28     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
29         // call to .map()
30         if let ExprMethodCall(name, _, ref args) = expr.node {
31             if &*name.node.as_str() == "map" && args.len() == 2 {
32                 match args[1].node {
33                     ExprClosure(_, ref decl, closure_eid, _) => {
34                         let body = cx.tcx.map.body(closure_eid);
35                         let closure_expr = remove_blocks(&body.value);
36                         if_let_chain! {[
37                             // nothing special in the argument, besides reference bindings
38                             // (e.g. .map(|&x| x) )
39                             let Some(first_arg) = iter_input_pats(decl, body).next(),
40                             let Some(arg_ident) = get_arg_name(&first_arg.pat),
41                             // the method is being called on a known type (option or iterator)
42                             let Some(type_name) = get_type_name(cx, expr, &args[0])
43                         ], {
44                             // look for derefs, for .map(|x| *x)
45                             if only_derefs(cx, &*closure_expr, arg_ident) &&
46                                 // .cloned() only removes one level of indirection, don't lint on more
47                                 walk_ptrs_ty_depth(cx.tables.pat_ty(&first_arg.pat)).1 == 1
48                             {
49                                 span_help_and_lint(cx, MAP_CLONE, expr.span, &format!(
50                                     "you seem to be using .map() to clone the contents of an {}, consider \
51                                     using `.cloned()`", type_name),
52                                     &format!("try\n{}.cloned()", snippet(cx, args[0].span, "..")));
53                             }
54                             // explicit clone() calls ( .map(|x| x.clone()) )
55                             else if let ExprMethodCall(clone_call, _, ref clone_args) = closure_expr.node {
56                                 if &*clone_call.node.as_str() == "clone" &&
57                                     clone_args.len() == 1 &&
58                                     match_trait_method(cx, closure_expr, &paths::CLONE_TRAIT) &&
59                                     expr_eq_name(&clone_args[0], arg_ident)
60                                 {
61                                     span_help_and_lint(cx, MAP_CLONE, expr.span, &format!(
62                                         "you seem to be using .map() to clone the contents of an {}, consider \
63                                         using `.cloned()`", type_name),
64                                         &format!("try\n{}.cloned()", snippet(cx, args[0].span, "..")));
65                                 }
66                             }
67                         }}
68                     },
69                     ExprPath(ref path) => {
70                         if match_path(path, &paths::CLONE) {
71                             let type_name = get_type_name(cx, expr, &args[0]).unwrap_or("_");
72                             span_help_and_lint(cx,
73                                                MAP_CLONE,
74                                                expr.span,
75                                                &format!("you seem to be using .map() to clone the contents of an \
76                                                          {}, consider using `.cloned()`",
77                                                         type_name),
78                                                &format!("try\n{}.cloned()", snippet(cx, args[0].span, "..")));
79                         }
80                     },
81                     _ => (),
82                 }
83             }
84         }
85     }
86 }
87
88 fn expr_eq_name(expr: &Expr, id: ast::Name) -> bool {
89     match expr.node {
90         ExprPath(QPath::Resolved(None, ref path)) => {
91             let arg_segment = [PathSegment {
92                                    name: id,
93                                    parameters: PathParameters::none(),
94                                }];
95             !path.is_global() && path.segments[..] == arg_segment
96         },
97         _ => false,
98     }
99 }
100
101 fn get_type_name(cx: &LateContext, expr: &Expr, arg: &Expr) -> Option<&'static str> {
102     if match_trait_method(cx, expr, &paths::ITERATOR) {
103         Some("iterator")
104     } else if match_type(cx, walk_ptrs_ty(cx.tables.expr_ty(arg)), &paths::OPTION) {
105         Some("Option")
106     } else {
107         None
108     }
109 }
110
111 fn get_arg_name(pat: &Pat) -> Option<ast::Name> {
112     match pat.node {
113         PatKind::Binding(_, _, name, None) => Some(name.node),
114         PatKind::Ref(ref subpat, _) => get_arg_name(subpat),
115         _ => None,
116     }
117 }
118
119 fn only_derefs(cx: &LateContext, expr: &Expr, id: ast::Name) -> bool {
120     match expr.node {
121         ExprUnary(UnDeref, ref subexpr) if !is_adjusted(cx, subexpr) => only_derefs(cx, subexpr, id),
122         _ => expr_eq_name(expr, id),
123     }
124 }
125
126 impl LintPass for Pass {
127     fn get_lints(&self) -> LintArray {
128         lint_array!(MAP_CLONE)
129     }
130 }