Skip to content

Fix suggestion-causes-error of manual_swap #14978

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

relaxcn
Copy link
Contributor

@relaxcn relaxcn commented Jun 4, 2025

Fixes: #14931

changelog: Fix [manual_swap]'s suggestion-causes-error when the variable is mutable or as loop variable.

@rustbot
Copy link
Collaborator

rustbot commented Jun 4, 2025

r? @dswij

rustbot has assigned @dswij.
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 Jun 4, 2025
Comment on lines +367 to +369
|| self
.get_res_span(expr)
.is_some_and(|span| !self.suggest_span.contains(span))
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think you need to introduce a new get_res_span() method.

Suggested change
|| self
.get_res_span(expr)
.is_some_and(|span| !self.suggest_span.contains(span))
|| path_to_local(expr)
.is_some_and(|hir_id| !self.suggest_span.contains(self.cx.tcx.hir_span(hir_id)))

Comment on lines +386 to +400
fn get_res_span(&self, expr: &'tcx Expr<'tcx>) -> Option<Span> {
if let ExprKind::Path(QPath::Resolved(
_,
Path {
res: rustc_hir::def::Res::Local(hir_id),
..
},
)) = expr.kind
{
Some(self.cx.tcx.hir_span(*hir_id))
} else {
None
}
}

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
fn get_res_span(&self, expr: &'tcx Expr<'tcx>) -> Option<Span> {
if let ExprKind::Path(QPath::Resolved(
_,
Path {
res: rustc_hir::def::Res::Local(hir_id),
..
},
)) = expr.kind
{
Some(self.cx.tcx.hir_span(*hir_id))
} else {
None
}
}

// 3. Variable initialization is inside the suggestion span and the variable is used as an
// index/elsewhere later, but its declaration is outside the suggestion span
if !self.suggest_span.contains(init.span)
|| !self.is_used_other_than_swapping(first_segment.ident)
Copy link
Contributor

Choose a reason for hiding this comment

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

This test seems more costly than the new one (declaration outside the suggestion span), maybe you should move it in last position.

Comment on lines +359 to +364
// We skip suggesting a variable binding in any of these cases:
// 1. Variable initialization is outside the suggestion span
// 2. Variable initialization is inside the suggestion span but the variable is not used as an index
// or elsewhere later
// 3. Variable initialization is inside the suggestion span and the variable is used as an
// index/elsewhere later, but its declaration is outside the suggestion span
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for adding documentation here. I would not repeat the negation of previous conditions, this seems simpler and is logically equivalent:

Suggested change
// We skip suggesting a variable binding in any of these cases:
// 1. Variable initialization is outside the suggestion span
// 2. Variable initialization is inside the suggestion span but the variable is not used as an index
// or elsewhere later
// 3. Variable initialization is inside the suggestion span and the variable is used as an
// index/elsewhere later, but its declaration is outside the suggestion span
// We skip suggesting a variable binding in any of these cases:
// - Variable initialization is outside the suggestion span
// - Variable declaration is outside the suggestion span
// - Variable is not used as an index or elsewhere later

(see below for the ordering)

@samueltardieu
Copy link
Contributor

r? @samueltardieu
@rustbot author

@rustbot rustbot assigned samueltardieu and unassigned dswij Jun 5, 2025
@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 5, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 5, 2025

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

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 from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FP manual_swap : mutability mismatch
4 participants