@@ -515,94 +515,12 @@ impl<'a> Parser<'a> {
515
515
} else {
516
516
stmt. span
517
517
} ;
518
- match ( & self . token . kind , & stmt. kind ) {
519
- ( token:: OpenDelim ( Delimiter :: Brace ) , StmtKind :: Expr ( expr) )
520
- if let ExprKind :: Call ( ..) = expr. kind =>
521
- {
522
- // for _ in x y() {}
523
- e. span_suggestion_verbose (
524
- prev. between ( sp) ,
525
- "you might have meant to write a method call" ,
526
- "." . to_string ( ) ,
527
- Applicability :: MaybeIncorrect ,
528
- ) ;
529
- }
530
- ( token:: OpenDelim ( Delimiter :: Brace ) , StmtKind :: Expr ( expr) )
531
- if let ExprKind :: Field ( ..) = expr. kind =>
532
- {
533
- // for _ in x y.z {}
534
- e. span_suggestion_verbose (
535
- prev. between ( sp) ,
536
- "you might have meant to write a field access" ,
537
- "." . to_string ( ) ,
538
- Applicability :: MaybeIncorrect ,
539
- ) ;
540
- }
541
- ( token:: CloseDelim ( Delimiter :: Brace ) , StmtKind :: Expr ( expr) )
542
- if let ExprKind :: Struct ( expr) = & expr. kind
543
- && let None = expr. qself
544
- && expr. path . segments . len ( ) == 1 =>
545
- {
546
- // This is specific to "mistyped `if` condition followed by empty body"
547
- //
548
- // for _ in x y {}
549
- e. span_suggestion_verbose (
550
- prev. between ( sp) ,
551
- "you might have meant to write a field access" ,
552
- "." . to_string ( ) ,
553
- Applicability :: MaybeIncorrect ,
554
- ) ;
555
- }
556
- ( token:: OpenDelim ( Delimiter :: Brace ) , StmtKind :: Expr ( expr) )
557
- if let ExprKind :: Lit ( lit) = expr. kind
558
- && let None = lit. suffix
559
- && let token:: LitKind :: Integer | token:: LitKind :: Float = lit. kind =>
560
- {
561
- // for _ in x 0 {}
562
- // for _ in x 0.0 {}
563
- e. span_suggestion_verbose (
564
- prev. between ( sp) ,
565
- format ! ( "you might have meant to write a field access" ) ,
566
- "." . to_string ( ) ,
567
- Applicability :: MaybeIncorrect ,
568
- ) ;
569
- }
570
- ( token:: OpenDelim ( Delimiter :: Brace ) , StmtKind :: Expr ( expr) )
571
- if let ExprKind :: Loop ( ..)
572
- | ExprKind :: If ( ..)
573
- | ExprKind :: While ( ..)
574
- | ExprKind :: Match ( ..)
575
- | ExprKind :: ForLoop { .. }
576
- | ExprKind :: TryBlock ( ..)
577
- | ExprKind :: Ret ( ..)
578
- | ExprKind :: Closure ( ..)
579
- | ExprKind :: Struct ( ..)
580
- | ExprKind :: Try ( ..) = expr. kind =>
581
- {
582
- // These are more likely to have been meant as a block body.
583
- e. multipart_suggestion (
584
- "you might have meant to write this as part of a block" ,
585
- vec ! [
586
- ( stmt_span. shrink_to_lo( ) , "{ " . to_string( ) ) ,
587
- ( stmt_span. shrink_to_hi( ) , " }" . to_string( ) ) ,
588
- ] ,
589
- // Speculative; has been misleading in the past (#46836).
590
- Applicability :: MaybeIncorrect ,
591
- ) ;
592
- }
593
- ( token:: OpenDelim ( Delimiter :: Brace ) , _) => { }
594
- ( _, _) => {
595
- e. multipart_suggestion (
596
- "you might have meant to write this as part of a block" ,
597
- vec ! [
598
- ( stmt_span. shrink_to_lo( ) , "{ " . to_string( ) ) ,
599
- ( stmt_span. shrink_to_hi( ) , " }" . to_string( ) ) ,
600
- ] ,
601
- // Speculative; has been misleading in the past (#46836).
602
- Applicability :: MaybeIncorrect ,
603
- ) ;
604
- }
605
- }
518
+ self . suggest_fixes_misparsed_for_loop_head (
519
+ & mut e,
520
+ prev. between ( sp) ,
521
+ stmt_span,
522
+ & stmt. kind ,
523
+ ) ;
606
524
}
607
525
Err ( e) => {
608
526
self . recover_stmt_ ( SemiColonMode :: Break , BlockMode :: Ignore ) ;
@@ -614,6 +532,103 @@ impl<'a> Parser<'a> {
614
532
e
615
533
}
616
534
535
+ fn suggest_fixes_misparsed_for_loop_head (
536
+ & self ,
537
+ e : & mut Diag < ' _ > ,
538
+ between : Span ,
539
+ stmt_span : Span ,
540
+ stmt_kind : & StmtKind ,
541
+ ) {
542
+ match ( & self . token . kind , & stmt_kind) {
543
+ ( token:: OpenDelim ( Delimiter :: Brace ) , StmtKind :: Expr ( expr) )
544
+ if let ExprKind :: Call ( ..) = expr. kind =>
545
+ {
546
+ // for _ in x y() {}
547
+ e. span_suggestion_verbose (
548
+ between,
549
+ "you might have meant to write a method call" ,
550
+ "." . to_string ( ) ,
551
+ Applicability :: MaybeIncorrect ,
552
+ ) ;
553
+ }
554
+ ( token:: OpenDelim ( Delimiter :: Brace ) , StmtKind :: Expr ( expr) )
555
+ if let ExprKind :: Field ( ..) = expr. kind =>
556
+ {
557
+ // for _ in x y.z {}
558
+ e. span_suggestion_verbose (
559
+ between,
560
+ "you might have meant to write a field access" ,
561
+ "." . to_string ( ) ,
562
+ Applicability :: MaybeIncorrect ,
563
+ ) ;
564
+ }
565
+ ( token:: CloseDelim ( Delimiter :: Brace ) , StmtKind :: Expr ( expr) )
566
+ if let ExprKind :: Struct ( expr) = & expr. kind
567
+ && let None = expr. qself
568
+ && expr. path . segments . len ( ) == 1 =>
569
+ {
570
+ // This is specific to "mistyped `if` condition followed by empty body"
571
+ //
572
+ // for _ in x y {}
573
+ e. span_suggestion_verbose (
574
+ between,
575
+ "you might have meant to write a field access" ,
576
+ "." . to_string ( ) ,
577
+ Applicability :: MaybeIncorrect ,
578
+ ) ;
579
+ }
580
+ ( token:: OpenDelim ( Delimiter :: Brace ) , StmtKind :: Expr ( expr) )
581
+ if let ExprKind :: Lit ( lit) = expr. kind
582
+ && let None = lit. suffix
583
+ && let token:: LitKind :: Integer | token:: LitKind :: Float = lit. kind =>
584
+ {
585
+ // for _ in x 0 {}
586
+ // for _ in x 0.0 {}
587
+ e. span_suggestion_verbose (
588
+ between,
589
+ format ! ( "you might have meant to write a field access" ) ,
590
+ "." . to_string ( ) ,
591
+ Applicability :: MaybeIncorrect ,
592
+ ) ;
593
+ }
594
+ ( token:: OpenDelim ( Delimiter :: Brace ) , StmtKind :: Expr ( expr) )
595
+ if let ExprKind :: Loop ( ..)
596
+ | ExprKind :: If ( ..)
597
+ | ExprKind :: While ( ..)
598
+ | ExprKind :: Match ( ..)
599
+ | ExprKind :: ForLoop { .. }
600
+ | ExprKind :: TryBlock ( ..)
601
+ | ExprKind :: Ret ( ..)
602
+ | ExprKind :: Closure ( ..)
603
+ | ExprKind :: Struct ( ..)
604
+ | ExprKind :: Try ( ..) = expr. kind =>
605
+ {
606
+ // These are more likely to have been meant as a block body.
607
+ e. multipart_suggestion (
608
+ "you might have meant to write this as part of a block" ,
609
+ vec ! [
610
+ ( stmt_span. shrink_to_lo( ) , "{ " . to_string( ) ) ,
611
+ ( stmt_span. shrink_to_hi( ) , " }" . to_string( ) ) ,
612
+ ] ,
613
+ // Speculative; has been misleading in the past (#46836).
614
+ Applicability :: MaybeIncorrect ,
615
+ ) ;
616
+ }
617
+ ( token:: OpenDelim ( Delimiter :: Brace ) , _) => { }
618
+ ( _, _) => {
619
+ e. multipart_suggestion (
620
+ "you might have meant to write this as part of a block" ,
621
+ vec ! [
622
+ ( stmt_span. shrink_to_lo( ) , "{ " . to_string( ) ) ,
623
+ ( stmt_span. shrink_to_hi( ) , " }" . to_string( ) ) ,
624
+ ] ,
625
+ // Speculative; has been misleading in the past (#46836).
626
+ Applicability :: MaybeIncorrect ,
627
+ ) ;
628
+ }
629
+ }
630
+ }
631
+
617
632
fn error_block_no_opening_brace < T > ( & mut self ) -> PResult < ' a , T > {
618
633
let tok = super :: token_descr ( & self . token ) ;
619
634
let msg = format ! ( "expected `{{`, found {tok}" ) ;
0 commit comments