Skip to content

Conversation

scrabsha
Copy link
Contributor

@scrabsha scrabsha commented Aug 15, 2025

Fixes #15487.

This lint triggers when calling .iter().last() on a slice or array and suggests using .last() directly.

This is a modified version of the iter_next_slice. I could not figure out how to avoid code duplication though :/.

changelog: Add iter_last_slice lint

Summary Notes

Managed by @rustbot—see help for details

@rustbot
Copy link
Collaborator

rustbot commented Aug 15, 2025

r? @y21

rustbot has assigned @y21.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 15, 2025
Copy link

github-actions bot commented Aug 15, 2025

No changes for 8ea1451

@rustbot rustbot added S-blocked Status: marked as blocked ❌ on something else such as an RFC or other implementation work A-lint Area: New lints and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Aug 15, 2025
This lint triggers when calling `.iter().last()` on a slice or array
and suggests using `.last()` directly.

This is a modified version of the `iter_next_slice`. I could not figure
out how to avoid code duplication though :/.
Copy link
Contributor

@ada4a ada4a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some drive-by comments..

iter_overeager_cloned::Op::LaterCloned,
false,
);
double_ended_iterator_last::check(cx, expr, recv, call_span);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why repeat this check in two match arms? I think leaving it outside the match is fine..


let _ = s.iter().last();
//~^ iter_last_slice
// Should be replaced by s.last()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these comments could be left out -- the .fixed file already captures the intended output

cx,
ITER_LAST_SLICE,
expr.span,
"using `.iter().last()` on a slice without end index",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"using `.iter().last()` on a slice without end index",
"using `.iter().last()` on a slice without start index",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints S-blocked Status: marked as blocked ❌ on something else such as an RFC or other implementation work
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lint against [T].iter().last() and suggest [T].last()
4 participants