@@ -8,7 +8,9 @@ use crate::errors::{
88 TopLevelOrPatternNotAllowed , TopLevelOrPatternNotAllowedSugg , TrailingVertNotAllowed ,
99 UnexpectedExpressionInPattern , UnexpectedExpressionInPatternArmSugg ,
1010 UnexpectedExpressionInPatternConstSugg , UnexpectedExpressionInPatternInlineConstSugg ,
11- UnexpectedLifetimeInPattern , UnexpectedParenInRangePat , UnexpectedParenInRangePatSugg ,
11+ UnexpectedExpressionInPatternRemoveLetSugg ,
12+ UnexpectedExpressionInPatternReplaceLetElseWithIfSugg , UnexpectedLifetimeInPattern ,
13+ UnexpectedParenInRangePat , UnexpectedParenInRangePatSugg ,
1214 UnexpectedVertVertBeforeFunctionParam , UnexpectedVertVertInPattern ,
1315} ;
1416use crate :: parser:: expr:: { could_be_unclosed_char_literal, DestructuredFloat , LhsExpr } ;
@@ -26,7 +28,7 @@ use rustc_errors::{Applicability, Diag, PResult, StashKey};
2628use rustc_session:: errors:: ExprParenthesesNeeded ;
2729use rustc_span:: source_map:: { respan, Spanned } ;
2830use rustc_span:: symbol:: { kw, sym, Ident } ;
29- use rustc_span:: { ErrorGuaranteed , Span } ;
31+ use rustc_span:: { BytePos , ErrorGuaranteed , Span } ;
3032use thin_vec:: { thin_vec, ThinVec } ;
3133
3234#[ derive( PartialEq , Copy , Clone ) ]
@@ -497,15 +499,38 @@ impl<'a> Parser<'a> {
497499 err. span . replace ( stash_span, expr_span) ;
498500
499501 if let StmtKind :: Let ( local) = & stmt. kind {
500- // If we have an `ExprInPat`, the user tried to assign a value to another value,
501- // which doesn't makes much sense.
502502 match & local. kind {
503- LocalKind :: Decl => { }
504- LocalKind :: Init ( _) => { }
505- LocalKind :: InitElse ( _, _) => { }
503+ // help: remove this `let`
504+ LocalKind :: Decl | LocalKind :: Init ( _) => {
505+ err. subdiagnostic (
506+ UnexpectedExpressionInPatternRemoveLetSugg {
507+ // HACK: retrieves `let`'s span
508+ span : local
509+ . span
510+ . shrink_to_lo ( )
511+ . with_hi ( local. span . lo ( ) + BytePos ( 3 ) ) ,
512+ } ,
513+ ) ;
514+ }
515+ // help: replace the `let` with an `if`
516+ LocalKind :: InitElse ( init, els) => {
517+ err. subdiagnostic (
518+ UnexpectedExpressionInPatternReplaceLetElseWithIfSugg {
519+ span : local. span . shrink_to_lo ( ) . until ( els. span ) ,
520+ init : self
521+ . parser
522+ . span_to_snippet ( init. span )
523+ . unwrap ( ) ,
524+ pat : self
525+ . parser
526+ . span_to_snippet ( local. pat . span )
527+ . unwrap ( ) ,
528+ } ,
529+ ) ;
530+ }
506531 }
507532 } else {
508- // help: use an arm guard `if val == expr`
533+ // help: check the value in an arm guard
509534 if let Some ( arm) = & self . arm {
510535 let ( ident, ident_span) = match self . field {
511536 Some ( field) => (
0 commit comments