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};
6 /// **What it does:** This lint checks for mapping clone() over an iterator.
8 /// **Why is this bad?** It makes the code less readable.
10 /// **Known problems:** None
12 /// **Example:** `x.map(|e| e.clone());`
15 "using `.map(|x| x.clone())` to clone an iterator or option's contents (recommends \
19 #[derive(Copy, Clone)]
20 pub struct MapClonePass;
22 impl LateLintPass for MapClonePass {
23 fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
25 if let ExprMethodCall(name, _, ref args) = expr.node {
26 if name.node.as_str() == "map" && args.len() == 2 {
28 ExprClosure(_, ref decl, ref blk, _) => {
31 // just one expression in the closure
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])
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
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, "..")));
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)
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, "..")));
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,
72 &format!("you seem to be using .map() to clone the contents of an \
73 {}, consider using `.cloned()`",
75 &format!("try\n{}.cloned()", snippet(cx, args[0].span, "..")));
85 fn expr_eq_ident(expr: &Expr, id: Ident) -> bool {
87 ExprPath(None, ref path) => {
88 let arg_segment = [PathSegment {
90 parameters: PathParameters::none(),
92 !path.global && path.segments[..] == arg_segment
98 fn get_type_name(cx: &LateContext, expr: &Expr, arg: &Expr) -> Option<&'static str> {
99 if match_trait_method(cx, expr, &paths::ITERATOR) {
101 } else if match_type(cx, walk_ptrs_ty(cx.tcx.expr_ty(arg)), &paths::OPTION) {
108 fn get_arg_name(pat: &Pat) -> Option<Ident> {
110 PatKind::Ident(_, ident, None) => Some(ident.node),
111 PatKind::Ref(ref subpat, _) => get_arg_name(subpat),
116 fn only_derefs(cx: &LateContext, expr: &Expr, id: Ident) -> bool {
118 ExprUnary(UnDeref, ref subexpr) if !is_adjusted(cx, subexpr) => only_derefs(cx, subexpr, id),
119 _ => expr_eq_ident(expr, id),
123 impl LintPass for MapClonePass {
124 fn get_lints(&self) -> LintArray {
125 lint_array!(MAP_CLONE)