-
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
.last()
to .next_back()
requires a mutable receiver
#14140
base: master
Are you sure you want to change the base?
Conversation
Commits logically separated for easier review. |
5dfa493
to
fb033f1
Compare
Rebased |
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.
Just one thing: I suppose we shouldn't just change it to be mutable in the suggestion? I'm not sure if there'd be any case that doesn't work/causes an error considering .last()
will already consume it.
Good question, let me try that, I'll switch to @rustbot author in the meantime. On an unrelated topic, I also realized that this lint should probably not be |
In the case where `iter` is a `DoubleEndedIterator`, replacing a call to `iter.last()` (which consumes `iter`) by `iter.next_back()` (which requires a mutable reference to `iter`) cannot be done when `iter` Is not a mutable binding or a mutable reference. When `iter` is a local binding, it can be made mutable by fixing its definition site.
`iter.last()` will drop all elements of `iter` in order, while `iter.next_back()` will drop the non-last elements of `iter` when `iter` goes out of scope since `.next_back()` does not consume its argument. When the transformation proposed by `double_ended_iterator_last` would concern an iterator whose element type has a significant drop, a note is added to warn about the possible drop order change, and the suggestion is switched from `MachineApplicable` to `MaybeIncorrect`.
fb033f1
to
dd54d79
Compare
In the case where
iter
is aDoubleEndedIterator
, replacing a call toiter.last()
(which consumesiter
) byiter.next_back()
(which requires a mutable reference toiter
) cannot be done wheniter
is a non-mutable binding which is not a mutable reference. When possible, a local immutable binding is made into a mutable one.Also, the applicability is switched to
MaybeIncorrect
and a note is added to the output when the element types have a significant drop, because the drop order will potentially be modified because.next_back()
does not consume the iterator nor the elements before the last one.Fix #14139
changelog: [
double_ended_iterator_last
]: do not trigger on non-reference immutable receiver, and warn about possible drop order change