Skip to content

clippy_lints: support iter_mut in ITER_NEXT_SLICE fixes #17115#17122

Merged
samueltardieu merged 1 commit into
rust-lang:masterfrom
durationextender:master
Jun 3, 2026
Merged

clippy_lints: support iter_mut in ITER_NEXT_SLICE fixes #17115#17122
samueltardieu merged 1 commit into
rust-lang:masterfrom
durationextender:master

Conversation

@durationextender

@durationextender durationextender commented May 31, 2026

Copy link
Copy Markdown
Contributor

View all comments

changelog: [`iter_next_slice`]: Extend lint to support `iter_mut()`

fixes #17115

  • Followed [lint naming conventions][lint_naming]
  • Added passing UI tests (including committed .stderr file)
  • cargo test passes locally
  • Executed cargo dev update_lints
  • Added lint documentation
  • Run cargo dev fmt

changelog: This PR extends the iter_next_slice lint to also detect and suggest replacements for .iter_mut().next() calls on slices and arrays, providing consistent behavior for mutable iterators.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 31, 2026
@rustbot

rustbot commented May 31, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @llogiq (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 8 candidates
  • 8 candidates expanded to 8 candidates
  • Random selection from Jarcho, ada4a, dswij, llogiq, samueltardieu

@samueltardieu samueltardieu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The suggestions are broken.

View changes since this review

ITER_NEXT_SLICE,
expr.span,
"using `.iter().next()` on a Slice without end index",
"using `.{method}.next()` on a Slice without end index",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Formatting missing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

True, but it wasnt there to begin with so i didnt wanna add that, but ill add it

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What do you mean? Of course there was no format!(), as iter() was hardcoded, but this is no longer the case after your change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

oh yeah ur talking since we try to load method dynamically we need to use format! ? sorry its my first time contributing to clippy

"try calling",
format!(
"{}.first()",
"{}.{method}()",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are you suggesting calling {method} instead of {method}.next()?

@durationextender durationextender Jun 2, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

nope ur right should be .next() i will change it. expect a commit in the coming 15 mins addressing everything hopefully

Comment thread clippy_lints/src/methods/mod.rs Outdated
Comment on lines +5591 to +5592
(sym::iter, []) => iter_next_slice::check(cx, expr, recv2, false),
(sym::iter_mut, []) => iter_next_slice::check(cx, expr, recv2, true),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Pass the method name instead and let the function distinguish between both cases.

Suggested change
(sym::iter, []) => iter_next_slice::check(cx, expr, recv2, false),
(sym::iter_mut, []) => iter_next_slice::check(cx, expr, recv2, true),
(sym::iter | sym::iter_mut, []) => iter_next_slice::check(cx, expr, recv2, name),

Comment thread tests/ui/iter_next_slice.fixed Outdated
let v = vec![1, 2, 3];

let _ = s.first();
let _ = s.iter();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This makes no sense, you are suggesting using .iter() instead of .iter().next().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yeah thats my bad that was some old i forgot to change

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jun 1, 2026
@rustbot

rustbot commented Jun 1, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@durationextender

Copy link
Copy Markdown
Contributor Author

@rustbot ready

i pushed the changes u wanted @samueltardieu

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jun 2, 2026
Comment thread clippy_lints/src/methods/mod.rs Outdated
Comment thread tests/ui/iter_next_slice.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jun 2, 2026
let _: Option<&mut i32> = s_mut.iter_mut().next();
//~^ iter_next_slice

let _ = v_mut.iter_mut().next();

@samueltardieu samueltardieu Jun 3, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please add a type here as well.

View changes since the review

@samueltardieu

Copy link
Copy Markdown
Member

When you're done, could you please squash the commits together?

@rustbot

This comment has been minimized.

@durationextender

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@samueltardieu i think its good now. Please have a look

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jun 3, 2026
@samueltardieu

samueltardieu commented Jun 3, 2026

Copy link
Copy Markdown
Member

i think its good now. Please have a look

I am a bit surprised, tests fail, which means that you haven't run them locally.

@samueltardieu samueltardieu added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jun 3, 2026
@durationextender

Copy link
Copy Markdown
Contributor Author

@rustbot ready

the tests pass now

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jun 3, 2026
@samueltardieu

Copy link
Copy Markdown
Member

But commits are not squashed anymore.
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jun 3, 2026
@rustbot

This comment has been minimized.

@durationextender

Copy link
Copy Markdown
Contributor Author

@rustbot ready

squashed commit

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jun 3, 2026

@samueltardieu samueltardieu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The description of the lint should be updated to include .iter_mut() as well.

View changes since this review

parent_expr_opt = get_parent_expr(cx, parent_expr);
}

let method = method_name.as_str();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do you need to transform it into a &str? This is inefficient as it requires a lock from the interner.

ITER_NEXT_SLICE,
expr.span,
"using `.iter().next()` on a Slice without end index",
format!("using `.{method}.next()` on a Slice without end index"),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
format!("using `.{method}.next()` on a Slice without end index"),
format!("using `.{method_name}.next()` on a Slice without end index"),

ITER_NEXT_SLICE,
expr.span,
"using `.iter().next()` on an array",
format!("using `.{method}.next()` on an array"),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
format!("using `.{method}.next()` on an array"),
format!("using `.{method_name}.next()` on an array"),

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jun 3, 2026
@durationextender

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jun 3, 2026
@samueltardieu

Copy link
Copy Markdown
Member

The description of the lint should be updated to include .iter_mut() as well.

You don't seem to have modified the lint description.

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jun 3, 2026
@durationextender

Copy link
Copy Markdown
Contributor Author

updated the lint description

@samueltardieu

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jun 3, 2026

@samueltardieu samueltardieu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@samueltardieu samueltardieu enabled auto-merge June 3, 2026 20:07
@durationextender

Copy link
Copy Markdown
Contributor Author

thanks alot for ur patience. Certainly learned alot

@samueltardieu samueltardieu added this pull request to the merge queue Jun 3, 2026
Merged via the queue into rust-lang:master with commit e676765 Jun 3, 2026
11 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extend iter_next_slice to also cover mut

4 participants