Skip to content

Match Allows Multiple Identical as i32 Statements Without Warning #14427

Open
@oysteinmyrmo

Description

@oysteinmyrmo

I tried this code:

#[repr(i32)]
pub enum Something {
    Foo = 0,
    Bar = 1,
    Blarg = 2,
}

fn main() {
    let my_int: i32 = 1;

    match my_int {
        1 => {}
        //1 => {} // Warning: no value can reach this
        x if x == Something::Foo as i32 => {}
        x if x == Something::Foo as i32 => {} // No warning
        x if x == Something::Foo as i32 => {} // No warning
        x if x == Something::Blarg as i32 => {}
        _ => {}
    }
}

rustc seems to happily accept multiple identical cases of enums as i32 in match statements. Using i32 directly gives warnings as expected, but not when representing the enum as i32. There are no warnings in nightly nor in rustc 1.0.0, tested on https://rust.godbolt.org/.

I would expect the same warning for the second x if x == Something::Foo as i32 => {} as for the second 1 => {} in the example below.

At the time of writing https://rust.godbolt.org/ does not allow me to see the disassembly for nightly. But for 1.0.0 the same code is generated for each statement with different offsets.

rustc --version --verbose:

nightly

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn't

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions