Skip to content

Commit

Permalink
Use visit_place
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkuu committed Mar 12, 2020
1 parent aca64b8 commit d9ad338
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
8 changes: 5 additions & 3 deletions clippy_lints/src/redundant_clone.rs
Expand Up @@ -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) => {
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/redundant_clone.fixed
Expand Up @@ -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);
}
}
9 changes: 9 additions & 0 deletions tests/ui/redundant_clone.rs
Expand Up @@ -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);
}
}

0 comments on commit d9ad338

Please sign in to comment.