Skip to content

Warning on redundant_clone when required with scopes #10893

Open
@JonathanWoollett-Light

Description

@JonathanWoollett-Light

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)

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions