diff --git a/src/visitor.rs b/src/visitor.rs index 569bacc1300..073a05ef2bc 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -454,11 +454,19 @@ impl<'a> FmtVisitor<'a> { if is_internal { self.buffer.push_str(" {"); - self.last_pos = ::utils::span_after(s, "{", self.codemap); - self.block_indent = self.block_indent.block_indent(self.config); - self.walk_mod_items(m); - self.format_missing_with_indent(m.inner.hi - BytePos(1)); - self.close_block(); + // Hackery to account for the closing }. + let mod_lo = ::utils::span_after(s, "{", self.codemap); + let body_snippet = self.snippet(codemap::mk_sp(mod_lo, m.inner.hi - BytePos(1))); + let body_snippet = body_snippet.trim(); + if body_snippet.is_empty() { + self.buffer.push_str("}"); + } else { + self.last_pos = mod_lo; + self.block_indent = self.block_indent.block_indent(self.config); + self.walk_mod_items(m); + self.format_missing_with_indent(m.inner.hi - BytePos(1)); + self.close_block(); + } self.last_pos = m.inner.hi; } else { self.buffer.push_str(";"); diff --git a/tests/target/comment2.rs b/tests/target/comment2.rs index cecb7b0d95f..04f84a15c95 100644 --- a/tests/target/comment2.rs +++ b/tests/target/comment2.rs @@ -2,5 +2,4 @@ /// This is a long line that angers rustfmt. Rustfmt shall deal with it swiftly /// and justly. -pub mod foo { -} +pub mod foo {} diff --git a/tests/target/comment3.rs b/tests/target/comment3.rs index 24598931ed1..3a810590d7b 100644 --- a/tests/target/comment3.rs +++ b/tests/target/comment3.rs @@ -3,5 +3,4 @@ //! This is a long line that angers rustfmt. Rustfmt shall deal with it swiftly //! and justly. -pub mod foo { -} +pub mod foo {} diff --git a/tests/target/mod-1.rs b/tests/target/mod-1.rs index 6fe2e65c565..abf25b5e649 100644 --- a/tests/target/mod-1.rs +++ b/tests/target/mod-1.rs @@ -2,8 +2,7 @@ mod foo { mod bar { - mod baz { - } + mod baz {} } } @@ -16,9 +15,7 @@ mod foo { } } - mod qux { - - } + mod qux {} } mod boxed {