4 use utils::{is_adjusted, match_path, match_trait_method, match_type, remove_blocks, paths, snippet, span_help_and_lint,
5 walk_ptrs_ty, walk_ptrs_ty_depth, iter_input_pats};
7 /// **What it does:** Checks for mapping `clone()` over an iterator.
9 /// **Why is this bad?** It makes the code less readable than using the
10 /// `.cloned()` adapter.
12 /// **Known problems:** None.
16 /// x.map(|e| e.clone());
21 "using `.map(|x| x.clone())` to clone an iterator or option's contents"
24 #[derive(Copy, Clone)]
27 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
28 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
30 if let ExprMethodCall(name, _, ref args) = expr.node {
31 if &*name.node.as_str() == "map" && args.len() == 2 {
33 ExprClosure(_, ref decl, closure_eid, _) => {
34 let body = cx.tcx.map.body(closure_eid);
35 let closure_expr = remove_blocks(&body.value);
37 // nothing special in the argument, besides reference bindings
38 // (e.g. .map(|&x| x) )
39 let Some(first_arg) = iter_input_pats(decl, body).next(),
40 let Some(arg_ident) = get_arg_name(&first_arg.pat),
41 // the method is being called on a known type (option or iterator)
42 let Some(type_name) = get_type_name(cx, expr, &args[0])
44 // look for derefs, for .map(|x| *x)
45 if only_derefs(cx, &*closure_expr, arg_ident) &&
46 // .cloned() only removes one level of indirection, don't lint on more
47 walk_ptrs_ty_depth(cx.tables.pat_ty(&first_arg.pat)).1 == 1
49 span_help_and_lint(cx, MAP_CLONE, expr.span, &format!(
50 "you seem to be using .map() to clone the contents of an {}, consider \
51 using `.cloned()`", type_name),
52 &format!("try\n{}.cloned()", snippet(cx, args[0].span, "..")));
54 // explicit clone() calls ( .map(|x| x.clone()) )
55 else if let ExprMethodCall(clone_call, _, ref clone_args) = closure_expr.node {
56 if &*clone_call.node.as_str() == "clone" &&
57 clone_args.len() == 1 &&
58 match_trait_method(cx, closure_expr, &paths::CLONE_TRAIT) &&
59 expr_eq_name(&clone_args[0], arg_ident)
61 span_help_and_lint(cx, MAP_CLONE, expr.span, &format!(
62 "you seem to be using .map() to clone the contents of an {}, consider \
63 using `.cloned()`", type_name),
64 &format!("try\n{}.cloned()", snippet(cx, args[0].span, "..")));
69 ExprPath(ref path) => {
70 if match_path(path, &paths::CLONE) {
71 let type_name = get_type_name(cx, expr, &args[0]).unwrap_or("_");
72 span_help_and_lint(cx,
75 &format!("you seem to be using .map() to clone the contents of an \
76 {}, consider using `.cloned()`",
78 &format!("try\n{}.cloned()", snippet(cx, args[0].span, "..")));
88 fn expr_eq_name(expr: &Expr, id: ast::Name) -> bool {
90 ExprPath(QPath::Resolved(None, ref path)) => {
91 let arg_segment = [PathSegment {
93 parameters: PathParameters::none(),
95 !path.is_global() && path.segments[..] == arg_segment
101 fn get_type_name(cx: &LateContext, expr: &Expr, arg: &Expr) -> Option<&'static str> {
102 if match_trait_method(cx, expr, &paths::ITERATOR) {
104 } else if match_type(cx, walk_ptrs_ty(cx.tables.expr_ty(arg)), &paths::OPTION) {
111 fn get_arg_name(pat: &Pat) -> Option<ast::Name> {
113 PatKind::Binding(_, _, name, None) => Some(name.node),
114 PatKind::Ref(ref subpat, _) => get_arg_name(subpat),
119 fn only_derefs(cx: &LateContext, expr: &Expr, id: ast::Name) -> bool {
121 ExprUnary(UnDeref, ref subexpr) if !is_adjusted(cx, subexpr) => only_derefs(cx, subexpr, id),
122 _ => expr_eq_name(expr, id),
126 impl LintPass for Pass {
127 fn get_lints(&self) -> LintArray {
128 lint_array!(MAP_CLONE)