1 use crate::utils::paths;
3 in_macro, is_copy, 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_lint_pass, declare_tool_lint};
10 use rustc_errors::Applicability;
11 use syntax::ast::Ident;
12 use syntax::source_map::Span;
14 declare_clippy_lint! {
15 /// **What it does:** Checks for usage of `iterator.map(|x| x.clone())` and suggests
16 /// `iterator.cloned()` instead
18 /// **Why is this bad?** Readability, this can be written more concisely
20 /// **Known problems:** None
25 /// let x = vec![42, 43];
27 /// let z = y.map(|i| *i);
30 /// The correct use would be:
33 /// let x = vec![42, 43];
35 /// let z = y.cloned();
39 "using `iterator.map(|x| x.clone())`, or dereferencing closures for `Copy` types"
42 declare_lint_pass!(MapClone => [MAP_CLONE]);
44 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapClone {
45 fn check_expr(&mut self, cx: &LateContext<'_, '_>, e: &hir::Expr) {
51 if let hir::ExprKind::MethodCall(ref method, _, ref args) = e.node;
53 if method.ident.as_str() == "map";
54 let ty = cx.tables.expr_ty(&args[0]);
55 let is_option = match_type(cx, ty, &paths::OPTION);
56 if is_option || match_trait_method(cx, e, &paths::ITERATOR);
57 if let hir::ExprKind::Closure(_, _, body_id, _, _) = args[1].node;
58 let closure_body = cx.tcx.hir().body(body_id);
59 let closure_expr = remove_blocks(&closure_body.value);
61 match closure_body.arguments[0].pat.node {
62 hir::PatKind::Ref(ref inner, _) => if let hir::PatKind::Binding(
63 hir::BindingAnnotation::Unannotated, .., name, None
65 if ident_eq(name, closure_expr) {
66 // FIXME When Iterator::copied() stabilizes we can remove is_option
67 // from here and the other lint() calls
68 lint(cx, e.span, args[0].span, is_option);
71 hir::PatKind::Binding(hir::BindingAnnotation::Unannotated, .., name, None) => {
72 match closure_expr.node {
73 hir::ExprKind::Unary(hir::UnOp::UnDeref, ref inner) => {
74 if ident_eq(name, inner) && !cx.tables.expr_ty(inner).is_box() {
75 lint(cx, e.span, args[0].span, is_option);
78 hir::ExprKind::MethodCall(ref method, _, ref obj) => {
79 if ident_eq(name, &obj[0]) && method.ident.as_str() == "clone"
80 && match_trait_method(cx, closure_expr, &paths::CLONE_TRAIT) {
82 let obj_ty = cx.tables.expr_ty(&obj[0]);
83 if let ty::Ref(_, ty, _) = obj_ty.sty {
84 let copy = is_copy(cx, ty);
85 lint(cx, e.span, args[0].span, is_option && copy);
87 lint_needless_cloning(cx, e.span, args[0].span);
101 fn ident_eq(name: Ident, path: &hir::Expr) -> bool {
102 if let hir::ExprKind::Path(hir::QPath::Resolved(None, ref path)) = path.node {
103 path.segments.len() == 1 && path.segments[0].ident == name
109 fn lint_needless_cloning(cx: &LateContext<'_, '_>, root: Span, receiver: Span) {
113 root.trim_start(receiver).unwrap(),
114 "You are needlessly cloning iterator elements",
115 "Remove the map call",
117 Applicability::MachineApplicable,
121 fn lint(cx: &LateContext<'_, '_>, replace: Span, root: Span, copied: bool) {
122 let mut applicability = Applicability::MachineApplicable;
128 "You are using an explicit closure for copying elements",
129 "Consider calling the dedicated `copied` method",
132 snippet_with_applicability(cx, root, "..", &mut applicability)
141 "You are using an explicit closure for cloning elements",
142 "Consider calling the dedicated `cloned` method",
145 snippet_with_applicability(cx, root, "..", &mut applicability)