Skip to content

Look for available consts when typoing pattern #132582

Closed

Description

@estebank

Code

fn main() {
    let input: i32 = 42;
    
    const GOOD: i32 = 1;
    const BAD: i32 = 2;
    
    match input {
    	GOD => println!("input was 1"), //~ WARN
    	otherwise => println!("input was {otherwise}"),
    }
}

Current output

warning: unreachable pattern
 --> src/main.rs:9:6
  |
8 |         GOD => println!("input was 1"),
  |         --- matches any value
9 |         otherwise => println!("input was {otherwise}"),
  |         ^^^^^^^^^ no value can reach this
  |
  = note: `#[warn(unreachable_patterns)]` on by default

warning: unused variable: `GOD`
 --> src/main.rs:8:6
  |
8 |         GOD => println!("input was 1"),
  |         ^^^ help: if this is intentional, prefix it with an underscore: `_GOD`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: constant `GOOD` is never used
 --> src/main.rs:4:11
  |
4 |     const GOOD: i32 = 1;
  |           ^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: constant `BAD` is never used
 --> src/main.rs:5:11
  |
5 |     const BAD: i32 = 2;
  |           ^^^

warning: variable `GOD` should have a snake case name
 --> src/main.rs:8:6
  |
8 |         GOD => println!("input was 1"),
  |         ^^^ help: convert the identifier to snake case: `god`
  |
  = note: `#[warn(non_snake_case)]` on by default

Desired output

warning: unreachable pattern
 --> src/main.rs:9:6
  |
8 |         GOD => println!("input was 1"),
  |         --- matches any value
9 |         otherwise => println!("input was {otherwise}"),
  |         ^^^^^^^^^ no value can reach this
  |
  = note: `#[warn(unreachable_patterns)]` on by default
note: `GOD` looks similar to constant `GOOD`
  |
4 |     const GOOD: i32 = 1;
  |           ^^^^
help: you might have meant to pattern match on the value of the constant `GOOD`
  |
8 |         GOOD => println!("input was 1"),
  |         ~~~~

Rationale and extra context

No response

Other cases

We should also account for uppercase to lowercase changes, beyond just levenshtein distance.

Rust Version

current

Anything else?

No response

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Nov 4, 2024
  2. jieyouxu commented on Nov 4, 2024

    @jieyouxu
    Member

    Maybe cc #25207, #40476, #39371 and #121704.

  3. added a commit that references this issue on Nov 5, 2024
    ab19d0a
  4. added a commit that references this issue on Nov 20, 2024
    2487765
  5. added 2 commits that reference this issue on Nov 20, 2024
  6. added a commit that references this issue on Nov 21, 2024
  7. added a commit that references this issue on Nov 22, 2024
    f2d0388
  8. added a commit that references this issue on Nov 22, 2024
    db521ab
  9. added a commit that references this issue on Nov 23, 2024
    d2fb8b5
  10. added a commit that references this issue on Nov 23, 2024
    98663dd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions