about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--clippy_lints/src/map_clone.rs14
-rw-r--r--tests/ui/map_clone.fixed7
-rw-r--r--tests/ui/map_clone.rs7
3 files changed, 23 insertions, 5 deletions
diff --git a/clippy_lints/src/map_clone.rs b/clippy_lints/src/map_clone.rs
index 9a00608ce39..3f34dd5112e 100644
--- a/clippy_lints/src/map_clone.rs
+++ b/clippy_lints/src/map_clone.rs
@@ -8,6 +8,7 @@ use rustc_hir as hir;
 use rustc_lint::{LateContext, LateLintPass};
 use rustc_middle::mir::Mutability;
 use rustc_middle::ty;
+use rustc_middle::ty::adjustment::Adjust;
 use rustc_session::{declare_lint_pass, declare_tool_lint};
 use rustc_span::symbol::Ident;
 use rustc_span::{sym, Span};
@@ -75,11 +76,14 @@ impl<'tcx> LateLintPass<'tcx> for MapClone {
                                     }
                                 }
                             },
-                            hir::ExprKind::MethodCall(ref method, _, ref obj, _) => {
-                                if ident_eq(name, &obj[0]) && method.ident.as_str() == "clone"
-                                    && match_trait_method(cx, closure_expr, &paths::CLONE_TRAIT) {
-
-                                    let obj_ty = cx.typeck_results().expr_ty(&obj[0]);
+                            hir::ExprKind::MethodCall(ref method, _, [obj], _) => if_chain! {
+                                if ident_eq(name, obj) && method.ident.name == sym::clone;
+                                if match_trait_method(cx, closure_expr, &paths::CLONE_TRAIT);
+                                // no autoderefs
+                                if !cx.typeck_results().expr_adjustments(obj).iter()
+                                    .any(|a| matches!(a.kind, Adjust::Deref(Some(..))));
+                                then {
+                                    let obj_ty = cx.typeck_results().expr_ty(obj);
                                     if let ty::Ref(_, ty, mutability) = obj_ty.kind() {
                                         if matches!(mutability, Mutability::Not) {
                                             let copy = is_copy(cx, ty);
diff --git a/tests/ui/map_clone.fixed b/tests/ui/map_clone.fixed
index 6e3a8e67e81..504ae281fe7 100644
--- a/tests/ui/map_clone.fixed
+++ b/tests/ui/map_clone.fixed
@@ -52,4 +52,11 @@ fn main() {
         let items = vec![&mut aa, &mut bb];
         let _: Vec<_> = items.into_iter().map(|x| x.clone()).collect();
     }
+
+    // Issue #6239 deref coercion and clone deref
+    {
+        use std::cell::RefCell;
+
+        let _ = Some(RefCell::new(String::new()).borrow()).map(|s| s.clone());
+    }
 }
diff --git a/tests/ui/map_clone.rs b/tests/ui/map_clone.rs
index 6fd395710d4..9348e6bae7a 100644
--- a/tests/ui/map_clone.rs
+++ b/tests/ui/map_clone.rs
@@ -52,4 +52,11 @@ fn main() {
         let items = vec![&mut aa, &mut bb];
         let _: Vec<_> = items.into_iter().map(|x| x.clone()).collect();
     }
+
+    // Issue #6239 deref coercion and clone deref
+    {
+        use std::cell::RefCell;
+
+        let _ = Some(RefCell::new(String::new()).borrow()).map(|s| s.clone());
+    }
 }