From b15b977d76cc5d8a2d8a9f9c592c2633e85a454b Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Sun, 22 Dec 2019 11:26:51 +0200 Subject: [PATCH] Fix `map_clone` false positive Don't lint when the item type is not a reference. `copied` only applies to references. --- clippy_lints/src/map_clone.rs | 6 ++++-- tests/ui/map_clone.fixed | 10 ++++++++++ tests/ui/map_clone.rs | 10 ++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/map_clone.rs b/clippy_lints/src/map_clone.rs index 1f3954d569a2..7931fabf8d71 100644 --- a/clippy_lints/src/map_clone.rs +++ b/clippy_lints/src/map_clone.rs @@ -69,8 +69,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapClone { hir::PatKind::Binding(hir::BindingAnnotation::Unannotated, .., name, None) => { match closure_expr.kind { hir::ExprKind::Unary(hir::UnOp::UnDeref, ref inner) => { - if ident_eq(name, inner) && !cx.tables.expr_ty(inner).is_box() { - lint(cx, e.span, args[0].span, true); + if ident_eq(name, inner) { + if let ty::Ref(..) = cx.tables.expr_ty(inner).kind { + lint(cx, e.span, args[0].span, true); + } } }, hir::ExprKind::MethodCall(ref method, _, ref obj) => { diff --git a/tests/ui/map_clone.fixed b/tests/ui/map_clone.fixed index 2029c81d0d5a..5e6b3fad41ce 100644 --- a/tests/ui/map_clone.fixed +++ b/tests/ui/map_clone.fixed @@ -23,4 +23,14 @@ fn main() { // Issue #498 let _ = std::env::args(); + + // Issue #4824 item types that aren't references + { + use std::rc::Rc; + + let o: Option> = Some(Rc::new(0_u32)); + let _: Option = o.map(|x| *x); + let v: Vec> = vec![Rc::new(0_u32)]; + let _: Vec = v.into_iter().map(|x| *x).collect(); + } } diff --git a/tests/ui/map_clone.rs b/tests/ui/map_clone.rs index 495c18f311f5..2fe078b27526 100644 --- a/tests/ui/map_clone.rs +++ b/tests/ui/map_clone.rs @@ -23,4 +23,14 @@ fn main() { // Issue #498 let _ = std::env::args().map(|v| v.clone()); + + // Issue #4824 item types that aren't references + { + use std::rc::Rc; + + let o: Option> = Some(Rc::new(0_u32)); + let _: Option = o.map(|x| *x); + let v: Vec> = vec![Rc::new(0_u32)]; + let _: Vec = v.into_iter().map(|x| *x).collect(); + } }