Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upRange::step_by(0) loops forever #26239
Comments
This comment has been minimized.
This comment has been minimized.
|
There is nothing unsafe about looping forever, so I strongly oppose a |
This comment has been minimized.
This comment has been minimized.
|
@Thiez: Agreed, this is not inherently unsafe. So maybe It may also be useful to be able to issue a warning on debug builds iff a `StepBy { step_by: 0, .. } is actually iterated; perhaps as an additional Debug option. |
This comment has been minimized.
This comment has been minimized.
|
This is currently intended behavior, and for now I'm going to close this in favor of the discussion in stabilizing |
llogiq commentedJun 12, 2015
I believe that the step_by(_) method of ranges should
panic!if the argument is zero. Currently it loops forever, which is a footgun. At the very least, iterating over such a StepBy shouldpanic!on debug builds. However, I think the earlier the error appears, the better.Note that this may require us to specialize
step_byto types whoseAddimplementation have a known identity element – at the moment that would mean bounding by thenum::Zerotrait, which is unstable, also it would probably reduce the usefulness of ranges to numeric types. However, special casing numeric ranges could solve this.I have also filed an issue at rust-clippy to create a lint for cases where it can be statically detected. Still, that lint cannot detect cases where the argument is calculated at runtime.
The documentation of
.step_by(_)should at least warn of the possibility of looping forever or panic once it is implemented.