rust-lang / rust-lang/rust-clippy

`single_match` shouldn't warn when matching on an explicit type

Open
#13,371 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

In the following code, I think the clippy suggestion is worse than the original code. The original code is explicitly matching on the error "NotFoundError". If the error return type of foo were to change in the future (for example to become an enum with multiple error variants, or to become something like "NotAuthenticatedError" instead), we'd want this match statement to fail to build so that we could update it for the new error type and make sure we're handling it correctly. The clippy suggestion wants us to remove the error matching completely.

I think it would be nicer if this lint only triggered when the error type is omitted completely, for example Err(_) => {}.

Lint Name

single_match

Reproducer

I tried this code:

struct NotFoundError;

fn bar() -> Result<(), NotFoundError> { todo!() }

fn main() {
    match bar() {
        Ok(()) => {},
        // don't need to do anything if not found
        Err(NotFoundError) => {},
    }
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=421e3fe89d0da5e2685c435d43246689

I saw this happen:

warning: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
  --> src/main.rs:6:5
   |
6  | /     match bar() {
7  | |         Ok(()) => {},
8  | |         // don't need to do anything if not found
9  | |         Err(NotFoundError) => {},
10 | |     }
   | |_____^ help: try: `if let Ok(()) = bar() {}`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_match
   = note: `#[warn(clippy::single_match)]` on by default

I expected to see this happen:

No warning.

Version
rustc 1.83.0-nightly (26b5599e4 2024-09-06)
binary: rustc
commit-hash: 26b5599e4d6ed2b45152c60493c1788c0a27533d
commit-date: 2024-09-06
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the single_match warning with the Rust code in the issue and inspect the lint implementation and its tests by searching for single_match. The change is done when an explicit error pattern such as Err(NotFoundError) does not trigger the lint, while an omitted error pattern such as Err(_) still behaves as intended.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.