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

Non-exhaustive patterns lead to bogus "unused variable" / "dead code" warnings #63654

Open
RalfJung opened this issue Aug 17, 2019 · 2 comments
Labels
A-lint 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

@RalfJung
Copy link
Member

Consider this code:

fn foo() -> Option<i32> { None }

pub fn bar(x: i32) {
    match foo() {
        Some(_) => return,
    }
    
    let _val = Box::new(x);
}

This generates the following diagnostics on current nightly:

warning: unreachable statement
 --> src/lib.rs:8:5
  |
8 |     let _val = Box::new(x);
  |     ^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unreachable_code)]` on by default

error[E0004]: non-exhaustive patterns: `None` not covered
 --> src/lib.rs:4:11
  |
4 |     match foo() {
  |           ^^^^^ pattern `None` not covered
  |
  = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

warning: unused variable: `x`
 --> src/lib.rs:3:8
  |
3 | pub fn bar(x: i32) {
  |        ^ help: consider prefixing with an underscore: `_x`
  |
  = note: `#[warn(unused_variables)]` on by default

The "unused variable" and "dead code" lints are spurious, and not helpful. This only seems like dead code because there's a missing match arm. After adding _ => {} in the match, all diagnostics go away.

@Centril Centril added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. labels Aug 17, 2019
@shekhei
Copy link

shekhei commented Jan 20, 2020

I would like to try this.

What would be the preferred behavior? Should the "unused variable" / "dead code" warnings be suppressed for this case?

shekhei added a commit to shekhei/rust that referenced this issue Feb 10, 2020
@Spoonbender
Copy link

Triage: no change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint 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

No branches or pull requests

4 participants