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