@@ -192,16 +192,13 @@ pub(crate) fn format_expr(
192192 // Rewrite block without trying to put it in a single line.
193193 Ok ( rw)
194194 } else {
195- let prefix = block_prefix ( context, block, shape) ?;
196-
197- rewrite_block_with_visitor (
198- context,
199- & prefix,
195+ rewrite_block_inner (
200196 block,
201197 Some ( & expr. attrs ) ,
202198 opt_label,
199+ false ,
200+ context,
203201 shape,
204- true ,
205202 )
206203 }
207204 }
@@ -634,6 +631,31 @@ fn rewrite_block(
634631 rewrite_block_inner ( block, attrs, label, true , context, shape)
635632}
636633
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+
637659fn rewrite_block_inner (
638660 block : & ast:: Block ,
639661 attrs : Option < & [ ast:: Attribute ] > ,
@@ -642,8 +664,52 @@ fn rewrite_block_inner(
642664 context : & RewriteContext < ' _ > ,
643665 shape : Shape ,
644666) -> RewriteResult {
667+ debug ! ( "rewrite_block : {:?}" , context. snippet( block. span) ) ;
645668 let prefix = block_prefix ( context, block, shape) ?;
646669
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+ }
647713 // shape.width is used only for the single line case: either the empty block `{}`,
648714 // or an unsafe expression `unsafe { e }`.
649715 if let Some ( rw_str) = rewrite_empty_block ( context, block, attrs, label, & prefix, shape) {
0 commit comments