Skip to content

Commit

Permalink
Fix map_clone false positive
Browse files Browse the repository at this point in the history
Don't lint when the item type is not a reference. `copied` only applies
to references.
  • Loading branch information
Michael Wright committed Dec 22, 2019
1 parent cfb3320 commit b15b977
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions clippy_lints/src/map_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapClone {
hir::PatKind::Binding(hir::BindingAnnotation::Unannotated, .., name, None) => {
match closure_expr.kind {
hir::ExprKind::Unary(hir::UnOp::UnDeref, ref inner) => {
if ident_eq(name, inner) && !cx.tables.expr_ty(inner).is_box() {
lint(cx, e.span, args[0].span, true);
if ident_eq(name, inner) {
if let ty::Ref(..) = cx.tables.expr_ty(inner).kind {
lint(cx, e.span, args[0].span, true);
}
}
},
hir::ExprKind::MethodCall(ref method, _, ref obj) => {
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/map_clone.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,14 @@ fn main() {

// Issue #498
let _ = std::env::args();

// Issue #4824 item types that aren't references
{
use std::rc::Rc;

let o: Option<Rc<u32>> = Some(Rc::new(0_u32));
let _: Option<u32> = o.map(|x| *x);
let v: Vec<Rc<u32>> = vec![Rc::new(0_u32)];
let _: Vec<u32> = v.into_iter().map(|x| *x).collect();
}
}
10 changes: 10 additions & 0 deletions tests/ui/map_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,14 @@ fn main() {

// Issue #498
let _ = std::env::args().map(|v| v.clone());

// Issue #4824 item types that aren't references
{
use std::rc::Rc;

let o: Option<Rc<u32>> = Some(Rc::new(0_u32));
let _: Option<u32> = o.map(|x| *x);
let v: Vec<Rc<u32>> = vec![Rc::new(0_u32)];
let _: Vec<u32> = v.into_iter().map(|x| *x).collect();
}
}

0 comments on commit b15b977

Please sign in to comment.