@@ -263,14 +263,17 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
263
263
self . strsep ( "," , false , b, elts, op)
264
264
}
265
265
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 ;
267
268
while let Some ( ref cmnt) = self . next_comment ( ) {
268
269
if cmnt. pos < pos {
270
+ has_comment = true ;
269
271
self . print_comment ( cmnt) ;
270
272
} else {
271
273
break ;
272
274
}
273
275
}
276
+ has_comment
274
277
}
275
278
276
279
fn print_comment ( & mut self , cmnt : & Comment ) {
@@ -570,7 +573,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
570
573
self . print_tts ( tts, convert_dollar_crate) ;
571
574
self . end ( ) ;
572
575
match delim {
573
- DelimToken :: Brace => self . bclose ( span) ,
576
+ DelimToken :: Brace => {
577
+ let empty = tts. is_empty ( ) ;
578
+ self . bclose ( span, empty) ;
579
+ }
574
580
_ => {
575
581
let token_str = self . token_kind_to_string ( & token:: CloseDelim ( delim) ) ;
576
582
self . word ( token_str)
@@ -642,17 +648,20 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
642
648
self . end ( ) ; // Close the head-box.
643
649
}
644
650
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
+ }
648
656
self . word ( "}" ) ;
649
657
if close_box {
650
658
self . end ( ) ; // Close the outer-box.
651
659
}
652
660
}
653
661
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)
656
665
}
657
666
658
667
fn break_offset_if_not_bol ( & mut self , n : usize , off : isize ) {
@@ -1196,7 +1205,8 @@ impl<'a> State<'a> {
1196
1205
for item in items {
1197
1206
self . print_item ( item) ;
1198
1207
}
1199
- self . bclose ( item. span ) ;
1208
+ let empty = item. attrs . is_empty ( ) && items. is_empty ( ) ;
1209
+ self . bclose ( item. span , empty) ;
1200
1210
}
1201
1211
ModKind :: Unloaded => {
1202
1212
self . s . word ( ";" ) ;
@@ -1216,7 +1226,8 @@ impl<'a> State<'a> {
1216
1226
}
1217
1227
self . bopen ( ) ;
1218
1228
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) ;
1220
1231
}
1221
1232
ast:: ItemKind :: GlobalAsm ( ref asm) => {
1222
1233
self . head ( visibility_qualified ( & item. vis , "global_asm!" ) ) ;
@@ -1291,7 +1302,8 @@ impl<'a> State<'a> {
1291
1302
for impl_item in items {
1292
1303
self . print_assoc_item ( impl_item) ;
1293
1304
}
1294
- self . bclose ( item. span ) ;
1305
+ let empty = item. attrs . is_empty ( ) && items. is_empty ( ) ;
1306
+ self . bclose ( item. span , empty) ;
1295
1307
}
1296
1308
ast:: ItemKind :: Trait ( box ast:: Trait {
1297
1309
is_auto,
@@ -1326,7 +1338,8 @@ impl<'a> State<'a> {
1326
1338
for trait_item in items {
1327
1339
self . print_assoc_item ( trait_item) ;
1328
1340
}
1329
- self . bclose ( item. span ) ;
1341
+ let empty = item. attrs . is_empty ( ) && items. is_empty ( ) ;
1342
+ self . bclose ( item. span , empty) ;
1330
1343
}
1331
1344
ast:: ItemKind :: TraitAlias ( ref generics, ref bounds) => {
1332
1345
self . head ( "" ) ;
@@ -1410,7 +1423,8 @@ impl<'a> State<'a> {
1410
1423
self . end ( ) ;
1411
1424
self . maybe_print_trailing_comment ( v. span , None ) ;
1412
1425
}
1413
- self . bclose ( span)
1426
+ let empty = variants. is_empty ( ) ;
1427
+ self . bclose ( span, empty)
1414
1428
}
1415
1429
1416
1430
crate fn print_visibility ( & mut self , vis : & ast:: Visibility ) {
@@ -1441,20 +1455,24 @@ impl<'a> State<'a> {
1441
1455
crate fn print_record_struct_body ( & mut self , fields : & [ ast:: FieldDef ] , span : rustc_span:: Span ) {
1442
1456
self . nbsp ( ) ;
1443
1457
self . bopen ( ) ;
1444
- self . hardbreak_if_not_bol ( ) ;
1445
1458
1446
- for field in fields {
1459
+ let empty = fields. is_empty ( ) ;
1460
+ if !empty {
1447
1461
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
+ }
1455
1473
}
1456
1474
1457
- self . bclose ( span)
1475
+ self . bclose ( span, empty ) ;
1458
1476
}
1459
1477
1460
1478
crate fn print_struct (
@@ -1633,7 +1651,8 @@ impl<'a> State<'a> {
1633
1651
}
1634
1652
}
1635
1653
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) ;
1637
1656
self . ann . post ( self , AnnNode :: Block ( blk) )
1638
1657
}
1639
1658
@@ -2010,7 +2029,8 @@ impl<'a> State<'a> {
2010
2029
for arm in arms {
2011
2030
self . print_arm ( arm) ;
2012
2031
}
2013
- self . bclose ( expr. span ) ;
2032
+ let empty = attrs. is_empty ( ) && arms. is_empty ( ) ;
2033
+ self . bclose ( expr. span , empty) ;
2014
2034
}
2015
2035
ast:: ExprKind :: Closure (
2016
2036
capture_clause,
0 commit comments