-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ACP: Avoid the common mistake of for x in 0u8..
#304
Comments
As I understand it, the behavior where In order to prevent the overflow and stop at the max value, we would need to implement a flag similar to So the real solution to this is the same solution to the |
All that said, it would be good to have a lint for this. Please open a clippy issue for that. |
There's nothing particularly wrong with them if you expect to find a value in that range and then |
Well, one problem is that the iterator can only ever yield So the lint can actually just recommend |
As another potential motivation, a bug was introduced to the The behavior is confusing to explain. If someone writes the non-panicking fn panic() {
let x = (0..).zip([0; 256].iter()).collect::<Vec<(u8, &u8)>>();
println!("{}", x.len());
}
fn no_panic() {
let mut sum = 0u32;
for (a, _b) in (0..).zip([0; 256].iter()) {
sum += a;
}
println!("{sum}");
} Personally, instead of an open-ended range iterator I almost always want edit: Sorry if this is off-topic. I realize my post is more about the hazards of using open-ended ranges than the specific |
That's very surprising, to say the least. Thank you for reporting it!
Don't worry. I would dare to say |
It won't "panic" if you turn off |
Correct! The example I gave was bad. But my intention/opinion is the same: the code is a potential footgun, no matter the int size (although |
Proposal
Problem statement
When a type has finite possible values and total ordering, it is tempting to write this code:
for x in SOME_VAL.. {}
However this is plain incorrect... It panics with overflow in debug mode and loops infinitely in release mode, for integers.
Linting against this pattern is helpful, but it's more meaningful to have a correct fix for the expected behavior.
Motivating examples or use cases
Integers and
char
types(see rust-lang #111514) and many other types might benefit from this.Solution sketch
Maybe add a
to_inclusive()
api to open-ended ranges types, such that:(0u8..).to_inclusive()
=>(0u8..=255u8)
Alternatives
TO BE ADDED.
Links and related work
char
open ended range isn't bounded bychar::MAX
rust#111514What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: