|
| 1 | +use rustc::lint::*; |
| 2 | +use rustc_front::hir::*; |
| 3 | +use syntax::ast::Ident; |
| 4 | +use utils::OPTION_PATH; |
| 5 | +use utils::{is_adjusted, match_trait_method, match_type, snippet, span_help_and_lint}; |
| 6 | +use utils::{walk_ptrs_ty, walk_ptrs_ty_depth}; |
| 7 | + |
| 8 | +declare_lint!(pub MAP_CLONE, Warn, |
| 9 | + "using `.map(|x| x.clone())` to clone an iterator or option's contents (recommends \ |
| 10 | + `.cloned()` instead)"); |
| 11 | + |
| 12 | +#[derive(Copy, Clone)] |
| 13 | +pub struct MapClonePass; |
| 14 | + |
| 15 | +impl LateLintPass for MapClonePass { |
| 16 | + fn check_expr(&mut self, cx: &LateContext, expr: &Expr) { |
| 17 | + if_let_chain! { |
| 18 | + [ |
| 19 | + // call to .map() |
| 20 | + let ExprMethodCall(name, _, ref args) = expr.node, |
| 21 | + name.node.as_str() == "map" && args.len() == 2, |
| 22 | + let ExprClosure(_, ref decl, ref blk) = args[1].node, |
| 23 | + // just one expression in the closure |
| 24 | + blk.stmts.is_empty(), |
| 25 | + let Some(ref closure_expr) = blk.expr, |
| 26 | + // nothing special in the argument, besides reference bindings |
| 27 | + // (e.g. .map(|&x| x) ) |
| 28 | + let Some(arg_ident) = get_arg_name(&*decl.inputs[0].pat), |
| 29 | + // the method is being called on a known type (option or iterator) |
| 30 | + let Some(type_name) = get_type_name(cx, expr, &args[0]) |
| 31 | + ], { |
| 32 | + // look for derefs, for .map(|x| *x) |
| 33 | + if only_derefs(cx, &*closure_expr, arg_ident) && |
| 34 | + // .cloned() only removes one level of indirection, don't lint on more |
| 35 | + walk_ptrs_ty_depth(cx.tcx.pat_ty(&*decl.inputs[0].pat)).1 == 1 |
| 36 | + { |
| 37 | + span_help_and_lint(cx, MAP_CLONE, expr.span, &format!( |
| 38 | + "you seem to be using .map() to clone the contents of an {}, consider \ |
| 39 | + using `.cloned()`", type_name), |
| 40 | + &format!("try\n{}.cloned()", snippet(cx, args[0].span, ".."))); |
| 41 | + } |
| 42 | + // explicit clone() calls ( .map(|x| x.clone()) ) |
| 43 | + else if let ExprMethodCall(clone_call, _, ref clone_args) = closure_expr.node { |
| 44 | + if clone_call.node.as_str() == "clone" && |
| 45 | + clone_args.len() == 1 && |
| 46 | + match_trait_method(cx, closure_expr, &["core", "clone", "Clone"]) && |
| 47 | + expr_eq_ident(&clone_args[0], arg_ident) |
| 48 | + { |
| 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, ".."))); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +fn expr_eq_ident(expr: &Expr, id: Ident) -> bool { |
| 61 | + match expr.node { |
| 62 | + ExprPath(None, ref path) => { |
| 63 | + let arg_segment = [PathSegment { identifier: id, parameters: PathParameters::none() }]; |
| 64 | + !path.global && path.segments == arg_segment |
| 65 | + }, |
| 66 | + _ => false, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +fn get_type_name(cx: &LateContext, expr: &Expr, arg: &Expr) -> Option<&'static str> { |
| 71 | + if match_trait_method(cx, expr, &["core", "iter", "Iterator"]) { |
| 72 | + Some("iterator") |
| 73 | + } else if match_type(cx, walk_ptrs_ty(cx.tcx.expr_ty(arg)), &OPTION_PATH) { |
| 74 | + Some("Option") |
| 75 | + } else { |
| 76 | + None |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +fn get_arg_name(pat: &Pat) -> Option<Ident> { |
| 81 | + match pat.node { |
| 82 | + PatIdent(_, ident, None) => Some(ident.node), |
| 83 | + PatRegion(ref subpat, _) => get_arg_name(subpat), |
| 84 | + _ => None, |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +fn only_derefs(cx: &LateContext, expr: &Expr, id: Ident) -> bool { |
| 89 | + match expr.node { |
| 90 | + ExprUnary(UnDeref, ref subexpr) if !is_adjusted(cx, subexpr) => { |
| 91 | + only_derefs(cx, subexpr, id) |
| 92 | + }, |
| 93 | + _ => expr_eq_ident(expr, id), |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +impl LintPass for MapClonePass { |
| 98 | + fn get_lints(&self) -> LintArray { |
| 99 | + lint_array!(MAP_CLONE) |
| 100 | + } |
| 101 | +} |
0 commit comments