rust-lang / rust-lang/rust-clippy

Warn if make iterator out of iterator in `for`.

Open
#2,369 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-enhancement E-medium
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Hi.

fn main() {
    // for m in (1..10).take(5) is enough!
    for m in (1..10).take(5).collect::<Vec<i32>>() {
        println!("{}", m);
    }
}

.take(5) gives iterator that is usable in for loop.
.collect::<Vec<i32>>() creates Vec from .take iterator and it(Vec) has iterator behaviour too.

What about creating lint, that warns, because code creates iterator out of iterator unnecessarily?
Thanks.

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

The issue names no files, tests, or entry points. Start from the Rust example in the report and inspect existing iterator-related lints in rust-clippy; done means a lint warns when a collected iterator is immediately used in a for loop, with coverage for the reported pattern.

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
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.