]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/map_clone.rs
Merge pull request #2920 from rust-lang-nursery/rustup
[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 crate::utils::{get_arg_ident, 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, SpanlessEq};
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.ident.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                         if_chain! {
40                             // nothing special in the argument, besides reference bindings
41                             // (e.g. .map(|&x| x) )
42                             if let Some(first_arg) = iter_input_pats(decl, body).next();
43                             if let Some(arg_ident) = get_arg_ident(&first_arg.pat);
44                             // the method is being called on a known type (option or iterator)
45                             if let Some(type_name) = get_type_name(cx, expr, &args[0]);
46                             then {
47                                 // We know that body.arguments is not empty at this point
48                                 let ty = cx.tables.pat_ty(&body.arguments[0].pat);
49                                 // look for derefs, for .map(|x| *x)
50                                 if only_derefs(cx, &*closure_expr, arg_ident) &&
51                                     // .cloned() only removes one level of indirection, don't lint on more
52                                     walk_ptrs_ty_depth(cx.tables.pat_ty(&first_arg.pat)).1 == 1
53                                 {
54                                     // the argument is not an &mut T
55                                     if let ty::TyRef(_, _, mutbl) = ty.sty {
56                                         if mutbl == MutImmutable {
57                                             span_help_and_lint(cx, MAP_CLONE, expr.span, &format!(
58                                                 "you seem to be using .map() to clone the contents of an {}, consider \
59                                                 using `.cloned()`", type_name),
60                                                 &format!("try\n{}.cloned()", snippet(cx, args[0].span, "..")));
61                                         }
62                                     }
63                                 }
64                                 // explicit clone() calls ( .map(|x| x.clone()) )
65                                 else if let ExprMethodCall(ref clone_call, _, ref clone_args) = closure_expr.node {
66                                     if clone_call.ident.name == "clone" &&
67                                         clone_args.len() == 1 &&
68                                         match_trait_method(cx, closure_expr, &paths::CLONE_TRAIT) &&
69                                         expr_eq_name(cx, &clone_args[0], arg_ident)
70                                     {
71                                         span_help_and_lint(cx, MAP_CLONE, expr.span, &format!(
72                                             "you seem to be using .map() to clone the contents of an {}, consider \
73                                             using `.cloned()`", type_name),
74                                             &format!("try\n{}.cloned()", snippet(cx, args[0].span, "..")));
75                                     }
76                                 }
77                             }
78                         }
79                     },
80                     ExprPath(ref path) => if match_qpath(path, &paths::CLONE) {
81                         let type_name = get_type_name(cx, expr, &args[0]).unwrap_or("_");
82                         span_help_and_lint(
83                             cx,
84                             MAP_CLONE,
85                             expr.span,
86                             &format!(
87                                 "you seem to be using .map() to clone the contents of an \
88                                  {}, consider using `.cloned()`",
89                                 type_name
90                             ),
91                             &format!("try\n{}.cloned()", snippet(cx, args[0].span, "..")),
92                         );
93                     },
94                     _ => (),
95                 }
96             }
97         }
98     }
99 }
100
101 fn expr_eq_name(cx: &LateContext, expr: &Expr, id: ast::Ident) -> bool {
102     match expr.node {
103         ExprPath(QPath::Resolved(None, ref path)) => {
104             let arg_segment = [
105                 PathSegment {
106                     ident: id,
107                     args: None,
108                     infer_types: true,
109                 },
110             ];
111             !path.is_global() && SpanlessEq::new(cx).eq_path_segments(&path.segments[..], &arg_segment)
112         },
113         _ => false,
114     }
115 }
116
117 fn get_type_name(cx: &LateContext, expr: &Expr, arg: &Expr) -> Option<&'static str> {
118     if match_trait_method(cx, expr, &paths::ITERATOR) {
119         Some("iterator")
120     } else if match_type(cx, walk_ptrs_ty(cx.tables.expr_ty(arg)), &paths::OPTION) {
121         Some("Option")
122     } else {
123         None
124     }
125 }
126
127 fn only_derefs(cx: &LateContext, expr: &Expr, id: ast::Ident) -> bool {
128     match expr.node {
129         ExprUnary(UnDeref, ref subexpr) if !is_adjusted(cx, subexpr) => only_derefs(cx, subexpr, id),
130         _ => expr_eq_name(cx, expr, id),
131     }
132 }
133
134 impl LintPass for Pass {
135     fn get_lints(&self) -> LintArray {
136         lint_array!(MAP_CLONE)
137     }
138 }