Skip to content

Commit d9ad338

Browse files
committed
Use visit_place
1 parent aca64b8 commit d9ad338

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

clippy_lints/src/redundant_clone.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -375,14 +375,16 @@ impl<'tcx> mir::visit::Visitor<'tcx> for LocalUseVisitor {
375375
);
376376
}
377377

378-
fn visit_local(&mut self, local: &mir::Local, ctx: PlaceContext, _: mir::Location) {
379-
if *local == self.used.0
378+
fn visit_place(&mut self, place: &mir::Place<'tcx>, ctx: PlaceContext, _: mir::Location) {
379+
let local = place.local;
380+
381+
if local == self.used.0
380382
&& !matches!(ctx, PlaceContext::MutatingUse(MutatingUseContext::Drop) | PlaceContext::NonUse(_))
381383
{
382384
self.used.1 = true;
383385
}
384386

385-
if *local == self.consumed_or_mutated.0 {
387+
if local == self.consumed_or_mutated.0 {
386388
match ctx {
387389
PlaceContext::NonMutatingUse(NonMutatingUseContext::Move)
388390
| PlaceContext::MutatingUse(MutatingUseContext::Borrow) => {

tests/ui/redundant_clone.fixed

+9
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,13 @@ fn not_consumed() {
150150
s.clone().push_str("foo"); // OK, removing this `clone()` will change the behavior.
151151
s.push_str("bar");
152152
assert_eq!(s, "bar");
153+
154+
let t = Some(s);
155+
// OK
156+
if let Some(x) = t.clone() {
157+
println!("{}", x);
158+
}
159+
if let Some(x) = t {
160+
println!("{}", x);
161+
}
153162
}

tests/ui/redundant_clone.rs

+9
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,13 @@ fn not_consumed() {
150150
s.clone().push_str("foo"); // OK, removing this `clone()` will change the behavior.
151151
s.push_str("bar");
152152
assert_eq!(s, "bar");
153+
154+
let t = Some(s);
155+
// OK
156+
if let Some(x) = t.clone() {
157+
println!("{}", x);
158+
}
159+
if let Some(x) = t {
160+
println!("{}", x);
161+
}
153162
}

0 commit comments

Comments
 (0)