-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Warn when iterating an array of ranges #7125
Comments
nit: you can't actually annotate a type for the pattern in a |
i guess this should only lint on arrays of len 1? what about code like this:
|
Shouldn't that one be caught by |
Looking into that, it catches |
I'll extend |
extend `single_element_loop` to match `.iter()` This extends `single_element_loop` to also match `[..].iter()` in the loop argument. Related to #7125, but not completely fixing it due to the lint only firing if the array expression contains a local variable. --- changelog: none
extend `single_element_loop` to match `.iter()` This extends `single_element_loop` to also match `[..].iter()` in the loop argument. Related to #7125, but not completely fixing it due to the lint only firing if the array expression contains a local variable. --- changelog: none
|
Yes, but that appears to only work if the element is an |
It appears that, actually, |
We probably still want to cover the range case specially at some point, to at least give a better diagnostic. |
Hi, @ThinkerDreamer and myself worked on a pull request #11862 that lints the array itself, if it contains exactly one range. In this case, the lint suggests that the user wanted to iterate over the single range instead and suggests to rewrite the for loop accordingly. |
…er-range, r=llogiq suggest alternatives to iterate an array of ranges works towards #7125 changelog: [`single_element_loop`]: suggest better syntax when iterating over an array of a single range `@thinkerdreamer` and myself worked on this issue during a workshop by `@llogiq` at the RustLab 2023 conference. It is our first contribution to clippy. When iterating over an array of only one element, _which is a range_, our change suggests to replace the array with the contained range itself. Additionally, a hint is printed stating that the user probably intended to iterate over the range and not the array. If the single element in the array is not a range, the previous suggestion in the form of `let {pat_snip} = {prefix}{arg_snip};{block_str}`is used. This change lints the array with the single range directly, so any prefixes or suffixes are covered as well.
I think we can call this closed by #11862. |
What it does
Warns users if they accidentally iterate range struct items, rather than iterating through the indexes in the range:
Suggests:
This lint will become more important once
rustc
allows iterating arrays by value, because that allows code like:For context, see rust-lang/rust#84147 (comment)
Categories (optional)
clippy::correctness
What is the advantage of the recommended code over the original code
The recommended code either:
Drawbacks
Expressing iterating through an array of ranges becomes more verbose. But this disambiguation makes the intent of the code much clearer.
Example
Is probably intended to be:
And if range iteration is genuinely intended, it could be disambiguated as:
The text was updated successfully, but these errors were encountered: