@@ -192,16 +192,13 @@ pub(crate) fn format_expr(
192
192
// Rewrite block without trying to put it in a single line.
193
193
Ok ( rw)
194
194
} else {
195
- let prefix = block_prefix ( context, block, shape) ?;
196
-
197
- rewrite_block_with_visitor (
198
- context,
199
- & prefix,
195
+ rewrite_block_inner (
200
196
block,
201
197
Some ( & expr. attrs ) ,
202
198
opt_label,
199
+ false ,
200
+ context,
203
201
shape,
204
- true ,
205
202
)
206
203
}
207
204
}
@@ -634,6 +631,31 @@ fn rewrite_block(
634
631
rewrite_block_inner ( block, attrs, label, true , context, shape)
635
632
}
636
633
634
+ fn remove_nested_block (
635
+ block : & ast:: Block ,
636
+ inner_label : Option < ast:: Label > ,
637
+ block_expr : & ast:: Expr ,
638
+ inner_block : & ast:: Block ,
639
+ context : & RewriteContext < ' _ > ,
640
+ shape : Shape ,
641
+ ) -> Option < RewriteResult > {
642
+ let pre_inner_block_span = mk_sp ( block. span . lo ( ) , block_expr. span . lo ( ) ) ;
643
+ let post_inner_block_span = mk_sp ( block_expr. span . hi ( ) , block. span . hi ( ) ) ;
644
+
645
+ let immdiately_contains_comment = contains_comment ( context. snippet ( pre_inner_block_span) )
646
+ || contains_comment ( context. snippet ( post_inner_block_span) ) ;
647
+ if immdiately_contains_comment {
648
+ return None ;
649
+ }
650
+ Some ( rewrite_block (
651
+ inner_block,
652
+ Some ( & block_expr. attrs ) ,
653
+ inner_label,
654
+ context,
655
+ shape,
656
+ ) )
657
+ }
658
+
637
659
fn rewrite_block_inner (
638
660
block : & ast:: Block ,
639
661
attrs : Option < & [ ast:: Attribute ] > ,
@@ -642,8 +664,52 @@ fn rewrite_block_inner(
642
664
context : & RewriteContext < ' _ > ,
643
665
shape : Shape ,
644
666
) -> RewriteResult {
667
+ debug ! ( "rewrite_block : {:?}" , context. snippet( block. span) ) ;
645
668
let prefix = block_prefix ( context, block, shape) ?;
646
669
670
+ let no_attrs = attrs. is_none ( ) || attrs. unwrap ( ) . is_empty ( ) ;
671
+
672
+ if context. config . remove_nested_blocks ( )
673
+ && !is_unsafe_block ( block)
674
+ && no_attrs
675
+ && label. is_none ( )
676
+ && block. stmts . len ( ) == 1
677
+ {
678
+ if let ast:: StmtKind :: Expr ( ref block_expr) = & block. stmts [ 0 ] . kind {
679
+ match block_expr. kind {
680
+ ast:: ExprKind :: Block ( ref inner_block, inner_label) => {
681
+ if let Some ( rw) = remove_nested_block (
682
+ block,
683
+ inner_label,
684
+ block_expr,
685
+ inner_block,
686
+ context,
687
+ shape,
688
+ ) {
689
+ return rw;
690
+ }
691
+ }
692
+
693
+ ast:: ExprKind :: ConstBlock ( ref anon_const) => {
694
+ if let ast:: ExprKind :: Block ( ref inner_block, inner_label) =
695
+ anon_const. value . kind
696
+ {
697
+ if let Some ( rw) = remove_nested_block (
698
+ block,
699
+ inner_label,
700
+ block_expr,
701
+ inner_block,
702
+ context,
703
+ shape,
704
+ ) {
705
+ return Ok ( format ! ( "const {}" , rw?) ) ;
706
+ }
707
+ }
708
+ }
709
+ _ => { }
710
+ }
711
+ }
712
+ }
647
713
// shape.width is used only for the single line case: either the empty block `{}`,
648
714
// or an unsafe expression `unsafe { e }`.
649
715
if let Some ( rw_str) = rewrite_empty_block ( context, block, attrs, label, & prefix, shape) {
0 commit comments