Skip to content

"irrefutable_let_patterns" shouldn't fire for leading if-let guards #98361

Closed
@goffrie

Description

@goffrie

sample code:

#![feature(if_let_guard, let_chains)]

pub fn example(x: Option<Option<&str>>) {
    match x {
        Some(y) if let z = y.expect("can't be Some(None)") && z != "foo" => {
            dbg!(z);
        }
        _ => (),
    }
}

yields this warning:

warning: leading irrefutable pattern in let chain
 --> src/lib.rs:5:20
  |
5 |         Some(y) if let z = y.expect("can't be Some(None)") && z != "foo" => {
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(irrefutable_let_patterns)]` on by default
  = note: this pattern will always match
  = help: consider moving it outside of the construct

However, the suggestion to move it out of the construct doesn't make sense, as y isn't bound until we've entered the match arm in the first place.

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.F-if_let_guard`#![feature(if_let_guard)]`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