`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`.
23 lines
687 B
Rust
23 lines
687 B
Rust
//@no-rustfix
|
|
#![warn(clippy::double_ended_iterator_last)]
|
|
|
|
fn main() {
|
|
let mut index = [true, true, false, false, false, true].iter();
|
|
let subindex = (index.by_ref().take(3), 42);
|
|
let _ = subindex.0.last(); //~ ERROR: called `Iterator::last` on a `DoubleEndedIterator`
|
|
}
|
|
|
|
fn drop_order() {
|
|
struct S(&'static str);
|
|
impl std::ops::Drop for S {
|
|
fn drop(&mut self) {
|
|
println!("Dropping {}", self.0);
|
|
}
|
|
}
|
|
|
|
let v = vec![S("one"), S("two"), S("three")];
|
|
let v = (v.into_iter(), 42);
|
|
println!("Last element is {}", v.0.last().unwrap().0);
|
|
//~^ ERROR: called `Iterator::last` on a `DoubleEndedIterator`
|
|
println!("Done");
|
|
}
|