Skip to content

Fix needless_range_loop false positive with nested indexing#17438

Open
XhesicaFrost wants to merge 1 commit into
rust-lang:masterfrom
XhesicaFrost:fix-17432-needless-range-loop
Open

Fix needless_range_loop false positive with nested indexing#17438
XhesicaFrost wants to merge 1 commit into
rust-lang:masterfrom
XhesicaFrost:fix-17432-needless-range-loop

Conversation

@XhesicaFrost

@XhesicaFrost XhesicaFrost commented Jul 20, 2026

Copy link
Copy Markdown

needless_range_loop could emit an incorrect suggestion for nested indexing
expressions such as a[sum % 5][i].

In this case, the container indexed by the loop variable may change between
iterations, so replacing the range loop with an iterator over the base
container would change the program's semantics.

This detects non-constant container-selection indices while walking nested
index expressions and suppresses the lint in those cases. The existing behavior
is preserved when the loop variable selects the outer container.

A regression test for the reported case and a corresponding positive test have
been added.

fixes #17432

changelog: [needless_range_loop]: avoid false positives when indexing dynamically selected nested containers

@rustbot rustbot added S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jul 20, 2026
@rustbot

rustbot commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome!

You should hear from one of our reviewers after this PR gets at least 2 reviews from the community.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@XhesicaFrost
XhesicaFrost force-pushed the fix-17432-needless-range-loop branch from 7790ac9 to 1575048 Compare July 21, 2026 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. 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.

incorrect needless_range_loop

2 participants