Skip to content

Commit

Permalink
Fix map_clone with deref and clone
Browse files Browse the repository at this point in the history
  • Loading branch information
camsteffen committed Oct 30, 2020
1 parent c57d8ae commit be95f39
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
7 changes: 5 additions & 2 deletions clippy_lints/src/map_clone.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::utils::paths;
use crate::utils::{
is_copy, is_type_diagnostic_item, match_trait_method, remove_blocks, snippet_with_applicability, span_lint_and_sugg,
implements_trait, is_copy, is_type_diagnostic_item, match_trait_method, remove_blocks, snippet_with_applicability,
span_lint_and_sugg,
};
use if_chain::if_chain;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -83,7 +84,9 @@ impl<'tcx> LateLintPass<'tcx> for MapClone {
if let ty::Ref(_, ty, _) = obj_ty.kind() {
let copy = is_copy(cx, ty);
lint(cx, e.span, args[0].span, copy);
} else {
} else if cx.tcx.lang_items().clone_trait()
.map_or(false, |clone_trait| implements_trait(cx, obj_ty, clone_trait, &[]))
{
lint_needless_cloning(cx, e.span, args[0].span);
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/map_clone.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ fn main() {
let v = vec![&mut d];
let _: Vec<u32> = v.into_iter().map(|&mut x| x).collect();
}

// Issue #6239 deref coercion and clone deref
{
use std::cell::RefCell;

let _ = Some(RefCell::new(String::new()).borrow()).map(|s| s.clone());
}
}
7 changes: 7 additions & 0 deletions tests/ui/map_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ fn main() {
let v = vec![&mut d];
let _: Vec<u32> = v.into_iter().map(|&mut x| x).collect();
}

// Issue #6239 deref coercion and clone deref
{
use std::cell::RefCell;

let _ = Some(RefCell::new(String::new()).borrow()).map(|s| s.clone());
}
}

0 comments on commit be95f39

Please sign in to comment.