@@ -291,70 +291,76 @@ impl<'a> Parser<'a> {
291
291
let lo = self . token . span ;
292
292
293
293
if !self . eat ( & token:: OpenDelim ( token:: Brace ) ) {
294
- let sp = self . token . span ;
295
- let tok = self . this_token_descr ( ) ;
296
- let mut e = self . span_fatal ( sp, & format ! ( "expected `{{`, found {}" , tok) ) ;
297
- let do_not_suggest_help =
298
- self . token . is_keyword ( kw:: In ) || self . token == token:: Colon ;
299
-
300
- if self . token . is_ident_named ( sym:: and) {
301
- e. span_suggestion_short (
302
- self . token . span ,
303
- "use `&&` instead of `and` for the boolean operator" ,
304
- "&&" . to_string ( ) ,
305
- Applicability :: MaybeIncorrect ,
306
- ) ;
307
- }
308
- if self . token . is_ident_named ( sym:: or) {
309
- e. span_suggestion_short (
310
- self . token . span ,
311
- "use `||` instead of `or` for the boolean operator" ,
312
- "||" . to_string ( ) ,
313
- Applicability :: MaybeIncorrect ,
314
- ) ;
315
- }
294
+ return self . error_block_no_opening_brace ( ) ;
295
+ }
316
296
317
- // Check to see if the user has written something like
318
- //
319
- // if (cond)
320
- // bar;
321
- //
322
- // which is valid in other languages, but not Rust.
323
- match self . parse_stmt_without_recovery ( false ) {
324
- Ok ( Some ( stmt) ) => {
325
- if self . look_ahead ( 1 , |t| t == & token:: OpenDelim ( token:: Brace ) )
326
- || do_not_suggest_help {
327
- // If the next token is an open brace (e.g., `if a b {`), the place-
328
- // inside-a-block suggestion would be more likely wrong than right.
329
- e. span_label ( sp, "expected `{`" ) ;
330
- return Err ( e) ;
331
- }
332
- let mut stmt_span = stmt. span ;
333
- // Expand the span to include the semicolon, if it exists.
334
- if self . eat ( & token:: Semi ) {
335
- stmt_span = stmt_span. with_hi ( self . prev_span . hi ( ) ) ;
336
- }
337
- if let Ok ( snippet) = self . span_to_snippet ( stmt_span) {
338
- e. span_suggestion (
339
- stmt_span,
340
- "try placing this code inside a block" ,
341
- format ! ( "{{ {} }}" , snippet) ,
342
- // Speculative; has been misleading in the past (#46836).
343
- Applicability :: MaybeIncorrect ,
344
- ) ;
345
- }
297
+ self . parse_block_tail ( lo, BlockCheckMode :: Default )
298
+ }
299
+
300
+ fn error_block_no_opening_brace < T > ( & mut self ) -> PResult < ' a , T > {
301
+ let sp = self . token . span ;
302
+ let tok = self . this_token_descr ( ) ;
303
+ let mut e = self . span_fatal ( sp, & format ! ( "expected `{{`, found {}" , tok) ) ;
304
+ let do_not_suggest_help =
305
+ self . token . is_keyword ( kw:: In ) || self . token == token:: Colon ;
306
+
307
+ if self . token . is_ident_named ( sym:: and) {
308
+ e. span_suggestion_short (
309
+ self . token . span ,
310
+ "use `&&` instead of `and` for the boolean operator" ,
311
+ "&&" . to_string ( ) ,
312
+ Applicability :: MaybeIncorrect ,
313
+ ) ;
314
+ }
315
+ if self . token . is_ident_named ( sym:: or) {
316
+ e. span_suggestion_short (
317
+ self . token . span ,
318
+ "use `||` instead of `or` for the boolean operator" ,
319
+ "||" . to_string ( ) ,
320
+ Applicability :: MaybeIncorrect ,
321
+ ) ;
322
+ }
323
+
324
+ // Check to see if the user has written something like
325
+ //
326
+ // if (cond)
327
+ // bar;
328
+ //
329
+ // which is valid in other languages, but not Rust.
330
+ match self . parse_stmt_without_recovery ( false ) {
331
+ Ok ( Some ( stmt) ) => {
332
+ if self . look_ahead ( 1 , |t| t == & token:: OpenDelim ( token:: Brace ) )
333
+ || do_not_suggest_help
334
+ {
335
+ // If the next token is an open brace (e.g., `if a b {`), the place-
336
+ // inside-a-block suggestion would be more likely wrong than right.
337
+ e. span_label ( sp, "expected `{`" ) ;
338
+ return Err ( e) ;
346
339
}
347
- Err ( mut e) => {
348
- self . recover_stmt_ ( SemiColonMode :: Break , BlockMode :: Ignore ) ;
349
- e. cancel ( ) ;
340
+ let stmt_span = if self . eat ( & token:: Semi ) {
341
+ // Expand the span to include the semicolon.
342
+ stmt. span . with_hi ( self . prev_span . hi ( ) )
343
+ } else {
344
+ stmt. span
345
+ } ;
346
+ if let Ok ( snippet) = self . span_to_snippet ( stmt_span) {
347
+ e. span_suggestion (
348
+ stmt_span,
349
+ "try placing this code inside a block" ,
350
+ format ! ( "{{ {} }}" , snippet) ,
351
+ // Speculative; has been misleading in the past (#46836).
352
+ Applicability :: MaybeIncorrect ,
353
+ ) ;
350
354
}
351
- _ => ( )
352
355
}
353
- e. span_label ( sp, "expected `{`" ) ;
354
- return Err ( e) ;
356
+ Err ( mut e) => {
357
+ self . recover_stmt_ ( SemiColonMode :: Break , BlockMode :: Ignore ) ;
358
+ e. cancel ( ) ;
359
+ }
360
+ _ => { }
355
361
}
356
-
357
- self . parse_block_tail ( lo , BlockCheckMode :: Default )
362
+ e . span_label ( sp , "expected `{`" ) ;
363
+ return Err ( e ) ;
358
364
}
359
365
360
366
/// Parses a block. Inner attributes are allowed.
0 commit comments