Skip to content

Commit 96d5f45

Browse files
committed
Auto merge of #6301 - alex-700:fix-map-clone, r=matthiaskrgr
do not trigger map_clone in the case of &mut fixes #6299 changelog: do not trigger map_clone in the case of &mut
2 parents c015622 + 5f57608 commit 96d5f45

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

clippy_lints/src/map_clone.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ impl<'tcx> LateLintPass<'tcx> for MapClone {
8080
&& match_trait_method(cx, closure_expr, &paths::CLONE_TRAIT) {
8181

8282
let obj_ty = cx.typeck_results().expr_ty(&obj[0]);
83-
if let ty::Ref(_, ty, _) = obj_ty.kind() {
84-
let copy = is_copy(cx, ty);
85-
lint(cx, e.span, args[0].span, copy);
83+
if let ty::Ref(_, ty, mutability) = obj_ty.kind() {
84+
if matches!(mutability, Mutability::Not) {
85+
let copy = is_copy(cx, ty);
86+
lint(cx, e.span, args[0].span, copy);
87+
}
8688
} else {
8789
lint_needless_cloning(cx, e.span, args[0].span);
8890
}

tests/ui/map_clone.fixed

+8
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,12 @@ fn main() {
4444
let v = vec![&mut d];
4545
let _: Vec<u32> = v.into_iter().map(|&mut x| x).collect();
4646
}
47+
48+
// Issue #6299
49+
{
50+
let mut aa = 5;
51+
let mut bb = 3;
52+
let items = vec![&mut aa, &mut bb];
53+
let _: Vec<_> = items.into_iter().map(|x| x.clone()).collect();
54+
}
4755
}

tests/ui/map_clone.rs

+8
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,12 @@ fn main() {
4444
let v = vec![&mut d];
4545
let _: Vec<u32> = v.into_iter().map(|&mut x| x).collect();
4646
}
47+
48+
// Issue #6299
49+
{
50+
let mut aa = 5;
51+
let mut bb = 3;
52+
let items = vec![&mut aa, &mut bb];
53+
let _: Vec<_> = items.into_iter().map(|x| x.clone()).collect();
54+
}
4755
}

0 commit comments

Comments
 (0)