Skip to content

Tweak irrefutable let else warning output #153454

@estebank

Description

@estebank

Given

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

    // TODO: greet the user by name.
}

after #152938 the output is

warning: irrefutable `let...else` pattern
 --> src/lib.rs:2:5
  |
2 |     let name_str = Some(name) else {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: this pattern always matches, so the else clause is unreachable
help: remove this `else` block
 --> src/lib.rs:2:36
  |
2 |       let name_str = Some(name) else {
  |  ____________________________________^
3 | |         println!("Hello person!");
4 | |         return;
5 | |     };
  | |_____^
  = note: `#[warn(irrefutable_let_patterns)]` on by default

It could do with some tweaks. We can provide a structured suggestion (not very convinced that that is the right suggestion unless we have a lot of certainty about the types involved) and instead of a note about the irrefutable pattern, make it a span label:

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

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.P-lowLow priorityT-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