@@ -263,14 +263,17 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
263263 self . strsep ( "," , false , b, elts, op)
264264 }
265265
266- fn maybe_print_comment ( & mut self , pos : BytePos ) {
266+ fn maybe_print_comment ( & mut self , pos : BytePos ) -> bool {
267+ let mut has_comment = false ;
267268 while let Some ( ref cmnt) = self . next_comment ( ) {
268269 if cmnt. pos < pos {
270+ has_comment = true ;
269271 self . print_comment ( cmnt) ;
270272 } else {
271273 break ;
272274 }
273275 }
276+ has_comment
274277 }
275278
276279 fn print_comment ( & mut self , cmnt : & Comment ) {
@@ -570,7 +573,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
570573 self . print_tts ( tts, convert_dollar_crate) ;
571574 self . end ( ) ;
572575 match delim {
573- DelimToken :: Brace => self . bclose ( span) ,
576+ DelimToken :: Brace => {
577+ let empty = tts. is_empty ( ) ;
578+ self . bclose ( span, empty) ;
579+ }
574580 _ => {
575581 let token_str = self . token_kind_to_string ( & token:: CloseDelim ( delim) ) ;
576582 self . word ( token_str)
@@ -642,17 +648,20 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
642648 self . end ( ) ; // Close the head-box.
643649 }
644650
645- fn bclose_maybe_open ( & mut self , span : rustc_span:: Span , close_box : bool ) {
646- self . maybe_print_comment ( span. hi ( ) ) ;
647- self . break_offset_if_not_bol ( 1 , -( INDENT_UNIT as isize ) ) ;
651+ fn bclose_maybe_open ( & mut self , span : rustc_span:: Span , empty : bool , close_box : bool ) {
652+ let has_comment = self . maybe_print_comment ( span. hi ( ) ) ;
653+ if !empty || has_comment {
654+ self . break_offset_if_not_bol ( 1 , -( INDENT_UNIT as isize ) ) ;
655+ }
648656 self . word ( "}" ) ;
649657 if close_box {
650658 self . end ( ) ; // Close the outer-box.
651659 }
652660 }
653661
654- fn bclose ( & mut self , span : rustc_span:: Span ) {
655- self . bclose_maybe_open ( span, true )
662+ fn bclose ( & mut self , span : rustc_span:: Span , empty : bool ) {
663+ let close_box = true ;
664+ self . bclose_maybe_open ( span, empty, close_box)
656665 }
657666
658667 fn break_offset_if_not_bol ( & mut self , n : usize , off : isize ) {
@@ -1196,7 +1205,8 @@ impl<'a> State<'a> {
11961205 for item in items {
11971206 self . print_item ( item) ;
11981207 }
1199- self . bclose ( item. span ) ;
1208+ let empty = item. attrs . is_empty ( ) && items. is_empty ( ) ;
1209+ self . bclose ( item. span , empty) ;
12001210 }
12011211 ModKind :: Unloaded => {
12021212 self . s . word ( ";" ) ;
@@ -1216,7 +1226,8 @@ impl<'a> State<'a> {
12161226 }
12171227 self . bopen ( ) ;
12181228 self . print_foreign_mod ( nmod, & item. attrs ) ;
1219- self . bclose ( item. span ) ;
1229+ let empty = item. attrs . is_empty ( ) && nmod. items . is_empty ( ) ;
1230+ self . bclose ( item. span , empty) ;
12201231 }
12211232 ast:: ItemKind :: GlobalAsm ( ref asm) => {
12221233 self . head ( visibility_qualified ( & item. vis , "global_asm!" ) ) ;
@@ -1291,7 +1302,8 @@ impl<'a> State<'a> {
12911302 for impl_item in items {
12921303 self . print_assoc_item ( impl_item) ;
12931304 }
1294- self . bclose ( item. span ) ;
1305+ let empty = item. attrs . is_empty ( ) && items. is_empty ( ) ;
1306+ self . bclose ( item. span , empty) ;
12951307 }
12961308 ast:: ItemKind :: Trait ( box ast:: Trait {
12971309 is_auto,
@@ -1326,7 +1338,8 @@ impl<'a> State<'a> {
13261338 for trait_item in items {
13271339 self . print_assoc_item ( trait_item) ;
13281340 }
1329- self . bclose ( item. span ) ;
1341+ let empty = item. attrs . is_empty ( ) && items. is_empty ( ) ;
1342+ self . bclose ( item. span , empty) ;
13301343 }
13311344 ast:: ItemKind :: TraitAlias ( ref generics, ref bounds) => {
13321345 self . head ( "" ) ;
@@ -1410,7 +1423,8 @@ impl<'a> State<'a> {
14101423 self . end ( ) ;
14111424 self . maybe_print_trailing_comment ( v. span , None ) ;
14121425 }
1413- self . bclose ( span)
1426+ let empty = variants. is_empty ( ) ;
1427+ self . bclose ( span, empty)
14141428 }
14151429
14161430 crate fn print_visibility ( & mut self , vis : & ast:: Visibility ) {
@@ -1441,20 +1455,24 @@ impl<'a> State<'a> {
14411455 crate fn print_record_struct_body ( & mut self , fields : & [ ast:: FieldDef ] , span : rustc_span:: Span ) {
14421456 self . nbsp ( ) ;
14431457 self . bopen ( ) ;
1444- self . hardbreak_if_not_bol ( ) ;
14451458
1446- for field in fields {
1459+ let empty = fields. is_empty ( ) ;
1460+ if !empty {
14471461 self . hardbreak_if_not_bol ( ) ;
1448- self . maybe_print_comment ( field. span . lo ( ) ) ;
1449- self . print_outer_attributes ( & field. attrs ) ;
1450- self . print_visibility ( & field. vis ) ;
1451- self . print_ident ( field. ident . unwrap ( ) ) ;
1452- self . word_nbsp ( ":" ) ;
1453- self . print_type ( & field. ty ) ;
1454- self . s . word ( "," ) ;
1462+
1463+ for field in fields {
1464+ self . hardbreak_if_not_bol ( ) ;
1465+ self . maybe_print_comment ( field. span . lo ( ) ) ;
1466+ self . print_outer_attributes ( & field. attrs ) ;
1467+ self . print_visibility ( & field. vis ) ;
1468+ self . print_ident ( field. ident . unwrap ( ) ) ;
1469+ self . word_nbsp ( ":" ) ;
1470+ self . print_type ( & field. ty ) ;
1471+ self . s . word ( "," ) ;
1472+ }
14551473 }
14561474
1457- self . bclose ( span)
1475+ self . bclose ( span, empty ) ;
14581476 }
14591477
14601478 crate fn print_struct (
@@ -1633,7 +1651,8 @@ impl<'a> State<'a> {
16331651 }
16341652 }
16351653
1636- self . bclose_maybe_open ( blk. span , close_box) ;
1654+ let empty = attrs. is_empty ( ) && blk. stmts . is_empty ( ) ;
1655+ self . bclose_maybe_open ( blk. span , empty, close_box) ;
16371656 self . ann . post ( self , AnnNode :: Block ( blk) )
16381657 }
16391658
@@ -2010,7 +2029,8 @@ impl<'a> State<'a> {
20102029 for arm in arms {
20112030 self . print_arm ( arm) ;
20122031 }
2013- self . bclose ( expr. span ) ;
2032+ let empty = attrs. is_empty ( ) && arms. is_empty ( ) ;
2033+ self . bclose ( expr. span , empty) ;
20142034 }
20152035 ast:: ExprKind :: Closure (
20162036 capture_clause,
0 commit comments