about summary refs log tree commit diff
diff options
context:
space:
mode:
authorsinkuu <sinkuu@users.noreply.github.com>2017-01-09 12:11:12 +0900
committerNick Cameron <nrc@ncameron.org>2017-01-09 16:11:12 +1300
commit7e2fcc27e1b359efbdcc16b0aaabe6f7e09d8b4a (patch)
tree992551ce1a8696b6d2f74c7011bd28bb3a00845a
parentad46f9af95e3eefdd0e0b46084aba50568642aea (diff)
Fix #1258 (#1266)
* Fix #1258

* Add test
-rw-r--r--src/items.rs13
-rw-r--r--tests/source/structs.rs11
-rw-r--r--tests/target/structs.rs8
3 files changed, 31 insertions, 1 deletions
diff --git a/src/items.rs b/src/items.rs
index dafb7095002..6a187bad7d5 100644
--- a/src/items.rs
+++ b/src/items.rs
@@ -839,7 +839,18 @@ fn format_struct_struct(context: &RewriteContext,
 
     // FIXME(#919): properly format empty structs and their comments.
     if fields.is_empty() {
-        result.push_str(&context.snippet(mk_sp(body_lo, span.hi)));
+        let snippet = context.snippet(mk_sp(body_lo, span.hi - BytePos(1)));
+        if snippet.trim().is_empty() {
+            // `struct S {}`
+        } else if snippet.trim_right_matches(&[' ', '\t'][..]).ends_with('\n') {
+            // fix indent
+            result.push_str(&snippet.trim_right());
+            result.push('\n');
+            result.push_str(&offset.to_string(context.config));
+        } else {
+            result.push_str(&snippet);
+        }
+        result.push('}');
         return Some(result);
     }
 
diff --git a/tests/source/structs.rs b/tests/source/structs.rs
index ff297655713..5a13d59f572 100644
--- a/tests/source/structs.rs
+++ b/tests/source/structs.rs
@@ -153,4 +153,15 @@ struct Issue677 {
 }
 
 struct Foo {}
+struct Foo {
+    }
+struct Foo {
+    // comment
+    }
+struct Foo {
+    // trailing space ->    
+
+
+    }
+struct Foo { /* comment */ }
 struct Foo();
diff --git a/tests/target/structs.rs b/tests/target/structs.rs
index 0af68686318..3d3b20a9ad6 100644
--- a/tests/target/structs.rs
+++ b/tests/target/structs.rs
@@ -161,4 +161,12 @@ struct Issue677 {
 }
 
 struct Foo {}
+struct Foo {}
+struct Foo {
+    // comment
+}
+struct Foo {
+    // trailing space ->
+}
+struct Foo { /* comment */ }
 struct Foo();