-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
base: master
Are you sure you want to change the base?
Conversation
|| self | ||
.get_res_span(expr) | ||
.is_some_and(|span| !self.suggest_span.contains(span)) |
There was a problem hiding this comment.
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.
|| 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))) |
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 | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
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.
// 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 |
There was a problem hiding this comment.
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:
// 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)
r? @samueltardieu |
Reminder, once the PR becomes ready for a review, use |
Fixes: #14931
changelog: Fix [
manual_swap
]'s suggestion-causes-error when the variable is mutable or as loop variable.