1
Fork 0

code refactor and fix wrong suggestion

This commit is contained in:
yukang 2023-05-08 14:52:52 +08:00
parent a7fc32ceaf
commit 5e94b5faf1
4 changed files with 51 additions and 24 deletions

View file

@ -399,29 +399,6 @@ impl<'a> Parser<'a> {
}
}
}
// we suggest add the missing `let` before the identifier
// `a: Ty = 1` -> `let a: Ty = 1`
if self.token == token::Colon {
let prev_span = self.prev_token.span.shrink_to_lo();
let snapshot = self.create_snapshot_for_diagnostic();
self.bump();
match self.parse_ty() {
Ok(_) => {
if self.token == token::Eq {
err.span_suggestion_verbose(
prev_span,
"you might have meant to introduce a new binding",
"let ".to_string(),
Applicability::MaybeIncorrect,
);
}
}
Err(err) => {
err.cancel();
}
}
self.restore_snapshot(snapshot);
}
if let Some(recovered_ident) = recovered_ident && recover {
err.emit();
@ -1029,6 +1006,35 @@ impl<'a> Parser<'a> {
Err(e)
}
/// Suggest add the missing `let` before the identifier in stmt
/// `a: Ty = 1` -> `let a: Ty = 1`
pub(super) fn suggest_add_missing_let_for_stmt(
&mut self,
err: &mut DiagnosticBuilder<'a, ErrorGuaranteed>,
) {
if self.token == token::Colon {
let prev_span = self.prev_token.span.shrink_to_lo();
let snapshot = self.create_snapshot_for_diagnostic();
self.bump();
match self.parse_ty() {
Ok(_) => {
if self.token == token::Eq {
err.span_suggestion_verbose(
prev_span,
"you might have meant to introduce a new binding",
"let ".to_string(),
Applicability::MaybeIncorrect,
);
}
}
Err(e) => {
e.cancel();
}
}
self.restore_snapshot(snapshot);
}
}
/// Check to see if a pair of chained operators looks like an attempt at chained comparison,
/// e.g. `1 < x <= 3`. If so, suggest either splitting the comparison into two, or
/// parenthesising the leftmost comparison.

View file

@ -99,7 +99,13 @@ impl<'a> Parser<'a> {
ForceCollect::Yes => {
self.collect_tokens_no_attrs(|this| this.parse_stmt_path_start(lo, attrs))?
}
ForceCollect::No => self.parse_stmt_path_start(lo, attrs)?,
ForceCollect::No => match self.parse_stmt_path_start(lo, attrs) {
Ok(stmt) => stmt,
Err(mut err) => {
self.suggest_add_missing_let_for_stmt(&mut err);
return Err(err);
}
},
}
} else if let Some(item) = self.parse_item_common(
attrs.clone(),