Skip to content

fix(explicit_counter_loop): suggest .take(n) for for _ in 0..n co…#16658

Merged
Jarcho merged 1 commit intorust-lang:masterfrom
zayutaha:fix/explicit-counter-loop-take-suggestion
Mar 27, 2026
Merged

fix(explicit_counter_loop): suggest .take(n) for for _ in 0..n co…#16658
Jarcho merged 1 commit intorust-lang:masterfrom
zayutaha:fix/explicit-counter-loop-take-suggestion

Conversation

@zayutaha
Copy link
Copy Markdown
Contributor

@zayutaha zayutaha commented Mar 3, 2026

When a loop variable is unused (for _ in 0..n) and the counter is being
incremented, Clippy previously suggested the generic zip-based rewrite.
This PR adds a more idiomatic .take(n) suggestion for the common case
where the range is half-open and starts at zero:

// before
for _ in 0..MAX {
    base += 1;
}

// suggested
for base in (100..).take(MAX)

Non-zero starts (5..MAX) and inclusive ranges (0..=MAX) are
explicitly excluded and fall through to the existing zip suggestion.
Both cases are covered by new UI tests.

changelog: [explicit_counter_loop]: suggest (init..).take(n) when loop variable is unused and range is 0..n

fixes #16642

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 3, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 3, 2026

r? @Jarcho

rustbot has assigned @Jarcho.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 7 candidates
  • 7 candidates expanded to 7 candidates
  • Random selection from Jarcho, dswij, llogiq, samueltardieu

@zayutaha zayutaha force-pushed the fix/explicit-counter-loop-take-suggestion branch from ae45101 to 6fe961a Compare March 3, 2026 13:23
@zayutaha zayutaha marked this pull request as draft March 3, 2026 13:24
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 3, 2026
@zayutaha zayutaha marked this pull request as ready for review March 3, 2026 13:33
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 3, 2026
Comment thread clippy_lints/src/loops/explicit_counter_loop.rs Outdated
@zayutaha zayutaha force-pushed the fix/explicit-counter-loop-take-suggestion branch from 6fe961a to 07123db Compare March 5, 2026 14:53
@zayutaha zayutaha requested a review from Jarcho March 5, 2026 15:17
@zayutaha zayutaha force-pushed the fix/explicit-counter-loop-take-suggestion branch from 07123db to 9303cb9 Compare March 26, 2026 12:14
…unter loops

fix(explicit_counter_loop): suggest `.take(n)` for `for _ in 0..n` counter loops
@zayutaha zayutaha force-pushed the fix/explicit-counter-loop-take-suggestion branch from 9303cb9 to daad0fb Compare March 26, 2026 13:13
Copy link
Copy Markdown
Contributor

@Jarcho Jarcho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jarcho Jarcho added this pull request to the merge queue Mar 27, 2026
Merged via the queue into rust-lang:master with commit 0a29aac Mar 27, 2026
11 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 27, 2026
@zayutaha zayutaha deleted the fix/explicit-counter-loop-take-suggestion branch March 28, 2026 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

explicit_counter_loop suggests .zip() when .take() would be clearer

3 participants