From 7e2fcc27e1b359efbdcc16b0aaabe6f7e09d8b4a Mon Sep 17 00:00:00 2001 From: sinkuu Date: Mon, 9 Jan 2017 12:11:12 +0900 Subject: [PATCH] Fix #1258 (#1266) * Fix #1258 * Add test --- src/items.rs | 13 ++++++++++++- tests/source/structs.rs | 11 +++++++++++ tests/target/structs.rs | 8 ++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/items.rs b/src/items.rs index dafb70950028..6a187bad7d5e 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 ff297655713f..5a13d59f5727 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 0af68686318f..3d3b20a9ad6f 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();