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

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

Closed
jakubadamw opened this issue Jul 31, 2019 · 0 comments · Fixed by #63227
Closed

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

jakubadamw opened this issue Jul 31, 2019 · 0 comments · Fixed by #63227
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jakubadamw
Copy link
Contributor

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.

@Centril Centril added C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. labels Jul 31, 2019
Centril added a commit to Centril/rust that referenced this issue Aug 3, 2019
dead_code: Properly inspect fields in struct patterns with type relative paths

Closes rust-lang#63151.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants