Skip to content

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

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

vishnupoddar12 wants to merge 1 commit into
rust-lang:mainfrom
vishnupoddar12:issue-153667-fix

Conversation

@vishnupoddar12

@vishnupoddar12 vishnupoddar12 commented Mar 11, 2026 •

Copy link
Copy Markdown
Contributor

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

This comment has been minimized.

@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

@estebank estebank left a comment •

Copy link
Copy Markdown
Contributor

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

vishnupoddar12 commented Mar 13, 2026 •

Copy link
Copy Markdown
Contributor Author

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.

@vishnupoddar12

vishnupoddar12 commented Apr 13, 2026 •

Copy link
Copy Markdown
Contributor Author

@estebank I suppose this is very low priority bug causing the delay. If not, I just want to understand how to proceed on this PR.

@wesleywiser

Copy link
Copy Markdown
Member

Hi @estebank, it looks like this PR is ready for another round of review when you get a chance. Thanks!

Comment on lines +166 to +183
// Between receiver and method name (the part containing the dot)
let dot_part_span = rcvr_expr.span.between(span);
let dot_snippet = self.tcx.sess.source_map().span_to_snippet(dot_part_span).ok()?;
let dot_offset = Self::find_last_real_dot(&dot_snippet)?;

let replacement_span = sugg_span
.with_lo(dot_part_span.lo() + rustc_span::BytePos(u32::try_from(dot_offset).ok()?));

// A comment in the section being removed risks deleting user documentation,
// so we downgrade applicability to signal that human review is warranted.
let deleted_snippet = self.tcx.sess.source_map().span_to_snippet(replacement_span).ok()?;
let applicability = if deleted_snippet.contains("//") || deleted_snippet.contains("/*") {
Applicability::MaybeIncorrect
} else {
Applicability::MachineApplicable
};

Some((replacement_span, applicability))

@cjgillot cjgillot May 30, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Like @estebank suggested, this can be implemented as a one-liner using rcvr_expr.span and sugg_span. span_to_snippet is almost never a good solution.

View changes since the review

@cjgillot cjgillot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 30, 2026
@vishnupoddar12 vishnupoddar12 changed the title hir_typeck: suggest removing redundant .iter() and iter_mut ()calls hir_typeck: suggest removing redundant .iter() and iter_mut() calls Jun 5, 2026
@rust-bors

rust-bors Bot commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #163439) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

This branch has not been deployed

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

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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

6 participants