Fix #104086, Tighten the 'introduce new binding' suggestion
This commit is contained in:
parent
bc2504a83c
commit
952df48948
3 changed files with 109 additions and 7 deletions
|
@ -1810,18 +1810,27 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
|||
false
|
||||
}
|
||||
|
||||
fn let_binding_suggestion(&self, err: &mut Diagnostic, ident_span: Span) -> bool {
|
||||
fn let_binding_suggestion(&mut self, err: &mut Diagnostic, ident_span: Span) -> bool {
|
||||
// try to give a suggestion for this pattern: `name = 1`, which is common in other languages
|
||||
let mut added_suggestion = false;
|
||||
if let Some(Expr { kind: ExprKind::Assign(lhs, _rhs, _), .. }) = self.diagnostic_metadata.in_assignment &&
|
||||
let ast::ExprKind::Path(None, _) = lhs.kind {
|
||||
if let Some(Expr { kind: ExprKind::Assign(lhs, rhs, _), .. }) =
|
||||
self.diagnostic_metadata.in_assignment
|
||||
{
|
||||
let is_rhs_assign = match rhs.kind {
|
||||
ExprKind::Assign(..) => true,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
if let ast::ExprKind::Path(None, _) = lhs.kind && !is_rhs_assign {
|
||||
let sm = self.r.session.source_map();
|
||||
let line_span = sm.span_extend_to_line(ident_span);
|
||||
let ident_name = sm.span_to_snippet(ident_span).unwrap();
|
||||
// HACK(chenyukang): make sure ident_name is at the starting of the line to protect against macros
|
||||
if sm
|
||||
.span_to_snippet(line_span)
|
||||
.map_or(false, |s| s.trim().starts_with(&ident_name))
|
||||
// HACK(chenyukang): make sure ident_name is at the starting of the line to protect against macros,
|
||||
// and avoid some special cases like `x = x = x`
|
||||
if let Ok(line) = sm.span_to_snippet(line_span) &&
|
||||
let stripped = line.split_whitespace().collect::<String>() &&
|
||||
stripped.trim().starts_with(&ident_name) &&
|
||||
stripped.matches(&format!("{}=", &ident_name)).count() == 1
|
||||
{
|
||||
err.span_suggestion_verbose(
|
||||
ident_span.shrink_to_lo(),
|
||||
|
@ -1832,6 +1841,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
|||
added_suggestion = true;
|
||||
}
|
||||
}
|
||||
self.diagnostic_metadata.in_assignment = None;
|
||||
}
|
||||
added_suggestion
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue