rust-lang / rust-lang/rust-clippy

Avoid: call any()/all() on iterator after had called collect(), like: vec![1, 2, 3].iter().filter(...).collect().iter().any(...)

Open
#11,567 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

As the title says, it is not suggest to use any()/all() after iterator call collect(), instead just use any()/all( ) directly.

Advantage
  • make the code more correct and efficient
Drawbacks

No response

Example
let arr = [-33, -1, 0, 3, 4, 7, 9, 11, 34, 100].repeat(10000);
arr.iter()
    .map(|x| x + 100)
    .collect::<Vec<i32>>()
    .iter()
    .any(|x| x % 2 == 0);

arr.iter()
    .map(|x| x + 100)
    .collect::<Vec<i32>>()
    .iter()
    .all(|x| x % 2 == 0);

Could be written as:

let arr = [-33, -1, 0, 3, 4, 7, 9, 11, 34, 100].repeat(10000);
arr.iter()
    .map(|x| x + 100)
    .any(|x| x % 2 == 0);

arr.iter()
    .map(|x| x + 100)
    .all(|x| x % 2 == 0);

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 source file, test, or entry point is named; start with the two Rust examples in the issue and locate the existing Clippy lint patterns for iterator checks. Done means a check recognizes collect().iter().any/all() chains and recommends calling any/all directly, with coverage for both examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.