rust-lang / rust-lang/rust

Detect when pattern enum variant is not on the right type

Open
#159,461 3 comments 0 reactions 1 assignee View on GitHub

@sidchintamaneni is already working on this.

Since Jul 21, 2026.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
struct Segment {
    style: Style,
}

enum Style {
    Normal,
    Highlight,
}

fn main() {
    let segment = Segment { style: Style::Normal };
    match segment.style {
        Segment::Normal => {}
        Segment::Highlight => {}
    }
}
Current output
error[E0599]: no associated function or constant named `Normal` found for struct `Segment` in the current scope
  --> src/main.rs:13:18
   |
 1 | struct Segment {
   | -------------- associated function or constant `Normal` not found for this struct
...
13 |         Segment::Normal => {}
   |                  ^^^^^^ associated function or constant not found in `Segment`

error[E0599]: no associated function or constant named `Highlight` found for struct `Segment` in the current scope
  --> src/main.rs:14:18
   |
 1 | struct Segment {
   | -------------- associated function or constant `Highlight` not found for this struct
...
14 |         Segment::Highlight => {}
   |                  ^^^^^^^^^ associated function or constant not found in `Segment`
Desired output
error[E0599]: no associated function or constant named `Normal` found for struct `Segment` in the current scope
  --> src/main.rs:13:18
   |
 1 | struct Segment {
   | -------------- associated function or constant `Normal` not found for this struct
   |
 2 | enum Style {
   | ---------- enum variant `Normal` found for this struct
...
12 |     match segment.style {
   |           ------------- this expression has type `Style`, not `Segment`
13 |         Segment::Normal => {}
   |         -------  ^^^^^^ associated function or constant not found in `Segment`
   |         |
   |         you might have meant to use `Style` here
Rationale and extra context

No response

Other cases
Found initially when making the same mistake in rustc:

error[E0599]: no associated function or constant named `Normal` found for struct `StringPart` in the current scope
   --> compiler/rustc_errors/src/diagnostic.rs:221:30
    |
221 |                 (StringPart::Normal, StringPart::Normal)
    |                              ^^^^^^ associated function or constant not found in `StringPart`
...
236 | pub struct StringPart {
    | --------------------- associated function or constant `Normal` not found for this struct
    |
help: there is an associated function `normal` with a similar name
   --> compiler/rustc_errors/src/diagnostic.rs:242:5
    |
242 |     pub fn normal<S: Into<String>>(content: S) -> StringPart {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Rust Version
1.97
Anything else?

There are several things we could be checking for here, like whether any segment of the matched expression's chain is of the type that was found, as well as checking whether the pattern has the correct type.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.