`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`.
82 lines
2.9 KiB
Text
82 lines
2.9 KiB
Text
error: called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator
|
|
--> tests/ui/double_ended_iterator_last.rs:5:5
|
|
|
|
|
LL | s.split(' ').last()
|
|
| ^^^^^^^^^^^^^------
|
|
| |
|
|
| help: try: `next_back()`
|
|
|
|
|
= note: `-D clippy::double-ended-iterator-last` implied by `-D warnings`
|
|
= help: to override `-D warnings` add `#[allow(clippy::double_ended_iterator_last)]`
|
|
|
|
error: called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator
|
|
--> tests/ui/double_ended_iterator_last.rs:22:13
|
|
|
|
|
LL | let _ = DeIterator.last();
|
|
| ^^^^^^^^^^^------
|
|
| |
|
|
| help: try: `next_back()`
|
|
|
|
error: called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator
|
|
--> tests/ui/double_ended_iterator_last.rs:58:13
|
|
|
|
|
LL | let _ = subindex.last();
|
|
| ^^^^^^^^^^^^^^^
|
|
|
|
|
help: try
|
|
|
|
|
LL ~ let mut subindex = index.by_ref().take(3);
|
|
LL ~ let _ = subindex.next_back();
|
|
|
|
|
|
|
error: called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator
|
|
--> tests/ui/double_ended_iterator_last.rs:62:13
|
|
|
|
|
LL | let _ = subindex.last();
|
|
| ^^^^^^^^^------
|
|
| |
|
|
| help: try: `next_back()`
|
|
|
|
error: called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator
|
|
--> tests/ui/double_ended_iterator_last.rs:67:13
|
|
|
|
|
LL | let _ = subindex.last();
|
|
| ^^^^^^^^^------
|
|
| |
|
|
| help: try: `next_back()`
|
|
|
|
error: called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator
|
|
--> tests/ui/double_ended_iterator_last.rs:72:13
|
|
|
|
|
LL | let _ = subindex.last();
|
|
| ^^^^^^^^^------
|
|
| |
|
|
| help: try: `next_back()`
|
|
|
|
error: called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator
|
|
--> tests/ui/double_ended_iterator_last.rs:76:13
|
|
|
|
|
LL | let _ = subindex.last();
|
|
| ^^^^^^^^^^^^^^^
|
|
|
|
|
help: try
|
|
|
|
|
LL ~ let (mut subindex, _) = (index.by_ref().take(3), 42);
|
|
LL ~ let _ = subindex.next_back();
|
|
|
|
|
|
|
error: called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator
|
|
--> tests/ui/double_ended_iterator_last.rs:89:36
|
|
|
|
|
LL | println!("Last element is {}", v.last().unwrap().0);
|
|
| ^^^^^^^^
|
|
|
|
|
= note: this change will alter drop order which may be undesirable
|
|
help: try
|
|
|
|
|
LL ~ let mut v = v.into_iter();
|
|
LL ~ println!("Last element is {}", v.next_back().unwrap().0);
|
|
|
|
|
|
|
error: aborting due to 8 previous errors
|
|
|