Skip to content

single_match should lint exhaustive match on standard types #8928

@yeputons

Description

@yeputons

Summary

After #8282 was closed via #8322 (in Rust 1.60) the example below is no longer linting.

The original rationale was that Clippy should not lint matches on user-defined enums which explicitly list all possible options. Otherwise extending the enum later would break the original code, but not the fixed one, which may be unexpected for the author.

However, there is little reason to believe that standard types like std::option::Option will be extended in the future. I suggest that they're exempt from this check. Another example is std::result::Result. Of course, that does not help with user-defined Result-like types like in the anyhow crate, but still.

Lint Name

single_match

Reproducer

I tried this code:

fn main() {
    let x = Some(10);
    match x {
        Some(_) => println!("hi!"),
        None => {},
    };
}

I expected to see this happen:

Like in Rust 1.58:

warning: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
 --> a.rs:3:5
  |
3 | /     match x {
4 | |         Some(x) => println!("hi! {}", x),
5 | |         None => {},
6 | |     };
  | |_____^ help: try this: `if let Some(x) = x { println!("hi! {}", x) }`
  |
  = note: `#[warn(clippy::single_match)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_match

warning: 1 warning emitted

Instead, this happened:

No output

Version

Clippy 0.1.63 (2022-05-31 e094492)

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn't

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions