Skip to content

Commit edcc4ec

Browse files
committed
Merge pull request #787 from rust-lang-nursery/mod-empty
Put empty modules on one line
2 parents 1d216e1 + a0e85f9 commit edcc4ec

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

src/visitor.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,19 @@ impl<'a> FmtVisitor<'a> {
454454

455455
if is_internal {
456456
self.buffer.push_str(" {");
457-
self.last_pos = ::utils::span_after(s, "{", self.codemap);
458-
self.block_indent = self.block_indent.block_indent(self.config);
459-
self.walk_mod_items(m);
460-
self.format_missing_with_indent(m.inner.hi - BytePos(1));
461-
self.close_block();
457+
// Hackery to account for the closing }.
458+
let mod_lo = ::utils::span_after(s, "{", self.codemap);
459+
let body_snippet = self.snippet(codemap::mk_sp(mod_lo, m.inner.hi - BytePos(1)));
460+
let body_snippet = body_snippet.trim();
461+
if body_snippet.is_empty() {
462+
self.buffer.push_str("}");
463+
} else {
464+
self.last_pos = mod_lo;
465+
self.block_indent = self.block_indent.block_indent(self.config);
466+
self.walk_mod_items(m);
467+
self.format_missing_with_indent(m.inner.hi - BytePos(1));
468+
self.close_block();
469+
}
462470
self.last_pos = m.inner.hi;
463471
} else {
464472
self.buffer.push_str(";");

tests/target/comment2.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22

33
/// This is a long line that angers rustfmt. Rustfmt shall deal with it swiftly
44
/// and justly.
5-
pub mod foo {
6-
}
5+
pub mod foo {}

tests/target/comment3.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
//! This is a long line that angers rustfmt. Rustfmt shall deal with it swiftly
44
//! and justly.
55
6-
pub mod foo {
7-
}
6+
pub mod foo {}

tests/target/mod-1.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
mod foo {
44
mod bar {
5-
mod baz {
6-
}
5+
mod baz {}
76
}
87
}
98

@@ -16,9 +15,7 @@ mod foo {
1615
}
1716
}
1817

19-
mod qux {
20-
21-
}
18+
mod qux {}
2219
}
2320

2421
mod boxed {

0 commit comments

Comments
 (0)