Skip to content

Commit 64b79fb

Browse files
committed
Fix map_clone with deref and clone
1 parent c2cf40c commit 64b79fb

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

clippy_lints/src/map_clone.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::utils::paths;
22
use crate::utils::{
3-
is_copy, is_type_diagnostic_item, match_trait_method, remove_blocks, snippet_with_applicability, span_lint_and_sugg,
3+
implements_trait, is_copy, is_type_diagnostic_item, match_trait_method, remove_blocks, snippet_with_applicability,
4+
span_lint_and_sugg,
45
};
56
use if_chain::if_chain;
67
use rustc_errors::Applicability;
@@ -83,7 +84,9 @@ impl<'tcx> LateLintPass<'tcx> for MapClone {
8384
if let ty::Ref(_, ty, _) = obj_ty.kind() {
8485
let copy = is_copy(cx, ty);
8586
lint(cx, e.span, args[0].span, copy);
86-
} else {
87+
} else if cx.tcx.lang_items().clone_trait()
88+
.map_or(false, |clone_trait| implements_trait(cx, obj_ty, clone_trait, &[]))
89+
{
8790
lint_needless_cloning(cx, e.span, args[0].span);
8891
}
8992
}

tests/ui/map_clone.fixed

Lines changed: 7 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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)