rust-lang / rust-lang/rust-clippy

new lint: `endless_iter`

Open
#9,699 3 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
  1. We can create an endless Iterator easily by calling the Iterator::cycle() method or by Range literal construction without an upper bound.
    There is a corner case; the cycle call on an empty iterator will not create an endless iterator.

  2. The endless property will be passed by many methods without an early return branch, e.g.:

  • map
  • filter
  • skip
  • skip_while
  • map_while
  • zip_longest
  • ...
  1. We also have some finalizers that will not early return:
  • min
  • max
  • last
  • count
  • for_each
  • reduce
  • collect *
  • sum *
  • product *
  • ...

There are also some false negative cases because FromIter, Sum and Product are customizable, so there may be some early-return implementation.

We can detect such cases.

Another acceptable name for the lint is infinite_iter, both are good.

Lint Name

endless_iter

Category

correctness

Advantage

Avoid some endless loops

Drawbacks

Some false negative cases

Example
(1..).map(|x| x + 2).for_each(|x| dbg!(x))

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 with the Rust example and the listed iterator methods and finalizers; done means the new correctness lint detects applicable endless iterator chains while accounting for the documented false negatives.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.