Skip to content

Commit b516a8c

Browse files
committed
Pretty print empty blocks as {}
1 parent f04a2f4 commit b516a8c

File tree

63 files changed

+182
-164
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+182
-164
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+44-24
Original file line numberDiff line numberDiff line change
@@ -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,

compiler/rustc_hir_pretty/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2171,7 +2171,7 @@ impl<'a> State<'a> {
21712171
match decl.output {
21722172
hir::FnRetTy::Return(ref ty) => {
21732173
self.print_type(&ty);
2174-
self.maybe_print_comment(ty.span.lo())
2174+
self.maybe_print_comment(ty.span.lo());
21752175
}
21762176
hir::FnRetTy::DefaultReturn(..) => unreachable!(),
21772177
}
@@ -2365,7 +2365,7 @@ impl<'a> State<'a> {
23652365
self.end();
23662366

23672367
if let hir::FnRetTy::Return(ref output) = decl.output {
2368-
self.maybe_print_comment(output.span.lo())
2368+
self.maybe_print_comment(output.span.lo());
23692369
}
23702370
}
23712371

src/test/pretty/ast-stmt-expr-attr.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// pp-exact
22

3-
fn main() { }
3+
fn main() {}
44

55
#[cfg(FALSE)]
66
fn syntax() {
@@ -117,7 +117,7 @@ fn syntax() {
117117
let _ = #[attr] foo!(#! [attr]);
118118
let _ = #[attr] foo![];
119119
let _ = #[attr] foo![#! [attr]];
120-
let _ = #[attr] foo! { };
120+
let _ = #[attr] foo! {};
121121
let _ = #[attr] foo! { #! [attr] };
122122
let _ = #[attr] Foo{bar: baz,};
123123
let _ = #[attr] Foo{..foo};
@@ -135,7 +135,7 @@ fn syntax() {
135135
foo!();
136136

137137
#[attr]
138-
foo! { }
138+
foo! {}
139139

140140
#[attr]
141141
foo![];
@@ -170,6 +170,6 @@ fn syntax() {
170170
{
171171

172172
#[attr]
173-
foo! { }
173+
foo! {}
174174
}
175175
}

src/test/pretty/attr-derive.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ enum Enum {
2929
Qwerty,
3030
}
3131

32-
fn main() { }
32+
fn main() {}

src/test/pretty/auto-trait.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
// pp-exact
44

5-
auto trait MyTrait { }
5+
auto trait MyTrait {}
66

7-
unsafe auto trait UnsafeMyTrait { }
7+
unsafe auto trait UnsafeMyTrait {}
88

9-
pub fn main() { }
9+
pub fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// compile-flags: --crate-type=lib
22

33
// pp-exact
4-
fn f() {
5-
} /*
6-
The next line should not be indented.
4+
fn f() {} /*
5+
The next line should not be indented.
76
8-
That one. It shouldn't have been indented.
9-
*/
7+
That one. It shouldn't have been indented.
8+
*/

src/test/pretty/closure-reform-pretty.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
// pp-exact
55

6-
fn call_it(f: Box<FnMut(String) -> String>) { }
6+
fn call_it(f: Box<FnMut(String) -> String>) {}
77

8-
fn call_this<F>(f: F) where F: Fn(&str) + Send { }
8+
fn call_this<F>(f: F) where F: Fn(&str) + Send {}
99

10-
fn call_that<F>(f: F) where F: for<'a> Fn(&'a isize, &'a isize) -> isize { }
10+
fn call_that<F>(f: F) where F: for<'a> Fn(&'a isize, &'a isize) -> isize {}
1111

12-
fn call_extern(f: fn() -> isize) { }
12+
fn call_extern(f: fn() -> isize) {}
1313

14-
fn call_abid_extern(f: extern "C" fn() -> isize) { }
14+
fn call_abid_extern(f: extern "C" fn() -> isize) {}
1515

16-
pub fn main() { }
16+
pub fn main() {}

src/test/pretty/disamb-stmt-expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
fn id<F>(f: F) -> isize where F: Fn() -> isize { f() }
88

99
fn wsucc(_n: isize) -> isize { id(|| { 1 }) - 0 }
10-
fn main() { }
10+
fn main() {}

src/test/pretty/enum-variant-vis.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// Check that the visibility is printed on an enum variant.
44

5-
fn main() { }
5+
fn main() {}
66

77
#[cfg(FALSE)]
88
enum Foo { pub V, }

src/test/pretty/example1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// pp-exact
22

3-
fn main() { }
3+
fn main() {}

src/test/pretty/example2.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// pp-exact:example2.pp
22

3-
fn main() { }
3+
fn main() {}

src/test/pretty/example2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// pp-exact:example2.pp
22

3-
fn main() { }
3+
fn main() {}

src/test/pretty/expanded-and-path-remap-80832.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
// pp-exact:expanded-and-path-remap-80832.pp
1111
// compile-flags: --remap-path-prefix {{src-base}}=the/src
1212

13-
fn main() { }
13+
fn main() {}

src/test/pretty/fn-return.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// pp-exact
22

3-
// Check that `fn f() -> () { }` does not print as `fn f() { }`.
3+
// Check that `fn f() -> () {}` does not print as `fn f() {}`.
44

5-
fn f() -> () { }
5+
fn f() -> () {}
66

7-
fn main() { }
7+
fn main() {}

src/test/pretty/fn-types.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// pp-exact
22

3-
fn from_foreign_fn(_x: fn()) { }
4-
fn from_stack_closure<F>(_x: F) where F: Fn() { }
5-
fn main() { }
3+
fn from_foreign_fn(_x: fn()) {}
4+
fn from_stack_closure<F>(_x: F) where F: Fn() {}
5+
fn main() {}

src/test/pretty/fn-variadic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ pub unsafe extern "C" fn bar(_: i32, mut ap: ...) -> usize {
1212
ap.arg::<usize>()
1313
}
1414

15-
fn main() { }
15+
fn main() {}

src/test/pretty/if-attr.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,34 @@
44
fn simple_attr() {
55

66
#[attr]
7-
if true { }
7+
if true {}
88

99
#[allow_warnings]
10-
if true { }
10+
if true {}
1111
}
1212

1313
#[cfg(FALSE)]
1414
fn if_else_chain() {
1515

1616
#[first_attr]
17-
if true { } else if false { } else { }
17+
if true {} else if false {} else {}
1818
}
1919

2020
#[cfg(FALSE)]
2121
fn if_let() {
2222

2323
#[attr]
24-
if let Some(_) = Some(true) { }
24+
if let Some(_) = Some(true) {}
2525
}
2626

2727
#[cfg(FALSE)]
2828
fn let_attr_if() {
29-
let _ = #[attr] if let _ = 0 { };
30-
let _ = #[attr] if true { };
29+
let _ = #[attr] if let _ = 0 {};
30+
let _ = #[attr] if true {};
3131

32-
let _ = #[attr] if let _ = 0 { } else { };
33-
let _ = #[attr] if true { } else { };
32+
let _ = #[attr] if let _ = 0 {} else {};
33+
let _ = #[attr] if true {} else {};
3434
}
3535

3636

37-
fn main() { }
37+
fn main() {}

src/test/pretty/issue-12590-a.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
#[path = "issue-12590-b.rs"]
77
mod issue_12590_b;
88

9-
fn main() { }
9+
fn main() {}

src/test/pretty/issue-12590-c.pp

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#[path = "issue-12590-b.rs"]
1414
mod issue_12590_b {
1515

16-
fn b() { }
17-
fn main() { }
16+
fn b() {}
17+
fn main() {}
1818
}
19-
fn main() { }
19+
fn main() {}

src/test/pretty/issue-12590-c.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
#[path = "issue-12590-b.rs"]
88
mod issue_12590_b;
99

10-
fn main() { }
10+
fn main() {}

0 commit comments

Comments
 (0)