1
Fork 0

Rollup merge of #91437 - dtolnay:emptybrace, r=nagisa

Pretty print empty blocks as {}

**Example:**

```rust
macro_rules! p {
    ($e:expr) => {
        println!("{}", stringify!($e));
    };
    ($i:item) => {
        println!("{}", stringify!($i));
    };
}

fn main() {
    p!(if true {});
    p!(struct S {});
}
```

**Before:**

```console
if true { }
struct S {
}
```

**After:**

```console
if true {}
struct S {}
```

This affects [`dbg!`](https://doc.rust-lang.org/std/macro.dbg.html), as well as ecosystem uses of stringify such as in [`anyhow::ensure!`](https://docs.rs/anyhow/1/anyhow/macro.ensure.html). Printing a `{ }` in today's heavily rustfmt'd world comes out looking jarring/sloppy.
This commit is contained in:
Matthias Krüger 2021-12-05 15:04:20 +01:00 committed by GitHub
commit a8f8f746fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
63 changed files with 182 additions and 164 deletions

View file

@ -263,14 +263,17 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
self.strsep(",", false, b, elts, op)
}
fn maybe_print_comment(&mut self, pos: BytePos) {
fn maybe_print_comment(&mut self, pos: BytePos) -> bool {
let mut has_comment = false;
while let Some(ref cmnt) = self.next_comment() {
if cmnt.pos < pos {
has_comment = true;
self.print_comment(cmnt);
} else {
break;
}
}
has_comment
}
fn print_comment(&mut self, cmnt: &Comment) {
@ -570,7 +573,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
self.print_tts(tts, convert_dollar_crate);
self.end();
match delim {
DelimToken::Brace => self.bclose(span),
DelimToken::Brace => {
let empty = tts.is_empty();
self.bclose(span, empty);
}
_ => {
let token_str = self.token_kind_to_string(&token::CloseDelim(delim));
self.word(token_str)
@ -642,17 +648,20 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
self.end(); // Close the head-box.
}
fn bclose_maybe_open(&mut self, span: rustc_span::Span, close_box: bool) {
self.maybe_print_comment(span.hi());
fn bclose_maybe_open(&mut self, span: rustc_span::Span, empty: bool, close_box: bool) {
let has_comment = self.maybe_print_comment(span.hi());
if !empty || has_comment {
self.break_offset_if_not_bol(1, -(INDENT_UNIT as isize));
}
self.word("}");
if close_box {
self.end(); // Close the outer-box.
}
}
fn bclose(&mut self, span: rustc_span::Span) {
self.bclose_maybe_open(span, true)
fn bclose(&mut self, span: rustc_span::Span, empty: bool) {
let close_box = true;
self.bclose_maybe_open(span, empty, close_box)
}
fn break_offset_if_not_bol(&mut self, n: usize, off: isize) {
@ -1196,7 +1205,8 @@ impl<'a> State<'a> {
for item in items {
self.print_item(item);
}
self.bclose(item.span);
let empty = item.attrs.is_empty() && items.is_empty();
self.bclose(item.span, empty);
}
ModKind::Unloaded => {
self.s.word(";");
@ -1216,7 +1226,8 @@ impl<'a> State<'a> {
}
self.bopen();
self.print_foreign_mod(nmod, &item.attrs);
self.bclose(item.span);
let empty = item.attrs.is_empty() && nmod.items.is_empty();
self.bclose(item.span, empty);
}
ast::ItemKind::GlobalAsm(ref asm) => {
self.head(visibility_qualified(&item.vis, "global_asm!"));
@ -1291,7 +1302,8 @@ impl<'a> State<'a> {
for impl_item in items {
self.print_assoc_item(impl_item);
}
self.bclose(item.span);
let empty = item.attrs.is_empty() && items.is_empty();
self.bclose(item.span, empty);
}
ast::ItemKind::Trait(box ast::Trait {
is_auto,
@ -1326,7 +1338,8 @@ impl<'a> State<'a> {
for trait_item in items {
self.print_assoc_item(trait_item);
}
self.bclose(item.span);
let empty = item.attrs.is_empty() && items.is_empty();
self.bclose(item.span, empty);
}
ast::ItemKind::TraitAlias(ref generics, ref bounds) => {
self.head("");
@ -1410,7 +1423,8 @@ impl<'a> State<'a> {
self.end();
self.maybe_print_trailing_comment(v.span, None);
}
self.bclose(span)
let empty = variants.is_empty();
self.bclose(span, empty)
}
crate fn print_visibility(&mut self, vis: &ast::Visibility) {
@ -1441,6 +1455,9 @@ impl<'a> State<'a> {
crate fn print_record_struct_body(&mut self, fields: &[ast::FieldDef], span: rustc_span::Span) {
self.nbsp();
self.bopen();
let empty = fields.is_empty();
if !empty {
self.hardbreak_if_not_bol();
for field in fields {
@ -1453,8 +1470,9 @@ impl<'a> State<'a> {
self.print_type(&field.ty);
self.s.word(",");
}
}
self.bclose(span)
self.bclose(span, empty);
}
crate fn print_struct(
@ -1633,7 +1651,8 @@ impl<'a> State<'a> {
}
}
self.bclose_maybe_open(blk.span, close_box);
let empty = attrs.is_empty() && blk.stmts.is_empty();
self.bclose_maybe_open(blk.span, empty, close_box);
self.ann.post(self, AnnNode::Block(blk))
}
@ -2010,7 +2029,8 @@ impl<'a> State<'a> {
for arm in arms {
self.print_arm(arm);
}
self.bclose(expr.span);
let empty = attrs.is_empty() && arms.is_empty();
self.bclose(expr.span, empty);
}
ast::ExprKind::Closure(
capture_clause,

View file

@ -2174,7 +2174,7 @@ impl<'a> State<'a> {
match decl.output {
hir::FnRetTy::Return(ref ty) => {
self.print_type(&ty);
self.maybe_print_comment(ty.span.lo())
self.maybe_print_comment(ty.span.lo());
}
hir::FnRetTy::DefaultReturn(..) => unreachable!(),
}
@ -2368,7 +2368,7 @@ impl<'a> State<'a> {
self.end();
if let hir::FnRetTy::Return(ref output) = decl.output {
self.maybe_print_comment(output.span.lo())
self.maybe_print_comment(output.span.lo());
}
}

View file

@ -1,8 +1,7 @@
// compile-flags: --crate-type=lib
// pp-exact
fn f() {
} /*
fn f() {} /*
The next line should not be indented.
That one. It shouldn't have been indented.

View file

@ -13,7 +13,6 @@ extern crate std;
// [pretty]compile-flags: -Zunpretty=everybody_loops
// [pretty]check-pass
#[repr("C")]
struct A {
}
struct A {}
fn main() { loop {} }