From cbccc4a59770f5124762e5fdb2085ff8b04e51cd Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 28 Dec 2021 12:26:18 -0800 Subject: [PATCH] Remove pretty printer space inside block with only outer attrs --- compiler/rustc_ast_pretty/src/pprust/state.rs | 23 ++++++++++--------- src/test/pretty/attr-fn-inner.rs | 4 ++-- src/test/pretty/attr-literals.rs | 4 ++-- src/test/pretty/attr-tokens-raw-ident.rs | 2 +- src/test/pretty/delimited-token-groups.rs | 2 +- src/test/pretty/doc-comments.rs | 10 ++++---- src/test/ui/macros/stringify.rs | 2 +- 7 files changed, 24 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index 6c5b38bc4bb..eb6ea8e5fe4 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -387,23 +387,23 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere self.print_string(sym.as_str(), style); } - fn print_inner_attributes(&mut self, attrs: &[ast::Attribute]) { + fn print_inner_attributes(&mut self, attrs: &[ast::Attribute]) -> bool { self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, true) } - fn print_inner_attributes_no_trailing_hardbreak(&mut self, attrs: &[ast::Attribute]) { + fn print_inner_attributes_no_trailing_hardbreak(&mut self, attrs: &[ast::Attribute]) -> bool { self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, false) } - fn print_outer_attributes(&mut self, attrs: &[ast::Attribute]) { + fn print_outer_attributes(&mut self, attrs: &[ast::Attribute]) -> bool { self.print_either_attributes(attrs, ast::AttrStyle::Outer, false, true) } - fn print_inner_attributes_inline(&mut self, attrs: &[ast::Attribute]) { + fn print_inner_attributes_inline(&mut self, attrs: &[ast::Attribute]) -> bool { self.print_either_attributes(attrs, ast::AttrStyle::Inner, true, true) } - fn print_outer_attributes_inline(&mut self, attrs: &[ast::Attribute]) { + fn print_outer_attributes_inline(&mut self, attrs: &[ast::Attribute]) -> bool { self.print_either_attributes(attrs, ast::AttrStyle::Outer, true, true) } @@ -413,20 +413,21 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere kind: ast::AttrStyle, is_inline: bool, trailing_hardbreak: bool, - ) { - let mut count = 0; + ) -> bool { + let mut printed = false; for attr in attrs { if attr.style == kind { self.print_attribute_inline(attr, is_inline); if is_inline { self.nbsp(); } - count += 1; + printed = true; } } - if count > 0 && trailing_hardbreak && !is_inline { + if printed && trailing_hardbreak && !is_inline { self.hardbreak_if_not_bol(); } + printed } fn print_attribute(&mut self, attr: &ast::Attribute) { @@ -1646,7 +1647,7 @@ impl<'a> State<'a> { self.ann.pre(self, AnnNode::Block(blk)); self.bopen(); - self.print_inner_attributes(attrs); + let has_attrs = self.print_inner_attributes(attrs); for (i, st) in blk.stmts.iter().enumerate() { match st.kind { @@ -1660,7 +1661,7 @@ impl<'a> State<'a> { } } - let empty = attrs.is_empty() && blk.stmts.is_empty(); + let empty = !has_attrs && blk.stmts.is_empty(); self.bclose_maybe_open(blk.span, empty, close_box); self.ann.post(self, AnnNode::Block(blk)) } diff --git a/src/test/pretty/attr-fn-inner.rs b/src/test/pretty/attr-fn-inner.rs index 0a745e7d34f..6d9cb89f022 100644 --- a/src/test/pretty/attr-fn-inner.rs +++ b/src/test/pretty/attr-fn-inner.rs @@ -9,8 +9,8 @@ fn main() { #![rustc_dummy] #[rustc_dummy] - fn f() { } + fn f() {} #[rustc_dummy] - fn g() { } + fn g() {} } diff --git a/src/test/pretty/attr-literals.rs b/src/test/pretty/attr-literals.rs index 44d2c5db3e6..d132014420d 100644 --- a/src/test/pretty/attr-literals.rs +++ b/src/test/pretty/attr-literals.rs @@ -7,8 +7,8 @@ fn main() { #![rustc_dummy("hi", 1, 2, 1.012, pi = 3.14, bye, name("John"))] #[rustc_dummy = 8] - fn f() { } + fn f() {} #[rustc_dummy(1, 2, 3)] - fn g() { } + fn g() {} } diff --git a/src/test/pretty/attr-tokens-raw-ident.rs b/src/test/pretty/attr-tokens-raw-ident.rs index bb2c4bb558e..8486342b087 100644 --- a/src/test/pretty/attr-tokens-raw-ident.rs +++ b/src/test/pretty/attr-tokens-raw-ident.rs @@ -4,4 +4,4 @@ // pp-exact #[rustfmt::r#final(final)] -fn main() { } +fn main() {} diff --git a/src/test/pretty/delimited-token-groups.rs b/src/test/pretty/delimited-token-groups.rs index 257c032b536..1137d804564 100644 --- a/src/test/pretty/delimited-token-groups.rs +++ b/src/test/pretty/delimited-token-groups.rs @@ -45,4 +45,4 @@ mac! { }] #[rustc_dummy = "aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa"] -fn main() { } +fn main() {} diff --git a/src/test/pretty/doc-comments.rs b/src/test/pretty/doc-comments.rs index 2a98c9fc058..a49860daa6a 100644 --- a/src/test/pretty/doc-comments.rs +++ b/src/test/pretty/doc-comments.rs @@ -5,7 +5,7 @@ // some single-line non-doc comment /// some single line outer-docs -fn a() { } +fn a() {} fn b() { //! some single line inner-docs @@ -17,7 +17,7 @@ fn b() { ////////////////////////////////// /// some single-line outer-docs preceded by a separator /// (and trailing whitespaces) -fn c() { } +fn c() {} /* * some multi-line non-doc comment @@ -26,7 +26,7 @@ fn c() { } /** * some multi-line outer-docs */ -fn d() { } +fn d() {} fn e() { /*! @@ -43,10 +43,10 @@ fn e() { /** * some multi-line outer-docs preceded by a separator */ -fn f() { } +fn f() {} #[doc = "unsugared outer doc-comments work also"] -fn g() { } +fn g() {} fn h() { #![doc = "as do inner ones"] diff --git a/src/test/ui/macros/stringify.rs b/src/test/ui/macros/stringify.rs index fcf6a9278d8..f284ac727f4 100644 --- a/src/test/ui/macros/stringify.rs +++ b/src/test/ui/macros/stringify.rs @@ -235,7 +235,7 @@ fn test_expr() { #[attr] {} ), - "#[attr] { }", // FIXME + "#[attr] {}", ); assert_eq!( stringify_expr!(