Rust

Rust

By DeepSource

Found cast from enum constructor or variant RS-W1103

Anti-pattern

An enum member can be either a value variant or a constructor, hence casting them can be risky. Instead, you may want to store the value as a member inside the variant and use that, or create a value enum using the repr attribute.

Cast the enum value instead of the constructor.

Bad practice

enum Foo {
    Bar(i32)
}

fn baz() {
    // Foo::Bar is a constructor for the value
    let _ = Foo::Bar as usize;
}

Recommended

#[repr(u32)]
enum Foo {
    Bar = 0
}

fn baz() {
    // this works as it is guaranteed to be a value
    let _ = Foo::Bar as usize;
}