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};
8 /// **What it does:** Checks for mapping `clone()` over an iterator.
10 /// **Why is this bad?** It makes the code less readable than using the
11 /// `.cloned()` adapter.
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).
19 /// x.map(|e| e.clone());
21 declare_clippy_lint! {
24 "using `.map(|x| x.clone())` to clone an iterator or option's contents"
27 #[derive(Copy, Clone)]
30 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
31 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
33 if let ExprMethodCall(ref method, _, ref args) = expr.node {
34 if method.name == "map" && args.len() == 2 {
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);
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]);
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
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, "..")));
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)
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, "..")));
79 ExprPath(ref path) => if match_qpath(path, &paths::CLONE) {
80 let type_name = get_type_name(cx, expr, &args[0]).unwrap_or("_");
86 "you seem to be using .map() to clone the contents of an \
87 {}, consider using `.cloned()`",
90 &format!("try\n{}.cloned()", snippet(cx, args[0].span, "..")),
100 fn expr_eq_name(expr: &Expr, id: ast::Name) -> bool {
102 ExprPath(QPath::Resolved(None, ref path)) => {
110 !path.is_global() && path.segments[..] == arg_segment
116 fn get_type_name(cx: &LateContext, expr: &Expr, arg: &Expr) -> Option<&'static str> {
117 if match_trait_method(cx, expr, &paths::ITERATOR) {
119 } else if match_type(cx, walk_ptrs_ty(cx.tables.expr_ty(arg)), &paths::OPTION) {
126 fn only_derefs(cx: &LateContext, expr: &Expr, id: ast::Name) -> bool {
128 ExprUnary(UnDeref, ref subexpr) if !is_adjusted(cx, subexpr) => only_derefs(cx, subexpr, id),
129 _ => expr_eq_name(expr, id),
133 impl LintPass for Pass {
134 fn get_lints(&self) -> LintArray {
135 lint_array!(MAP_CLONE)