Skip to content

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

Open
zayutaha wants to merge 1 commit intorust-lang:masterfrom
zayutaha:fix/explicit-counter-loop-take-suggestion
Open

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

Conversation

@zayutaha
Copy link

@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
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
if pat_snippet == "_"
&& let Some(range) = Range::hir(cx, arg)
&& range.limits == RangeLimits::HalfOpen
&& range.start.is_none_or(|start| is_integer_const(cx, start, 0))
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is_none_or? RangeTo isn't iterable in the first place.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the suggestion! The zero check is still needed though — without it, for _ in 5..MAX would incorrectly suggest (init..).take(MAX), which runs MAX times instead of the correct MAX - 5 times. I've added test cases for both non-zero start and inclusive ranges to cover this.

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

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