From 2aa35f0f6deb1f24c508077e877d1bf95bcc282b Mon Sep 17 00:00:00 2001 From: Florian Zeitz Date: Tue, 3 Nov 2015 23:14:37 +0100 Subject: [PATCH] Honor "enum_trailing_comma" option. Fixes #556 --- src/items.rs | 2 +- src/lists.rs | 10 +++++++ tests/source/enum-no_trailing_comma.rs | 41 ++++++++++++++++++++++++++ tests/target/enum-no_trailing_comma.rs | 41 ++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 tests/source/enum-no_trailing_comma.rs create mode 100644 tests/target/enum-no_trailing_comma.rs diff --git a/src/items.rs b/src/items.rs index e6b401d2ff3..fa15eeca3ac 100644 --- a/src/items.rs +++ b/src/items.rs @@ -693,7 +693,7 @@ impl<'a> FmtVisitor<'a> { let fmt = ListFormatting { tactic: DefinitiveListTactic::Vertical, separator: ",", - trailing_separator: SeparatorTactic::Always, + trailing_separator: SeparatorTactic::from_bool(self.config.enum_trailing_comma), indent: self.block_indent, width: budget, ends_with_newline: true, diff --git a/src/lists.rs b/src/lists.rs index 8182189178a..b05e4d46066 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -46,6 +46,16 @@ pub enum SeparatorTactic { impl_enum_decodable!(SeparatorTactic, Always, Never, Vertical); +impl SeparatorTactic { + pub fn from_bool(b: bool) -> SeparatorTactic { + if b { + SeparatorTactic::Always + } else { + SeparatorTactic::Never + } + } +} + pub struct ListFormatting<'a> { pub tactic: DefinitiveListTactic, pub separator: &'a str, diff --git a/tests/source/enum-no_trailing_comma.rs b/tests/source/enum-no_trailing_comma.rs new file mode 100644 index 00000000000..4b0347ab526 --- /dev/null +++ b/tests/source/enum-no_trailing_comma.rs @@ -0,0 +1,41 @@ +// rustfmt-enum_trailing_comma: false + +enum X { + A, + B, +} + +enum Y { + A, + B +} + +enum TupX { + A(u32), + B(i32, u16), +} + +enum TupY { + A(u32), + B(i32, u16) +} + +enum StructX { + A { + s: u16, + }, + B { + u: u32, + i: i32, + }, +} + +enum StructY { + A { + s: u16, + }, + B { + u: u32, + i: i32, + } +} diff --git a/tests/target/enum-no_trailing_comma.rs b/tests/target/enum-no_trailing_comma.rs new file mode 100644 index 00000000000..4ad7d1f12d7 --- /dev/null +++ b/tests/target/enum-no_trailing_comma.rs @@ -0,0 +1,41 @@ +// rustfmt-enum_trailing_comma: false + +enum X { + A, + B +} + +enum Y { + A, + B +} + +enum TupX { + A(u32), + B(i32, u16) +} + +enum TupY { + A(u32), + B(i32, u16) +} + +enum StructX { + A { + s: u16, + }, + B { + u: u32, + i: i32, + } +} + +enum StructY { + A { + s: u16, + }, + B { + u: u32, + i: i32, + } +}