add new lint: for_unbounded_range#16257
Conversation
cd100aa to
9220570
Compare
|
r? clippy |
This comment has been minimized.
This comment has been minimized.
2927a7d to
7572e5f
Compare
This comment has been minimized.
This comment has been minimized.
| |diag| { | ||
| diag.span_suggestion_verbose( | ||
| arg.span.shrink_to_hi(), | ||
| "for loops over unbounded ranges will wrap around, consider using `start..=MAX` instead", |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
changed it to "for loops over unbounded ranges will wrap around and may panic, consider using start..=MAX instead"
|
Reminder, once the PR becomes ready for a review, use |
I have implemented this using the 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. |
|
@rustbot ready |
|
@samueltardieu Do you have time to give it another look? |
There was a problem hiding this comment.
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.
|
This lint has been nominated for inclusion. |
d6df932 to
17fdc88
Compare
This comment has been minimized.
This comment has been minimized.
17fdc88 to
ded35ce
Compare
|
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. |
|
@rustbot ready |
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.
I think that should be the rest, I rebased it as well to make sure everything was up to date. |
ded35ce to
8fb4470
Compare
Done |
8fb4470 to
4858a73
Compare
|
@rustbot ready |
|
Lintcheck changes for 4858a73
This comment will be updated if you push new changes |
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:
AsciiChar,Ipv4AddrandIpv6Addr? (Currently these will always panic when running over the max value)changelog: new lint: [
for_unbounded_range]