Skip to content

incorrect diagnostic on irrefutable while-let with break #101539

Open
@pabigot

Description

@pabigot

Reviewing #87335 (comment) after finding a need for a while-let loop that provides access to an else pattern I crafted this program:

fn main() {
    println!("in");
    let mut x: Result<(), &'static str> = Ok(());
    'wl: while let _ = match x {
        Ok(v) => {
            println!("match ok {:?}", v);
            x = Err("text");
        },
        Err(_) => break 'wl,
    } {
        println!("body");
    }
    println!("out");
}

The rustc diagnostic output is:

warning: irrefutable `while let` pattern
  --> src/main.rs:4:16
   |
4  |       'wl: while let _ = match x {
   |  ________________^
5  | |         Ok(v) => {
6  | |             println!("match ok {:?}", v);
7  | |             x = Err("text");
8  | |         },
9  | |         Err(_) => break 'wl,
10 | |     } {
   | |_____^
   |
   = note: `#[warn(irrefutable_let_patterns)]` on by default
   = note: this pattern will always match, so the loop will never exit
   = help: consider instead using a `loop { ... }` with a `let` inside it

while the application output is:

in
match ok ()
body
out

Ideally the output should have no diagnostic, because the loop clearly exits.

Metadata

Metadata

Assignees

No one assigned

    Labels

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