Skip to content

Commit 93e204a

Browse files
committed
Fix map_clone with deref and clone
1 parent 0d2f1ae commit 93e204a

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

clippy_lints/src/map_clone.rs

+9-5
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+
let obj_ty = cx.typeck_results().expr_ty(obj);
86+
then {
8387
if let ty::Ref(_, ty, _) = obj_ty.kind() {
8488
let copy = is_copy(cx, ty);
8589
lint(cx, e.span, args[0].span, copy);

tests/ui/map_clone.fixed

+7
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,11 @@ fn main() {
4444
let v = vec![&mut d];
4545
let _: Vec<u32> = v.into_iter().map(|&mut x| x).collect();
4646
}
47+
48+
// Issue #6239 deref coercion and clone deref
49+
{
50+
use std::cell::RefCell;
51+
52+
let _ = Some(RefCell::new(String::new()).borrow()).map(|s| s.clone());
53+
}
4754
}

tests/ui/map_clone.rs

+7
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,11 @@ fn main() {
4444
let v = vec![&mut d];
4545
let _: Vec<u32> = v.into_iter().map(|&mut x| x).collect();
4646
}
47+
48+
// Issue #6239 deref coercion and clone deref
49+
{
50+
use std::cell::RefCell;
51+
52+
let _ = Some(RefCell::new(String::new()).borrow()).map(|s| s.clone());
53+
}
4754
}

0 commit comments

Comments
 (0)