@@ -124,7 +124,7 @@ pub fn format_expr(
124
124
if is_unsafe_block ( block) {
125
125
rewrite_block ( block, Some ( & expr. attrs ) , opt_label, context, shape)
126
126
} else if let rw @ Some ( _) =
127
- rewrite_empty_block ( context, block, Some ( & expr. attrs ) , "" , shape)
127
+ rewrite_empty_block ( context, block, Some ( & expr. attrs ) , opt_label , "" , shape)
128
128
{
129
129
// Rewrite block without trying to put it in a single line.
130
130
rw
@@ -136,6 +136,7 @@ pub fn format_expr(
136
136
& prefix,
137
137
block,
138
138
Some ( & expr. attrs ) ,
139
+ opt_label,
139
140
shape,
140
141
true ,
141
142
)
@@ -316,9 +317,14 @@ pub fn format_expr(
316
317
// satisfy our width restrictions.
317
318
ast:: ExprKind :: InlineAsm ( ..) => Some ( context. snippet ( expr. span ) . to_owned ( ) ) ,
318
319
ast:: ExprKind :: Catch ( ref block) => {
319
- if let rw @ Some ( _) =
320
- rewrite_single_line_block ( context, "do catch " , block, Some ( & expr. attrs ) , shape)
321
- {
320
+ if let rw @ Some ( _) = rewrite_single_line_block (
321
+ context,
322
+ "do catch " ,
323
+ block,
324
+ Some ( & expr. attrs ) ,
325
+ None ,
326
+ shape,
327
+ ) {
322
328
rw
323
329
} else {
324
330
// 9 = `do catch `
@@ -543,16 +549,18 @@ fn rewrite_empty_block(
543
549
context : & RewriteContext ,
544
550
block : & ast:: Block ,
545
551
attrs : Option < & [ ast:: Attribute ] > ,
552
+ label : Option < ast:: Label > ,
546
553
prefix : & str ,
547
554
shape : Shape ,
548
555
) -> Option < String > {
556
+ let label_str = rewrite_label ( label) ;
549
557
if attrs. map_or ( false , |a| !inner_attributes ( a) . is_empty ( ) ) {
550
558
return None ;
551
559
}
552
560
553
561
if block. stmts . is_empty ( ) && !block_contains_comment ( block, context. codemap ) && shape. width >= 2
554
562
{
555
- return Some ( format ! ( "{}{{ }}" , prefix) ) ;
563
+ return Some ( format ! ( "{}{}{{ }}" , prefix, label_str ) ) ;
556
564
}
557
565
558
566
// If a block contains only a single-line comment, then leave it on one line.
@@ -565,7 +573,7 @@ fn rewrite_empty_block(
565
573
&& !comment_str. starts_with ( "//" )
566
574
&& comment_str. len ( ) + 4 <= shape. width
567
575
{
568
- return Some ( format ! ( "{}{{ {} }}" , prefix, comment_str) ) ;
576
+ return Some ( format ! ( "{}{}{{ {} }}" , prefix, label_str , comment_str) ) ;
569
577
}
570
578
}
571
579
@@ -605,12 +613,14 @@ fn rewrite_single_line_block(
605
613
prefix : & str ,
606
614
block : & ast:: Block ,
607
615
attrs : Option < & [ ast:: Attribute ] > ,
616
+ label : Option < ast:: Label > ,
608
617
shape : Shape ,
609
618
) -> Option < String > {
610
619
if is_simple_block ( block, attrs, context. codemap ) {
611
620
let expr_shape = shape. offset_left ( last_line_width ( prefix) ) ?;
612
621
let expr_str = block. stmts [ 0 ] . rewrite ( context, expr_shape) ?;
613
- let result = format ! ( "{}{{ {} }}" , prefix, expr_str) ;
622
+ let label_str = rewrite_label ( label) ;
623
+ let result = format ! ( "{}{}{{ {} }}" , prefix, label_str, expr_str) ;
614
624
if result. len ( ) <= shape. width && !result. contains ( '\n' ) {
615
625
return Some ( result) ;
616
626
}
@@ -623,10 +633,11 @@ pub fn rewrite_block_with_visitor(
623
633
prefix : & str ,
624
634
block : & ast:: Block ,
625
635
attrs : Option < & [ ast:: Attribute ] > ,
636
+ label : Option < ast:: Label > ,
626
637
shape : Shape ,
627
638
has_braces : bool ,
628
639
) -> Option < String > {
629
- if let rw @ Some ( _) = rewrite_empty_block ( context, block, attrs, prefix, shape) {
640
+ if let rw @ Some ( _) = rewrite_empty_block ( context, block, attrs, label , prefix, shape) {
630
641
return rw;
631
642
}
632
643
@@ -643,8 +654,9 @@ pub fn rewrite_block_with_visitor(
643
654
}
644
655
645
656
let inner_attrs = attrs. map ( inner_attributes) ;
657
+ let label_str = rewrite_label ( label) ;
646
658
visitor. visit_block ( block, inner_attrs. as_ref ( ) . map ( |a| & * * a) , has_braces) ;
647
- Some ( format ! ( "{}{}" , prefix, visitor. buffer) )
659
+ Some ( format ! ( "{}{}{} " , prefix, label_str , visitor. buffer) )
648
660
}
649
661
650
662
impl Rewrite for ast:: Block {
@@ -660,20 +672,20 @@ fn rewrite_block(
660
672
context : & RewriteContext ,
661
673
shape : Shape ,
662
674
) -> Option < String > {
663
- let unsafe_string = block_prefix ( context, block, shape) ?;
664
- let label_string = rewrite_label ( label) ;
665
- let prefix = format ! ( "{}{}" , unsafe_string, label_string) ;
675
+ let prefix = block_prefix ( context, block, shape) ?;
666
676
667
677
// shape.width is used only for the single line case: either the empty block `{}`,
668
678
// or an unsafe expression `unsafe { e }`.
669
- if let rw @ Some ( _) = rewrite_empty_block ( context, block, attrs, & prefix, shape) {
679
+ if let rw @ Some ( _) = rewrite_empty_block ( context, block, attrs, label , & prefix, shape) {
670
680
return rw;
671
681
}
672
682
673
- let result = rewrite_block_with_visitor ( context, & prefix, block, attrs, shape, true ) ;
683
+ let result = rewrite_block_with_visitor ( context, & prefix, block, attrs, label , shape, true ) ;
674
684
if let Some ( ref result_str) = result {
675
685
if result_str. lines ( ) . count ( ) <= 3 {
676
- if let rw @ Some ( _) = rewrite_single_line_block ( context, & prefix, block, attrs, shape) {
686
+ if let rw @ Some ( _) =
687
+ rewrite_single_line_block ( context, & prefix, block, attrs, label, shape)
688
+ {
677
689
return rw;
678
690
}
679
691
}
@@ -1125,7 +1137,7 @@ impl<'a> Rewrite for ControlFlow<'a> {
1125
1137
let block_str = {
1126
1138
let old_val = context. is_if_else_block . replace ( self . else_block . is_some ( ) ) ;
1127
1139
let result =
1128
- rewrite_block_with_visitor ( context, "" , self . block , None , block_shape, true ) ;
1140
+ rewrite_block_with_visitor ( context, "" , self . block , None , None , block_shape, true ) ;
1129
1141
context. is_if_else_block . replace ( old_val) ;
1130
1142
result?
1131
1143
} ;
0 commit comments