Skip to content

Match on enum with a non_snake_case variable that has the same name as one of the enum variants is potentially confusing #121704

Open
@kareled

Description

@kareled

Code

enum Test {
    V1,
    V2,
    V3
}

fn process(t : &Test) -> u8 {
    match t {
        Test::V1 => 1,
        // here is the issue! Simple typo of missing '::' in Test::V2
        // results in errors of missing arms being removed and just few harmless
        // warnings emitted.
        TestV2 => 2,
    }
}

fn main() {
    let test_val = Test::V3;
    // to minimize amount of warnings, let's construct others too
    let _v1 = Test::V1;
    let _v2 = Test::V2;
    // let's process value which is "by mistake" not handled.
    let x = process(&test_val);
    println!("got: {}", x);
}

Current output

$ cargo build
   Compiling match1 v0.1.0 (/mnt/c/users/gardas/src/rust/bugs/match1)
warning: unused variable: `TestV2`
  --> src/main.rs:14:9
   |
14 |         TestV2 => 2,
   |         ^^^^^^ help: if this is intentional, prefix it with an underscore: `_TestV2`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: variable `TestV2` should have a snake case name
  --> src/main.rs:14:9
   |
14 |         TestV2 => 2,
   |         ^^^^^^ help: convert the identifier to snake case: `test_v2`
   |
   = note: `#[warn(non_snake_case)]` on by default

warning: `match1` (bin "match1") generated 2 warnings (run `cargo fix --bin "match1"` to apply 1 suggestion)
    Finished dev [unoptimized + debuginfo] target(s) in 2.25s

Desired output

$ cargo build
   Compiling match1 v0.1.0 (/mnt/c/users/gardas/src/rust/bugs/match1)
error[E0004]: non-exhaustive patterns: `&Test::V2` and `&Test::V3` not covered
 --> src/main.rs:9:11
  |
9 |     match t {
  |           ^ patterns `&Test::V2` and `&Test::V3` not covered
  |
note: `Test` defined here
 --> src/main.rs:2:6
  |
2 | enum Test {
  |      ^^^^
3 |     V1,
4 |     V2,
  |     -- not covered
5 |     V3
  |     -- not covered
  = note: the matched value is of type `&Test`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
  |
10~         Test::V1 => 1,
11~         &Test::V2 | &Test::V3 => todo!(),
  |

For more information about this error, try `rustc --explain E0004`.
error: could not compile `match1` (bin "match1") due to 1 previous error

Rationale and extra context

No response

Other cases

No response

Rust Version

rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-unknown-linux-gnu
release: 1.76.0
LLVM version: 17.0.6

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.L-non_snake_caseLint: non_snake_caseT-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions