rust-lang / rust-lang/rust-clippy

New lint: replace `iter.filter(x).count() > 0` with `iter.any(x)`

Open
#5,932 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint L-complexity S-needs-discussion
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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.