@@ -475,6 +475,7 @@ impl<'a> Parser<'a> {
475
475
}
476
476
477
477
fn error_block_no_opening_brace_msg ( & mut self , msg : Cow < ' static , str > ) -> Diag < ' a > {
478
+ let prev = self . prev_token . span ;
478
479
let sp = self . token . span ;
479
480
let mut e = self . dcx ( ) . struct_span_err ( sp, msg) ;
480
481
let do_not_suggest_help = self . token . is_keyword ( kw:: In ) || self . token == token:: Colon ;
@@ -514,15 +515,94 @@ impl<'a> Parser<'a> {
514
515
} else {
515
516
stmt. span
516
517
} ;
517
- e. multipart_suggestion (
518
- "try placing this code inside a block" ,
519
- vec ! [
520
- ( stmt_span. shrink_to_lo( ) , "{ " . to_string( ) ) ,
521
- ( stmt_span. shrink_to_hi( ) , " }" . to_string( ) ) ,
522
- ] ,
523
- // Speculative; has been misleading in the past (#46836).
524
- Applicability :: MaybeIncorrect ,
525
- ) ;
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
+ "try placing this code inside 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
+ "try placing this code inside 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
+ }
526
606
}
527
607
Err ( e) => {
528
608
self . recover_stmt_ ( SemiColonMode :: Break , BlockMode :: Ignore ) ;
0 commit comments