2 use rustc_front::hir::*;
3 use syntax::ast::Ident;
4 use utils::OPTION_PATH;
5 use utils::{match_trait_method, match_type, snippet, span_help_and_lint};
6 use utils::{walk_ptrs_ty, walk_ptrs_ty_depth};
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)");
12 #[derive(Copy, Clone)]
13 pub struct MapClonePass;
15 impl LateLintPass for MapClonePass {
16 fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
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
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])
32 // look for derefs, for .map(|x| *x)
33 if only_derefs(&*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
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, "..")));
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)
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, "..")));
60 fn expr_eq_ident(expr: &Expr, id: Ident) -> bool {
62 ExprPath(None, ref path) => {
63 let arg_segment = [PathSegment { identifier: id, parameters: PathParameters::none() }];
64 !path.global && path.segments == arg_segment
70 fn get_type_name(cx: &LateContext, expr: &Expr, arg: &Expr) -> Option<&'static str> {
71 if match_trait_method(cx, expr, &["core", "iter", "Iterator"]) {
73 } else if match_type(cx, walk_ptrs_ty(cx.tcx.expr_ty(arg)), &OPTION_PATH) {
80 fn get_arg_name(pat: &Pat) -> Option<Ident> {
82 PatIdent(_, ident, None) => Some(ident.node),
83 PatRegion(ref subpat, _) => get_arg_name(subpat),
88 fn only_derefs(expr: &Expr, id: Ident) -> bool {
89 if expr_eq_ident(expr, id) {
91 } else if let ExprUnary(UnDeref, ref subexpr) = expr.node {
92 only_derefs(subexpr, id)
98 impl LintPass for MapClonePass {
99 fn get_lints(&self) -> LintArray {
100 lint_array!(MAP_CLONE)