@@ -13,7 +13,7 @@ use crate::errors::{
13
13
IncorrectUseOfAwait , ParenthesesInForHead , ParenthesesInForHeadSugg ,
14
14
PatternMethodParamWithoutBody , QuestionMarkInType , QuestionMarkInTypeSugg , SelfParamNotFirst ,
15
15
StructLiteralBodyWithoutPath , StructLiteralBodyWithoutPathSugg , StructLiteralNeedingParens ,
16
- StructLiteralNeedingParensSugg , SuggEscapeIdentifier , SuggRemoveComma ,
16
+ StructLiteralNeedingParensSugg , SuggAddMissingLetStmt , SuggEscapeIdentifier , SuggRemoveComma ,
17
17
UnexpectedConstInGenericParam , UnexpectedConstParamDeclaration ,
18
18
UnexpectedConstParamDeclarationSugg , UnmatchedAngleBrackets , UseEqInstead ,
19
19
} ;
@@ -32,8 +32,8 @@ use rustc_ast::{
32
32
use rustc_ast_pretty:: pprust;
33
33
use rustc_data_structures:: fx:: FxHashSet ;
34
34
use 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 ,
37
37
} ;
38
38
use rustc_session:: errors:: ExprParenthesesNeeded ;
39
39
use rustc_span:: source_map:: Spanned ;
@@ -1006,6 +1006,31 @@ impl<'a> Parser<'a> {
1006
1006
Err ( e)
1007
1007
}
1008
1008
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
+
1009
1034
/// Check to see if a pair of chained operators looks like an attempt at chained comparison,
1010
1035
/// e.g. `1 < x <= 3`. If so, suggest either splitting the comparison into two, or
1011
1036
/// parenthesising the leftmost comparison.
0 commit comments