1 use crate::utils::paths;
3 in_macro, match_trait_method, match_type, remove_blocks, snippet_with_applicability, span_lint_and_sugg,
5 use if_chain::if_chain;
7 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
9 use rustc::{declare_tool_lint, lint_array};
10 use rustc_errors::Applicability;
11 use syntax::ast::Ident;
12 use syntax::source_map::Span;
17 /// **What it does:** Checks for usage of `iterator.map(|x| x.clone())` and suggests
18 /// `iterator.cloned()` instead
20 /// **Why is this bad?** Readability, this can be written more concisely
22 /// **Known problems:** None
27 /// let x = vec![42, 43];
29 /// let z = y.map(|i| *i);
32 /// The correct use would be:
35 /// let x = vec![42, 43];
37 /// let z = y.cloned();
39 declare_clippy_lint! {
42 "using `iterator.map(|x| x.clone())`, or dereferencing closures for `Copy` types"
45 impl LintPass for Pass {
46 fn get_lints(&self) -> LintArray {
47 lint_array!(MAP_CLONE)
51 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
52 fn check_expr(&mut self, cx: &LateContext<'_, '_>, e: &hir::Expr) {
58 if let hir::ExprKind::MethodCall(ref method, _, ref args) = e.node;
60 if method.ident.as_str() == "map";
61 let ty = cx.tables.expr_ty(&args[0]);
62 if match_type(cx, ty, &paths::OPTION) || match_trait_method(cx, e, &paths::ITERATOR);
63 if let hir::ExprKind::Closure(_, _, body_id, _, _) = args[1].node;
64 let closure_body = cx.tcx.hir().body(body_id);
65 let closure_expr = remove_blocks(&closure_body.value);
67 match closure_body.arguments[0].pat.node {
68 hir::PatKind::Ref(ref inner, _) => if let hir::PatKind::Binding(
69 hir::BindingAnnotation::Unannotated, _, name, None
71 if ident_eq(name, closure_expr) {
72 lint(cx, e.span, args[0].span);
75 hir::PatKind::Binding(hir::BindingAnnotation::Unannotated, _, name, None) => {
76 match closure_expr.node {
77 hir::ExprKind::Unary(hir::UnOp::UnDeref, ref inner) => {
78 if ident_eq(name, inner) && !cx.tables.expr_ty(inner).is_box() {
79 lint(cx, e.span, args[0].span);
82 hir::ExprKind::MethodCall(ref method, _, ref obj) => {
83 if ident_eq(name, &obj[0]) && method.ident.as_str() == "clone"
84 && match_trait_method(cx, closure_expr, &paths::CLONE_TRAIT) {
86 let obj_ty = cx.tables.expr_ty(&obj[0]);
87 if let ty::Ref(..) = obj_ty.sty {
88 lint(cx, e.span, args[0].span);
90 lint_needless_cloning(cx, e.span, args[0].span);
104 fn ident_eq(name: Ident, path: &hir::Expr) -> bool {
105 if let hir::ExprKind::Path(hir::QPath::Resolved(None, ref path)) = path.node {
106 path.segments.len() == 1 && path.segments[0].ident == name
112 fn lint_needless_cloning(cx: &LateContext<'_, '_>, root: Span, receiver: Span) {
116 root.trim_start(receiver).unwrap(),
117 "You are needlessly cloning iterator elements",
118 "Remove the map call",
120 Applicability::MachineApplicable,
124 fn lint(cx: &LateContext<'_, '_>, replace: Span, root: Span) {
125 let mut applicability = Applicability::MachineApplicable;
130 "You are using an explicit closure for cloning elements",
131 "Consider calling the dedicated `cloned` method",
134 snippet_with_applicability(cx, root, "..", &mut applicability)