Skip to content

hir_typeck: suggest removing redundant .iter() and iter_mut ()calls#153700

Open
vishnupoddar12 wants to merge 1 commit intorust-lang:mainfrom
vishnupoddar12:issue-153667-fix
Open

hir_typeck: suggest removing redundant .iter() and iter_mut ()calls#153700
vishnupoddar12 wants to merge 1 commit intorust-lang:mainfrom
vishnupoddar12:issue-153667-fix

Conversation

@vishnupoddar12
Copy link
Copy Markdown
Contributor

@vishnupoddar12 vishnupoddar12 commented Mar 11, 2026

This diagnostic intercepts cases where .iter() or .iter_mut() is called on a type that already implements Iterator

Instead of confusing trait fallback suggestions, it explicitly guides the user to remove the .iter() call. It implements precise span computation to support cargo fix while carefully preserving all inline comments and horizontal whitespace during the AST string replacement.

Fixes #153667

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 11, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 11, 2026

r? @fmease

rustbot has assigned @fmease.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 69 candidates
  • Random selection from 16 candidates

@vishnupoddar12 vishnupoddar12 force-pushed the issue-153667-fix branch 5 times, most recently from 8f0debd to 7dc8033 Compare March 11, 2026 19:59
@vishnupoddar12 vishnupoddar12 changed the title hir_typeck: suggest removing redundant .iter() calls hir_typeck: suggest removing redundant .iter() and iter_mut ()calls Mar 11, 2026
Copy link
Copy Markdown
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

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

You're doing a lot of Span wrangling and dealing with code snippets to try and create a suitable Span for the suggestion, but instead you should be leveraging the hir Exprs to find the right spans. you can do things like rcvr.span.between(arg[0].span) to construct a span that points at

rcvr.method(arg0, arg1);
    ^^^^^^^^

View changes since this review

This diagnostic intercepts cases where `.iter()` is called on a type
that already implements `Iterator`

Instead of confusing trait fallback suggestions, it explicitly guides
the user to remove the `.iter()` call. It implements precise span
computation to support `cargo fix` while carefully preserving all inline
comments and horizontal whitespace during the AST string replacement.
@vishnupoddar12
Copy link
Copy Markdown
Contributor Author

vishnupoddar12 commented Mar 13, 2026

Thanks @estebank, I was able to reduce lot of unnecessary span wrangling reducing code change by 30-40 lines. It should be cleaner now.

@fmease fmease assigned estebank and unassigned fmease Mar 15, 2026
@vishnupoddar12
Copy link
Copy Markdown
Contributor Author

Hi @estebank, Friendly ping! I updated the PR to use rcvr.span.between() based on last comment. Let me know when you have a chance to take a look, or if there's anything else you'd like me to tweak.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When encountering .iter() or .iter_mut() on an impl Iterator, suggest removing the method call

4 participants