-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
explicit_iter_loop: x.iter_mut()
vs &mut *x
#11074
Comments
Another variant of this problem is with an immutable borrow where clippy suggests this: - for xi in x.iter() {}
+ for xi in &*x {} For us, in uutils (see uutils/coreutils#5204), this has popped up since Rust 1.72. We think the suggested code is less readable and harder to understand (especially for beginners). I think a good compromise would be to only suggest these changes if no |
# Objective [Rust 1.72.0](https://blog.rust-lang.org/2023/08/24/Rust-1.72.0.html) is now stable. # Notes - `let-else` formatting has arrived! - I chose to allow `explicit_iter_loop` due to rust-lang/rust-clippy#11074. We didn't hit any of the false positives that prevent compilation, but fixing this did produce a lot of the "symbol soup" mentioned, e.g. `for image in &mut *image_events {`. Happy to undo this if there's consensus the other way. --------- Co-authored-by: François <[email protected]>
Another compromise would be to split this into two lints |
Referencing this raised issue: rust-lang#11074 The lint adds too much visual noise when deref lints are enforced. This commit splits the explicit_iter_loop into two versions, the first, `std`, where non-deref types are linted, and the second `deref` where deref types are linted.
Created a PR (#11406) that would split these and resolve this issue if that's the desired behaviour. |
Though I like the idea, one problem with splitting it is that it would require that everyone's allow attributes would need to be changed. I think it's worth exploring how many people actually want the version with |
That's true, and one option for that is to then change |
I misunderstood the pedantic/style designation before. Updated my PR so both lints are active as pedantic, but can be separately allowed/disallowed. This would leave users having to update their allow attributes, but gives better granularity in the linting. |
You could add |
Awesome thanks, will do that! |
- I won't document panics. - `clippy::tuple-array-conversions` seems to have false positives. - `clippy::tuple-array-conversions` seems to have false positives and is not very nice yet. - `clippy::explicit-iter-loop` is currently only partly nice. See rust-lang/rust-clippy#11074
[Rust 1.72.0](https://blog.rust-lang.org/2023/08/24/Rust-1.72.0.html) is now stable. - `let-else` formatting has arrived! - I chose to allow `explicit_iter_loop` due to rust-lang/rust-clippy#11074. We didn't hit any of the false positives that prevent compilation, but fixing this did produce a lot of the "symbol soup" mentioned, e.g. `for image in &mut *image_events {`. Happy to undo this if there's consensus the other way. --------- Co-authored-by: François <[email protected]>
Add config flag for reborrows in explicit_iter_loop This PR adds a config flag for enforcing explicit into iter lint for reborrowed values. The config flag, `enforce_iter_loop_reborrow`, can be added to clippy.toml files to enable the linting behaviour. By default the reborrow lint is disabled. fixes: #11074 changelog: [`explicit_iter_loop`]: add config flag `enforce_iter_loop_reborrow` to disable reborrow linting by default
# Objective [Rust 1.72.0](https://blog.rust-lang.org/2023/08/24/Rust-1.72.0.html) is now stable. # Notes - `let-else` formatting has arrived! - I chose to allow `explicit_iter_loop` due to rust-lang/rust-clippy#11074. We didn't hit any of the false positives that prevent compilation, but fixing this did produce a lot of the "symbol soup" mentioned, e.g. `for image in &mut *image_events {`. Happy to undo this if there's consensus the other way. --------- Co-authored-by: François <[email protected]>
Summary
I have a hard time being confident that Clippy's suggestion in the following case constitutes an improvement to the code:
Could we revisit whether it is worth suggesting a change in this case?
In cases where the suggestion is
for xi in x {
, I am fully supportive of the lint. Obviouslyfor xi in x
is a clear improvement overfor xi in x.iter_mut()
(modulo the restriction from #8585 being turned on).I am also generally supportive of
for xi in &mut x
.But the case of
for xi in &mut *x
starts to be over the line for me in symbol soup.Lint Name
explicit_iter_loop
Reproducer
This is a case where neither
for xi in x {
norfor xi in &mut x {
compiles.Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: