2 use rustc::{declare_lint, lint_array};
3 use if_chain::if_chain;
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};
10 /// **What it does:** Checks for mapping `clone()` over an iterator.
12 /// **Why is this bad?** It makes the code less readable than using the
13 /// `.cloned()` adapter.
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).
21 /// x.map(|e| e.clone());
23 declare_clippy_lint! {
26 "using `.map(|x| x.clone())` to clone an iterator or option's contents"
29 #[derive(Copy, Clone)]
32 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
33 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
35 if let ExprKind::MethodCall(ref method, _, ref args) = expr.node {
36 if method.ident.name == "map" && args.len() == 2 {
38 ExprKind::Closure(_, ref decl, closure_eid, _, _) => {
39 let body = cx.tcx.hir.body(closure_eid);
40 let closure_expr = remove_blocks(&body.value);
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]);
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
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, "..")));
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)
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, "..")));
82 ExprKind::Path(ref path) => if match_qpath(path, &paths::CLONE) {
83 let type_name = get_type_name(cx, expr, &args[0]).unwrap_or("_");
89 "you seem to be using .map() to clone the contents of an \
90 {}, consider using `.cloned()`",
93 &format!("try\n{}.cloned()", snippet(cx, args[0].span, "..")),
103 fn expr_eq_name(cx: &LateContext, expr: &Expr, id: ast::Ident) -> bool {
105 ExprKind::Path(QPath::Resolved(None, ref path)) => {
113 !path.is_global() && SpanlessEq::new(cx).eq_path_segments(&path.segments[..], &arg_segment)
119 fn get_type_name(cx: &LateContext, expr: &Expr, arg: &Expr) -> Option<&'static str> {
120 if match_trait_method(cx, expr, &paths::ITERATOR) {
122 } else if match_type(cx, walk_ptrs_ty(cx.tables.expr_ty(arg)), &paths::OPTION) {
129 fn only_derefs(cx: &LateContext, expr: &Expr, id: ast::Ident) -> bool {
131 ExprKind::Unary(UnDeref, ref subexpr) if !is_adjusted(cx, subexpr) => only_derefs(cx, subexpr, id),
132 _ => expr_eq_name(cx, expr, id),
136 impl LintPass for Pass {
137 fn get_lints(&self) -> LintArray {
138 lint_array!(MAP_CLONE)