Skip to content

Commit 7690944

Browse files
committed
Fix map_clone with deref and clone
1 parent 040d0ca commit 7690944

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

clippy_lints/src/map_clone.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_hir as hir;
88
use rustc_lint::{LateContext, LateLintPass};
99
use rustc_middle::mir::Mutability;
1010
use rustc_middle::ty;
11+
use rustc_middle::ty::adjustment::Adjust;
1112
use rustc_session::{declare_lint_pass, declare_tool_lint};
1213
use rustc_span::symbol::Ident;
1314
use rustc_span::{sym, Span};
@@ -75,11 +76,14 @@ impl<'tcx> LateLintPass<'tcx> for MapClone {
7576
}
7677
}
7778
},
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) {
81-
82-
let obj_ty = cx.typeck_results().expr_ty(&obj[0]);
79+
hir::ExprKind::MethodCall(ref method, _, [obj], _) => if_chain! {
80+
if ident_eq(name, obj) && method.ident.name == sym::clone;
81+
if match_trait_method(cx, closure_expr, &paths::CLONE_TRAIT);
82+
// no autoderefs
83+
if !cx.typeck_results().expr_adjustments(obj).iter()
84+
.any(|a| matches!(a.kind, Adjust::Deref(Some(..))));
85+
then {
86+
let obj_ty = cx.typeck_results().expr_ty(obj);
8387
if let ty::Ref(_, ty, mutability) = obj_ty.kind() {
8488
if matches!(mutability, Mutability::Not) {
8589
let copy = is_copy(cx, ty);

tests/ui/map_clone.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,11 @@ fn main() {
5252
let items = vec![&mut aa, &mut bb];
5353
let _: Vec<_> = items.into_iter().map(|x| x.clone()).collect();
5454
}
55+
56+
// Issue #6239 deref coercion and clone deref
57+
{
58+
use std::cell::RefCell;
59+
60+
let _ = Some(RefCell::new(String::new()).borrow()).map(|s| s.clone());
61+
}
5562
}

tests/ui/map_clone.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,11 @@ fn main() {
5252
let items = vec![&mut aa, &mut bb];
5353
let _: Vec<_> = items.into_iter().map(|x| x.clone()).collect();
5454
}
55+
56+
// Issue #6239 deref coercion and clone deref
57+
{
58+
use std::cell::RefCell;
59+
60+
let _ = Some(RefCell::new(String::new()).borrow()).map(|s| s.clone());
61+
}
5562
}

0 commit comments

Comments
 (0)