@@ -2347,26 +2347,47 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
23472347 // try to give a suggestion for this pattern: `name = blah`, which is common in other languages
23482348 // suggest `let name = blah` to introduce a new binding
23492349 fn let_binding_suggestion ( & mut self , err : & mut Diag < ' _ > , ident_span : Span ) -> bool {
2350- if let Some ( Expr { kind : ExprKind :: Assign ( lhs, ..) , .. } ) = self . diag_metadata . in_assignment
2351- && let ast:: ExprKind :: Path ( None , ref path) = lhs. kind
2352- && !ident_span. from_expansion ( )
2353- {
2354- let ( span, text) = match path. segments . first ( ) {
2355- Some ( seg) if let Some ( name) = seg. ident . as_str ( ) . strip_prefix ( "let" ) => {
2356- // a special case for #117894
2357- let name = name. strip_prefix ( '_' ) . unwrap_or ( name) ;
2358- ( ident_span, format ! ( "let {name}" ) )
2359- }
2360- _ => ( ident_span. shrink_to_lo ( ) , "let " . to_string ( ) ) ,
2361- } ;
2350+ if !ident_span. from_expansion ( ) {
2351+ // only suggest when the code is a assignment without prefix code
2352+ if let Some ( Expr { kind : ExprKind :: Assign ( lhs, ..) , .. } ) =
2353+ self . diag_metadata . in_assignment
2354+ && let ast:: ExprKind :: Path ( None , ref path) = lhs. kind
2355+ && self . r . tcx . sess . source_map ( ) . is_line_before_span_empty ( ident_span)
2356+ {
2357+ let ( span, text) = match path. segments . first ( ) {
2358+ Some ( seg) if let Some ( name) = seg. ident . as_str ( ) . strip_prefix ( "let" ) => {
2359+ // a special case for #117894
2360+ let name = name. strip_prefix ( '_' ) . unwrap_or ( name) ;
2361+ ( ident_span, format ! ( "let {name}" ) )
2362+ }
2363+ _ => ( ident_span. shrink_to_lo ( ) , "let " . to_string ( ) ) ,
2364+ } ;
23622365
2363- err. span_suggestion_verbose (
2364- span,
2365- "you might have meant to introduce a new binding" ,
2366- text,
2367- Applicability :: MaybeIncorrect ,
2368- ) ;
2369- return true ;
2366+ err. span_suggestion_verbose (
2367+ span,
2368+ "you might have meant to introduce a new binding" ,
2369+ text,
2370+ Applicability :: MaybeIncorrect ,
2371+ ) ;
2372+ return true ;
2373+ } else {
2374+ // a special case for #133713
2375+ // '=' maybe a typo of `:`, which is a type annotation instead of assignment
2376+ if err. code == Some ( E0423 )
2377+ && let Some ( ( let_span, _, _) ) = self . diag_metadata . current_let_binding
2378+ && let span = let_span. shrink_to_hi ( ) . to ( ident_span. shrink_to_lo ( ) )
2379+ && let Ok ( code) = self . r . tcx . sess . source_map ( ) . span_to_snippet ( span)
2380+ && code. trim ( ) == "="
2381+ {
2382+ err. span_suggestion_verbose (
2383+ span,
2384+ "you might have meant to use `:` for type annotation" ,
2385+ ": " ,
2386+ Applicability :: MaybeIncorrect ,
2387+ ) ;
2388+ return true ;
2389+ }
2390+ }
23702391 }
23712392 false
23722393 }
0 commit comments