Skip to content

Subpar diagnostics: unreachable_code lint fires before match arm coverage check #46426

Closed
@pnkfelix

Description

@pnkfelix

When you do #![deny(unreachable_code)] (which is implied by #![deny(warnings)]), the corresponding lint runs and aborts compilation before the match arm coverage check gets a chance to report.

This leads to somewhat confusing messages.

  • at least, they are confusing depending on what the user's mental model is of how match expressions should hypothetically behave if they were not forced to cover the domain of the input expression completely

Example:

#![deny(unreachable_code)]

fn main() {
    let v = vec![1, 2, 3];
    let mut i = v.iter().peekable();
    'label: while let Some(e1) = i.next() {
        
        match e1 {
            &1 => {
                continue 'label;
            }
        }
        println!("e1: {:?}", e1);
    }
}

yields the error message (playpen)

error: unreachable statement
  --> src/main.rs:13:9
   |
13 |         println!("e1: {:?}", e1);
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: lint level defined here
  --> src/main.rs:1:9
   |
1  | #![deny(unreachable_code)]
   |         ^^^^^^^^^^^^^^^^
   = note: this error originates in a macro outside of the current crate

error: aborting due to previous error

which, when I first look at the code, I think "that println! is clearly reachable", because in my head, execution is going to continue past the match expression after that first arm is not matched.

If I change the deny to warn, I get both the warning as above as well as an error about the non-exhaustive patterns in the match expression, saying that &_ is not covered.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-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