rust-lang / rust-lang/rust-clippy
Suggest `Option::is_some_and(...)` for manual `matches!(x, Some(x) if ...)` implementations
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Title.
Should also work for Result and its variants (is_ok_and, is_err_and).
Found while going through warnings for popsicle, where clippy suggested to replace
let sensitive = match combo_box.active_text() {
Some(text) if text.as_str() != "None" => true,
_ => false,
};
with
let sensitive = matches!(combo_box.active_text(), Some(text) if text.as_str() != "None");
However, Clippy didn't go from matches! to the even more concise alternative using is_some_and:
let sensitive = combo_box.active_text().is_some_and(|text| text.as_str() != "None");
Advantage
is_variant_and is shorter than the matches! alternative
is_variant_and results in code that is more readable
Drawbacks
MSRV of 1.70.0
Example
let is_empty = matches!(option, Some(x) if x.is_empty());
Could be written as:
let is_empty = option.is_some_and(|x| x.is_empty());
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 by locating existing Clippy lints that analyze matches! expressions and suggest method-based simplifications. Compare the requested Option::is_some_and transformation with the analogous Result methods, then verify that the lint recognizes the examples and respects the stated Rust 1.70.0 MSRV.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100