Skip to content

Implement Iterator::last for vec::IntoIter #139773

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

Merged
merged 1 commit into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions library/alloc/src/vec/into_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
self.len()
}

#[inline]
fn last(mut self) -> Option<T> {
self.next_back()
}

#[inline]
fn next_chunk<const N: usize>(&mut self) -> Result<[T; N], core::array::IntoIter<T, N>> {
let mut raw_ary = [const { MaybeUninit::uninit() }; N];
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/sys/args/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ impl Iterator for Args {
}

#[inline]
fn last(mut self) -> Option<OsString> {
self.iter.next_back()
fn last(self) -> Option<OsString> {
self.iter.last()
}

#[inline]
Expand Down
15 changes: 14 additions & 1 deletion src/tools/clippy/tests/ui/double_ended_iterator_last.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ fn issue_14139() {
}

fn drop_order() {
struct DropDeIterator(std::vec::IntoIter<S>);
impl Iterator for DropDeIterator {
type Item = S;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
impl DoubleEndedIterator for DropDeIterator {
fn next_back(&mut self) -> Option<Self::Item> {
self.0.next_back()
}
}

struct S(&'static str);
impl std::ops::Drop for S {
fn drop(&mut self) {
Expand All @@ -92,7 +105,7 @@ fn drop_order() {
}

let v = vec![S("one"), S("two"), S("three")];
let mut v = v.into_iter();
let mut v = DropDeIterator(v.into_iter());
println!("Last element is {}", v.next_back().unwrap().0);
//~^ ERROR: called `Iterator::last` on a `DoubleEndedIterator`
println!("Done");
Expand Down
15 changes: 14 additions & 1 deletion src/tools/clippy/tests/ui/double_ended_iterator_last.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ fn issue_14139() {
}

fn drop_order() {
struct DropDeIterator(std::vec::IntoIter<S>);
impl Iterator for DropDeIterator {
type Item = S;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
impl DoubleEndedIterator for DropDeIterator {
fn next_back(&mut self) -> Option<Self::Item> {
self.0.next_back()
}
}

struct S(&'static str);
impl std::ops::Drop for S {
fn drop(&mut self) {
Expand All @@ -92,7 +105,7 @@ fn drop_order() {
}

let v = vec![S("one"), S("two"), S("three")];
let v = v.into_iter();
let v = DropDeIterator(v.into_iter());
println!("Last element is {}", v.last().unwrap().0);
//~^ ERROR: called `Iterator::last` on a `DoubleEndedIterator`
println!("Done");
Expand Down
4 changes: 2 additions & 2 deletions src/tools/clippy/tests/ui/double_ended_iterator_last.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ 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:96:36
--> tests/ui/double_ended_iterator_last.rs:109: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 ~ let mut v = DropDeIterator(v.into_iter());
LL ~ println!("Last element is {}", v.next_back().unwrap().0);
|

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ fn main() {
}

fn drop_order() {
struct DropDeIterator(std::vec::IntoIter<S>);
impl Iterator for DropDeIterator {
type Item = S;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
impl DoubleEndedIterator for DropDeIterator {
fn next_back(&mut self) -> Option<Self::Item> {
self.0.next_back()
}
}

struct S(&'static str);
impl std::ops::Drop for S {
fn drop(&mut self) {
Expand All @@ -19,7 +32,7 @@ fn drop_order() {
}

let v = vec![S("one"), S("two"), S("three")];
let v = (v.into_iter(), 42);
let v = (DropDeIterator(v.into_iter()), 42);
println!("Last element is {}", v.0.last().unwrap().0);
//~^ ERROR: called `Iterator::last` on a `DoubleEndedIterator`
println!("Done");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator
--> tests/ui/double_ended_iterator_last_unfixable.rs:23:36
--> tests/ui/double_ended_iterator_last_unfixable.rs:36:36
|
LL | println!("Last element is {}", v.0.last().unwrap().0);
| ^^^^------
Expand All @@ -8,7 +8,7 @@ LL | println!("Last element is {}", v.0.last().unwrap().0);
|
= note: this change will alter drop order which may be undesirable
note: this must be made mutable to use `.next_back()`
--> tests/ui/double_ended_iterator_last_unfixable.rs:23:36
--> tests/ui/double_ended_iterator_last_unfixable.rs:36:36
|
LL | println!("Last element is {}", v.0.last().unwrap().0);
| ^^^
Expand Down
Loading