rust-lang / rust-lang/rust-clippy
needless_match can trigger from some single-arm capture matches
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
The needless_match lint can apparently trigger incorrectly in some edge cases involving capturing matches.
This could be a wild goose chase on what the root cause could be so I'd rather not chase it down much, someone with a better idea would be better to take a look.
I think the gist is that is you capture the whole match but only on a discriminant, the lint might think you are just always trying to return the value, which is not correct.
Lint Name
needless_match
Reproducer
I tried this code:
let ord = match self
.x
.partial_cmp(&other.string_match_param)
{
ord @ Some(Ordering::Equal) => ord,
other => return other,
};
I saw this happen:
warning: this match expression is unnecessary
--> src/x.rs:565:19
|
565 | let ord = match self
| ___________________^
566 | | .string_match_param
567 | | .partial_cmp(&other.x)
568 | | {
569 | | ord @ Some(Ordering::Equal) => ord,
570 | | other => return other,
571 | | };
| |_________^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_match
= note: `#[warn(clippy::needless_match)]` on by default
help: replace it with
|
565 ~ let ord = self
566 + .x
567 ~ .partial_cmp(&other.x);
|
I expected to see this happen:
No Error - this match serves are purpose and cannot be eliminated like that.
Version
rustc 1.70.0 (90c541806 2023-05-31)
binary: rustc
commit-hash: 90c541806f23a127002de5b4038be731ba1458ca
commit-date: 2023-05-31
host: aarch64-apple-darwin
release: 1.70.0
LLVM version: 16.0.2
Additional Labels
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the needless_match lint and reproduce the warning with the Rust match expression shown in the issue. Trace how the single-arm capture ord @ Some(Ordering::Equal) is classified, then verify that this valid match no longer produces the needless_match warning while other unnecessary matches remain covered.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100