rust-lang / rust-lang/rust-clippy
New lint: replace `iter.filter(x).count() > 0` with `iter.any(x)`
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
It suggests replacing iter.filter(x).count() > 0 with iter.any(x). The suggestion should be MachineApplicable.
Categories (optional)
- Kind: complexity or performance
What is the advantage of the recommended code over the original code
It's more concise and it should be faster since any is allowed to return early while filter().count() has to walk through all items.
Drawbacks
None.
Example
segments.iter().filter(|ps| ps.ident.as_str() == "prelude").count() > 0
Could be written as:
segments.iter().any(|ps| ps.ident.as_str() == "prelude")
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
No file or test is named in the issue. Start by locating existing Clippy lints that transform filter(...).count() > 0 and their applicability tests; done means the Rust example is diagnosed, the suggestion is MachineApplicable, and tests cover the rewrite.
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
- 45/100