Skip to content

Irrefutable pattern warning is confusing when pattern is on RHS #152938

@Wilfred

Description

@Wilfred

Code

pub fn say_hello(name: Option<String>) {
    let name_str = Some(name) else {
        println!("Hello person!");
        return;
    };

    // TODO: greet the user by name.
}

Current output

warning: irrefutable `let...else` pattern
 --> src/lib.rs:2:5
  |
2 |     let name_str = Some(name) else {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: this pattern will always match, so the `else` clause is useless
  = help: consider removing the `else` clause
  = note: `#[warn(irrefutable_let_patterns)]` on by default

Desired output

warning: unreachable `else` clause
 --> src/lib.rs:2:5
  |
2 |     let name_str = Some(name) else {
  |                               ^^^^
  |
  = note: this pattern will match anything
  = help: consider using `let Some(name_str)` to match on a specific variant
  = note: `#[warn(irrefutable_let_patterns)]` on by default

Rationale and extra context

This took me a minute to realise what I was doing wrong: the code doesn't look obviously wrong at first glance.

The irrefutable message is also a little opaque to me. I know 'irrefutable' is in the warning name irrefutable_let_patterns, but does it need to be in the prose? E.g. would something like this better (inspired by unreachable_code)?

warning: `let...else` has unreachable `else` block

Other cases

Rust Version

rustc 1.95.0-nightly (7f99507f5 2026-02-19)
binary: rustc
commit-hash: 7f99507f57e6c4aa0dce3daf6a13cca8cd4dd312
commit-date: 2026-02-19
host: x86_64-unknown-linux-gnu
release: 1.95.0-nightly
LLVM version: 22.1.0

Anything else?

No response

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions