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};
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.ident.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);
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]);
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
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, "..")));
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)
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, "..")));
80 ExprPath(ref path) => if match_qpath(path, &paths::CLONE) {
81 let type_name = get_type_name(cx, expr, &args[0]).unwrap_or("_");
87 "you seem to be using .map() to clone the contents of an \
88 {}, consider using `.cloned()`",
91 &format!("try\n{}.cloned()", snippet(cx, args[0].span, "..")),
101 fn expr_eq_name(cx: &LateContext, expr: &Expr, id: ast::Ident) -> bool {
103 ExprPath(QPath::Resolved(None, ref path)) => {
111 !path.is_global() && SpanlessEq::new(cx).eq_path_segments(&path.segments[..], &arg_segment)
117 fn get_type_name(cx: &LateContext, expr: &Expr, arg: &Expr) -> Option<&'static str> {
118 if match_trait_method(cx, expr, &paths::ITERATOR) {
120 } else if match_type(cx, walk_ptrs_ty(cx.tables.expr_ty(arg)), &paths::OPTION) {
127 fn only_derefs(cx: &LateContext, expr: &Expr, id: ast::Ident) -> bool {
129 ExprUnary(UnDeref, ref subexpr) if !is_adjusted(cx, subexpr) => only_derefs(cx, subexpr, id),
130 _ => expr_eq_name(cx, expr, id),
134 impl LintPass for Pass {
135 fn get_lints(&self) -> LintArray {
136 lint_array!(MAP_CLONE)