Open JonathanWoollett-Light opened 1 year ago
I encountered a similar problem and have created a minimal reproducible example for my case:
#[derive(Clone)]
struct A {
a: u32,
b: u32,
}
fn main(){
let a = A { a: 42, b: 0 };
let mut aa = a.clone();
aa.b = 1024;
dbg!(&a.a);
}
Tools -> Clippy
Checking playground v0.0.1 (/playground)
warning: redundant clone
--> src/main.rs:10:19
|
10 | let mut aa = a.clone();
| ^^^^^^^^ help: remove this
|
note: cloned value is neither consumed nor mutated
--> src/main.rs:10:18
|
10 | let mut aa = a.clone();
| ^^^^^^^^^
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone
= note: `#[warn(clippy::redundant_clone)]` on by default
warning: `playground` (bin "playground") generated 1 warning (run `cargo clippy --fix --bin "playground"` to apply 1 suggestion)
Finished dev [unoptimized + debuginfo] target(s) in 0.98s
Thanks for the MRE @whfuyn ! At least in this example, it looks like the way clippy checks if a clone is mutated does not understand all mutating uses:
This only considers either moving or borrowing mutably, but no direct stores (like in the example aa.b = 1024
). So this lints, but this does not. Seems like we could fix this one by changing it to this:
if matches!(
ctx,
PlaceContext::NonMutatingUse(NonMutatingUseContext::Move)
- | PlaceContext::MutatingUse(MutatingUseContext::Borrow)
+ | PlaceContext::MutatingUse(MutatingUseContext::Borrow | MutatingUseContext::Store)
) {
I don't think this is the same issue as in the OP though. That one seems to trigger even without any mutation at all:
#![warn(clippy::redundant_clone)]
fn main() {
let a = String::new();
{
let _a2 = a.clone();
}
{
let _a3 = a;
}
}
Summary
The following code requires that
let mut state = orig_state.clone();
is cloned.Without it an error is emitted:
With it, clippy emits a warning:
See: https://github.com/firecracker-microvm/firecracker/blob/main/src/vmm/src/vstate/vcpu/x86_64.rs#L908:L924
Lint Name
clippy::redundant_clone
Version
Additional Labels
No response