Skip to content

redundant_clone on enum cast #10074

@Tibo-lg

Description

@Tibo-lg

Summary

Casting an enum consumes it, but clippy doesn't seem to agree. Applying the suggested fix makes it impossible to compile the code.

Lint Name

redundant_clone

Reproducer

I tried this code:

#[derive(Debug, Clone)]
enum MyEnum {
    A = 1,
}

fn main() {
    let e = MyEnum::A;
    let v = e.clone() as u16;
    
    println!("{e:?}");
    println!("{v}");
}

I saw this happen:

 Checking playground v0.0.1 (/playground)
warning: redundant clone
 --> src/main.rs:9:14
  |
9 |     let v = e.clone() as u16;
  |              ^^^^^^^^ help: remove this
  |
note: cloned value is neither consumed nor mutated
 --> src/main.rs:9:13
  |
9 |     let v = e.clone() as u16;
  |             ^^^^^^^^^
  = 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 fix --bin "playground"` to apply 1 suggestion)
    Finished dev [unoptimized + debuginfo] target(s) in 0.46s

I expected to see this happen:

No warning

Version

1.65.0 (rust playground)

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