@@ -13,7 +13,7 @@ use crate::errors::{
1313 IncorrectUseOfAwait , ParenthesesInForHead , ParenthesesInForHeadSugg ,
1414 PatternMethodParamWithoutBody , QuestionMarkInType , QuestionMarkInTypeSugg , SelfParamNotFirst ,
1515 StructLiteralBodyWithoutPath , StructLiteralBodyWithoutPathSugg , StructLiteralNeedingParens ,
16- StructLiteralNeedingParensSugg , SuggEscapeIdentifier , SuggRemoveComma ,
16+ StructLiteralNeedingParensSugg , SuggAddMissingLetStmt , SuggEscapeIdentifier , SuggRemoveComma ,
1717 UnexpectedConstInGenericParam , UnexpectedConstParamDeclaration ,
1818 UnexpectedConstParamDeclarationSugg , UnmatchedAngleBrackets , UseEqInstead ,
1919} ;
@@ -32,8 +32,8 @@ use rustc_ast::{
3232use rustc_ast_pretty:: pprust;
3333use rustc_data_structures:: fx:: FxHashSet ;
3434use rustc_errors:: {
35- pluralize, Applicability , Diagnostic , DiagnosticBuilder , DiagnosticMessage , ErrorGuaranteed ,
36- FatalError , Handler , IntoDiagnostic , MultiSpan , PResult ,
35+ pluralize, AddToDiagnostic , Applicability , Diagnostic , DiagnosticBuilder , DiagnosticMessage ,
36+ ErrorGuaranteed , FatalError , Handler , IntoDiagnostic , MultiSpan , PResult ,
3737} ;
3838use rustc_session:: errors:: ExprParenthesesNeeded ;
3939use rustc_span:: source_map:: Spanned ;
@@ -1006,6 +1006,31 @@ impl<'a> Parser<'a> {
10061006 Err ( e)
10071007 }
10081008
1009+ /// Suggest add the missing `let` before the identifier in stmt
1010+ /// `a: Ty = 1` -> `let a: Ty = 1`
1011+ pub ( super ) fn suggest_add_missing_let_for_stmt (
1012+ & mut self ,
1013+ err : & mut DiagnosticBuilder < ' a , ErrorGuaranteed > ,
1014+ ) {
1015+ if self . token == token:: Colon {
1016+ let prev_span = self . prev_token . span . shrink_to_lo ( ) ;
1017+ let snapshot = self . create_snapshot_for_diagnostic ( ) ;
1018+ self . bump ( ) ;
1019+ match self . parse_ty ( ) {
1020+ Ok ( _) => {
1021+ if self . token == token:: Eq {
1022+ let sugg = SuggAddMissingLetStmt { span : prev_span } ;
1023+ sugg. add_to_diagnostic ( err) ;
1024+ }
1025+ }
1026+ Err ( e) => {
1027+ e. cancel ( ) ;
1028+ }
1029+ }
1030+ self . restore_snapshot ( snapshot) ;
1031+ }
1032+ }
1033+
10091034 /// Check to see if a pair of chained operators looks like an attempt at chained comparison,
10101035 /// e.g. `1 < x <= 3`. If so, suggest either splitting the comparison into two, or
10111036 /// parenthesising the leftmost comparison.
0 commit comments