From d9ad33852c1e0179fc9b22ac2324ab18455879f1 Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Fri, 13 Mar 2020 02:06:47 +0900 Subject: [PATCH] Use visit_place --- clippy_lints/src/redundant_clone.rs | 8 +++++--- tests/ui/redundant_clone.fixed | 9 +++++++++ tests/ui/redundant_clone.rs | 9 +++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/redundant_clone.rs b/clippy_lints/src/redundant_clone.rs index 624fe227ca3d..b63bb371c4f3 100644 --- a/clippy_lints/src/redundant_clone.rs +++ b/clippy_lints/src/redundant_clone.rs @@ -375,14 +375,16 @@ impl<'tcx> mir::visit::Visitor<'tcx> for LocalUseVisitor { ); } - fn visit_local(&mut self, local: &mir::Local, ctx: PlaceContext, _: mir::Location) { - if *local == self.used.0 + fn visit_place(&mut self, place: &mir::Place<'tcx>, ctx: PlaceContext, _: mir::Location) { + let local = place.local; + + if local == self.used.0 && !matches!(ctx, PlaceContext::MutatingUse(MutatingUseContext::Drop) | PlaceContext::NonUse(_)) { self.used.1 = true; } - if *local == self.consumed_or_mutated.0 { + if local == self.consumed_or_mutated.0 { match ctx { PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) | PlaceContext::MutatingUse(MutatingUseContext::Borrow) => { diff --git a/tests/ui/redundant_clone.fixed b/tests/ui/redundant_clone.fixed index 17734b04aba4..54815603c6de 100644 --- a/tests/ui/redundant_clone.fixed +++ b/tests/ui/redundant_clone.fixed @@ -150,4 +150,13 @@ fn not_consumed() { s.clone().push_str("foo"); // OK, removing this `clone()` will change the behavior. s.push_str("bar"); assert_eq!(s, "bar"); + + let t = Some(s); + // OK + if let Some(x) = t.clone() { + println!("{}", x); + } + if let Some(x) = t { + println!("{}", x); + } } diff --git a/tests/ui/redundant_clone.rs b/tests/ui/redundant_clone.rs index aee6855eea94..a9b31183adc8 100644 --- a/tests/ui/redundant_clone.rs +++ b/tests/ui/redundant_clone.rs @@ -150,4 +150,13 @@ fn not_consumed() { s.clone().push_str("foo"); // OK, removing this `clone()` will change the behavior. s.push_str("bar"); assert_eq!(s, "bar"); + + let t = Some(s); + // OK + if let Some(x) = t.clone() { + println!("{}", x); + } + if let Some(x) = t { + println!("{}", x); + } }