Skip to content

Spurious unused field warning when matching variants under a Self scope #63151

Closed
@jakubadamw

Description

@jakubadamw

This code should compile without warnings:

enum Enum {
    Variant { field: usize }
}
impl Enum {
    fn read_field(self) -> usize {
        match self {
            // Enum::Variant { field } => field
            Self::Variant { field } => field
        }
    }
}
fn main() {
    let e = Enum::Variant { field: 42 };
    println!("{}", e.read_field());
}

However, it produces the following one:

warning: field is never used: `field`
 --> src/main.rs:2:15
  |
2 |     Variant { field: usize }
  |               ^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

If we switch the pattern within match to the one that's commented out, the warning is gone.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.T-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