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