-
Notifications
You must be signed in to change notification settings - Fork 1.8k
map_identity: suggest making the variable mutable when necessary
#15268
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
Conversation
cb16cf2 to
4ae3029
Compare
map_identity: suggest making the variable mutable when necessarymap_identity: suggest making the variable mutable when necessary
map_identity: suggest making the variable mutable when necessarymap_identity: suggest making the variable mutable when necessary
|
@y21 friendly ping |
4ae3029 to
806ce8d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, I have one suggestion that could possibly make one more case autofixable
I think having a method chained immediately after the .map() is the only case which requires adding mut, so it should be safe to say that method_requiring_mut is always Some. I'd like to do a lintcheck run to see if that's actually true.
&mut iter.map(...) and let ref mut x = iter.map(..) are some other cases that would need it, though IMO not really worth exhaustively handling all other arbitrary ways since this is just for a specialized note. So that note seems fine to me.
ac75ec3 to
e6c4977
Compare
As suggested in #15268 (comment) WIP because: - [x] what should be done with the now-error-pattern-less `tests/ui/double_ended_iterator_last_unfixable.rs`? - [x] is the change in behaviour of `double_ended_iterator_last` okay in general? - cc @samueltardieu because this changes the code you added in #14140 changelog: none r? @y21
e6c4977 to
b4065d1
Compare
map_identity: suggest making the variable mutable when necessarymap_identity: suggest making the variable mutable when necessary
|
I've added some intermediary variables, which will hopefully make the logic a bit more clear, but made them lazy-evaluated just in case. Do let me know if the latter (or the former, for that matter) is unnecessary |
4c8c8ad to
a36c68d
Compare
|
Okay so I did a bigger refactor. Everything's a bit more wordy now, but I think that's necessary to represent all the complexity |
|
..or maybe not. I've made another branch where I just conditionally modify branch: https://github.com/ada4a/rust-clippy/tree/map-identity-sugg-on-mut-2 Let me know which of these approaches you prefer. Sorry for being a bit hectic^^ |
|
Having thought a little more about this, I realized that there is technically a behavioral change here for That said though,
This does look like a slight improvement to me, so I'd be fine with going with that. |
|
I agree that copy iterators are an antipattern. Downgrading the suggestion does sound like a good option. |
There's one problem with that: currently,
then we would now downgrade to I also tried to do it in the verbose version (from this PR), and it did work out a bit more nicely imo? I'll push commits to both branches, see which of them you like more |
huh! |
902c033 to
0f72c02
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This version looks all good to me now, can you squash the commits? Thanks!
0f72c02 to
61ce0c5
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. |
|
Thank you for all the help:) Squashed (and rebased) |
While working on #15229, I noticed a test case in
map_identitywhich avoids linting in the case where removing the.map()would require the iterator variable to be made mutable. But then I saw #14140, and thought I'd try to adapt its approach, by suggesting both removing the.map()and making the variable mutable.This is WIP only because I'm not sure about the very last
diag.span_note-- I think having a method chained immediately after the.map()is the only case which requires addingmut, so it should be safe to say thatmethod_requiring_mutis alwaysSome. I'd like to do a lintcheck run to see if that's actually true.@samueltardieu do you think this is a good approach? Maybe there are more lints where we could lint + suggest making the variable mut, instead of not linting?
changelog:
map_identity: suggest making the variable mutable when necessary