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