Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 opened this issue Feb 27, 2024 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. L-non_snake_case Lint: non_snake_case T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@kareled
Copy link

kareled commented Feb 27, 2024

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

@kareled kareled added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 27, 2024
@jieyouxu
Copy link
Member

jieyouxu commented Feb 27, 2024

The problem is that TestV2 is a valid binding (it is not your declared enum variant) and the match is exhaustive, albeit with a non-snake-case name. I do agree that this is very confusing and almost certainly warrants some kind of lint, maybe even warn-by-default in a future edition.

See also related:

Possible resolution:

We agreed that the current lint should not fire on uses, only on declarations. However, we also felt cases that are ambiguous between being a constant or a binding (e.g. bare names like xyzABC without a ::), we should have a different lint to flag those and suggest that they be made unambiguously either a binding or a constant.
A pattern could be made unambiguously a binding with an bound_ident @ _, or made unambiguously a constant by giving a path that includes a ::.

from #25207.

@jieyouxu jieyouxu added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 27, 2024
@jieyouxu jieyouxu changed the title Simple typo results in wrong match processing Match on enum with a non_snake_case variable that has the same name as one of the enum variants is potentially confusing Feb 27, 2024
@jieyouxu jieyouxu added the L-non_snake_case Lint: non_snake_case label May 13, 2024
@fmease fmease added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. and removed A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. labels Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. L-non_snake_case Lint: non_snake_case T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants