rust-lang / rust-lang/rust-clippy
Warning on `redundant_clone` when required with scopes
Open
Nobody has claimed this yet.
C-bug
I-false-positive
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
The following code requires that let mut state = orig_state.clone(); is cloned.
{
// The frequency difference is within tolerance.
let mut state = orig_state.clone();
state.tsc_khz = Some(state.tsc_khz.unwrap() + (TSC_KHZ_TOL / 2.0).round() as u32);
assert!(!vcpu
.is_tsc_scaling_required(state.tsc_khz.unwrap())
.unwrap());
}
{
// The frequency difference is over the tolerance.
let mut state = orig_state;
state.tsc_khz = Some(state.tsc_khz.unwrap() + (TSC_KHZ_TOL * 2.0).round() as u32);
assert!(!vcpu
.is_tsc_scaling_required(state.tsc_khz.unwrap())
.unwrap());
}
Without it an error is emitted:
error[E0382]: use of moved value: `orig_state`
--> src/vmm/src/vstate/vcpu/x86_64.rs:919:29
|
906 | let orig_state = vcpu.save_state().unwrap();
| ---------- move occurs because `orig_state` has type `vstate::vcpu::x86_64::VcpuState`, which does not implement the `Copy` trait
...
910 | let mut state = orig_state;
| ---------- value moved here
...
919 | let mut state = orig_state;
| ^^^^^^^^^^ value used here after move
|
help: consider cloning the value if the performance cost is acceptable
|
910 | let mut state = orig_state.clone();
| ++++++++
For more information about this error, try `rustc --explain E0382`.
With it, clippy emits a warning:
warning: redundant clone
--> src/vmm/src/vstate/vcpu/x86_64.rs:910:39
|
910 | let mut state = orig_state.clone();
| ^^^^^^^^ help: remove this
|
note: cloned value is neither consumed nor mutated
--> src/vmm/src/vstate/vcpu/x86_64.rs:910:29
|
910 | let mut state = orig_state.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: `vmm` (lib test) generated 1 warning (run `cargo clippy --fix --lib -p vmm --tests` to apply 1 suggestion)
Lint Name
clippy::redundant_clone
Version
rustc 1.70.0 (90c541806 2023-05-31)
binary: rustc
commit-hash: 90c541806f23a127002de5b4038be731ba1458ca
commit-date: 2023-05-31
host: x86_64-unknown-linux-gnu
release: 1.70.0
LLVM version: 16.0.2
Additional Labels
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the clippy::redundant_clone lint and reproduce the warning using the src/vmm/src/vstate/vcpu/x86_64.rs example, including cargo clippy --fix --lib -p vmm --tests. Trace how the two scoped uses of orig_state are analyzed; done means the required clone remains accepted without emitting the redundant-clone warning.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100