Skip to content

add new lint: for_unbounded_range#16257

Merged
samueltardieu merged 1 commit into
rust-lang:masterfrom
Erk-:erk/new-lint/for-loops-over-unbounded-ranges
May 29, 2026
Merged

add new lint: for_unbounded_range#16257
samueltardieu merged 1 commit into
rust-lang:masterfrom
Erk-:erk/new-lint/for-loops-over-unbounded-ranges

Conversation

@Erk-
Copy link
Copy Markdown
Contributor

@Erk- Erk- commented Dec 18, 2025

View all comments

This adds a Clippy lint for the possible confusing behaviour pointed out in rust-lang/rust#150084 and rust-lang/rust#111514, and discussed in rust-lang/libs-team#304.

While no consensus have been reached in the API Change Proposal linked above I thought that it would still make sense to add a Clippy lint for the time being.

Open questions:

  • Which group should it be in?
  • Should it give warnings for AsciiChar, Ipv4Addr and Ipv6Addr? (Currently these will always panic when running over the max value)

changelog: new lint: [for_unbounded_range]

@rustbot rustbot added needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Dec 18, 2025
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Dec 18, 2025

r? @dswij

rustbot has assigned @dswij.
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

@Erk- Erk- force-pushed the erk/new-lint/for-loops-over-unbounded-ranges branch 2 times, most recently from cd100aa to 9220570 Compare December 18, 2025 10:32
ada4a
ada4a previously requested changes Jan 12, 2026
Copy link
Copy Markdown
Contributor

@ada4a ada4a left a comment

Choose a reason for hiding this comment

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

LGTM:) just one thing

View changes since this review

Comment thread clippy_lints/src/loops/for_unbounded_range.rs Outdated
@ada4a
Copy link
Copy Markdown
Contributor

ada4a commented Mar 4, 2026

r? clippy

@rustbot rustbot assigned samueltardieu and unassigned dswij Mar 4, 2026
@rustbot

This comment has been minimized.

@Erk- Erk- force-pushed the erk/new-lint/for-loops-over-unbounded-ranges branch from 2927a7d to 7572e5f Compare March 6, 2026 08:11
@rustbot

This comment has been minimized.

Copy link
Copy Markdown
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

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

Looking for break targeting the loop and for return might limit the number of false positives.

View changes since this review

|diag| {
diag.span_suggestion_verbose(
arg.span.shrink_to_hi(),
"for loops over unbounded ranges will wrap around, consider using `start..=MAX` instead",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it would be more correct to say "will panick or wrap around", as it will panick using the dev profile.

In both cases, the suggestion changes the semantics and should be MaybeIncorrect.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

changed it to "for loops over unbounded ranges will wrap around and may panic, consider using start..=MAX instead"

Comment thread clippy_lints/src/loops/mod.rs Outdated
Comment thread clippy_lints/src/loops/mod.rs Outdated
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 6, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 6, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label Mar 6, 2026
@Erk-
Copy link
Copy Markdown
Contributor Author

Erk- commented Mar 6, 2026

Looking for break targeting the loop and for return might limit the number of false positives.

I have implemented this using the LoopVisitor from the infinite loop checker.

I am not really sure I agree if it should be a thing as I personally think a loop over a unbounded integer/char range is always wrong, even if you break out of it at a later point.

@Erk-
Copy link
Copy Markdown
Contributor Author

Erk- commented Mar 6, 2026

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Mar 6, 2026
@Erk- Erk- requested a review from samueltardieu March 16, 2026 13:38
@Erk-
Copy link
Copy Markdown
Contributor Author

Erk- commented May 12, 2026

@samueltardieu Do you have time to give it another look?

Copy link
Copy Markdown
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

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

Note that the following code will make an incorrect suggestion, as the size of std::ffi::c_long depends on the platform, and the actual primitive type will be suggested instead:

    for i in 0.. {
        //~^ for_unbounded_range
        do_something::<std::ffi::c_long>(i);
    }

but since this is MaybeIncorrect I'm fine with that.

View changes since this review

Comment thread clippy_lints/src/loops/for_unbounded_range.rs Outdated
Comment thread clippy_lints/src/loops/mod.rs Outdated
Comment thread tests/ui/manual_memcpy/without_loop_counters.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 12, 2026
@samueltardieu samueltardieu added the lint-nominated Create an FCP-thread on Zulip for this PR label May 12, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 12, 2026

This lint has been nominated for inclusion.

A FCP topic has been created on Zulip.

@Erk- Erk- force-pushed the erk/new-lint/for-loops-over-unbounded-ranges branch from d6df932 to 17fdc88 Compare May 15, 2026 09:17
@rustbot

This comment has been minimized.

@Erk- Erk- force-pushed the erk/new-lint/for-loops-over-unbounded-ranges branch from 17fdc88 to ded35ce Compare May 29, 2026 10:02
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 29, 2026

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@Erk-
Copy link
Copy Markdown
Contributor Author

Erk- commented May 29, 2026

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels May 29, 2026
@Erk-
Copy link
Copy Markdown
Contributor Author

Erk- commented May 29, 2026

What would you say the type of i is?

Yeah that makes sense, it would not really be ergonomic to say that it was the unification of those two (which is probably not the fully correct terminology either), but I guess that does also mean that you currently could not write a (at least late) lint to guard against that behavior.

Almost ready!

I think that should be the rest, I rebased it as well to make sure everything was up to date.

Copy link
Copy Markdown
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

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

Can you please squash the commits together?

View changes since this review

@Erk- Erk- force-pushed the erk/new-lint/for-loops-over-unbounded-ranges branch from ded35ce to 8fb4470 Compare May 29, 2026 10:58
@Erk-
Copy link
Copy Markdown
Contributor Author

Erk- commented May 29, 2026

Can you please squash the commits together?

Done

@Erk- Erk- requested a review from ada4a May 29, 2026 11:29
@samueltardieu samueltardieu added this pull request to the merge queue May 29, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks May 29, 2026
Comment thread tests/ui/for_unbounded_range.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 29, 2026
@Erk- Erk- force-pushed the erk/new-lint/for-loops-over-unbounded-ranges branch from 8fb4470 to 4858a73 Compare May 29, 2026 13:55
@Erk-
Copy link
Copy Markdown
Contributor Author

Erk- commented May 29, 2026

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels May 29, 2026
@samueltardieu samueltardieu added this pull request to the merge queue May 29, 2026
@github-actions
Copy link
Copy Markdown

Lintcheck changes for 4858a73

Lint Added Removed Changed
clippy::multiple_crate_versions 0 0 1

This comment will be updated if you push new changes

Merged via the queue into rust-lang:master with commit aede4dc May 29, 2026
11 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lint-nominated Create an FCP-thread on Zulip for this PR needs-fcp PRs that add, remove, or rename lints and need an FCP

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants