From 6a58d9123952de104b1e02640261b7e90908bb0e Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Fri, 24 Feb 2017 10:31:23 +1300 Subject: [PATCH] Coalesce trailing comma options --- src/config.rs | 10 +--- src/expr.rs | 8 +-- src/items.rs | 20 +++---- tests/config/small_tabs.toml | 5 +- tests/source/enum-no_trailing_comma.rs | 41 -------------- tests/source/match-wildcard-trailing-comma.rs | 10 ---- tests/source/where-trailing-comma.rs | 36 ------------ tests/target/enum-no_trailing_comma.rs | 31 ---------- tests/target/match-wildcard-trailing-comma.rs | 10 ---- tests/target/where-trailing-comma.rs | 56 ------------------- 10 files changed, 14 insertions(+), 213 deletions(-) delete mode 100644 tests/source/enum-no_trailing_comma.rs delete mode 100644 tests/source/match-wildcard-trailing-comma.rs delete mode 100644 tests/source/where-trailing-comma.rs delete mode 100644 tests/target/enum-no_trailing_comma.rs delete mode 100644 tests/target/match-wildcard-trailing-comma.rs delete mode 100644 tests/target/where-trailing-comma.rs diff --git a/src/config.rs b/src/config.rs index c6c2beae56a..57b8ba190f8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -104,6 +104,7 @@ pub enum $e { Wide, } + impl Density { pub fn to_list_tactic(self) -> ListTactic { match self { @@ -348,6 +349,8 @@ fn default() -> Config { control_brace_style: ControlBraceStyle, ControlBraceStyle::AlwaysSameLine, "Brace style for control flow constructs"; impl_empty_single_line: bool, true, "Put empty-body implementations on a single line"; + trailing_comma: SeparatorTactic, SeparatorTactic::Vertical, + "How to handle trailing commas for lists"; fn_empty_single_line: bool, true, "Put empty-body functions on a single line"; fn_single_line: bool, false, "Put single-expression functions on a single line"; fn_return_indent: ReturnIndent, ReturnIndent::WithArgs, @@ -366,16 +369,10 @@ fn default() -> Config { where_layout: ListTactic, ListTactic::Vertical, "Element layout inside a where clause"; where_pred_indent: BlockIndentStyle, BlockIndentStyle::Visual, "Indentation style of a where predicate"; - where_trailing_comma: bool, false, "Put a trailing comma on where clauses"; generics_indent: BlockIndentStyle, BlockIndentStyle::Visual, "Indentation of generics"; - struct_trailing_comma: SeparatorTactic, SeparatorTactic::Vertical, - "If there is a trailing comma on structs"; - struct_lit_trailing_comma: SeparatorTactic, SeparatorTactic::Vertical, - "If there is a trailing comma on literal structs"; struct_lit_style: StructLitStyle, StructLitStyle::Block, "Style of struct definition"; struct_lit_multiline_style: MultilineStyle, MultilineStyle::PreferSingle, "Multiline style on literal structs"; - enum_trailing_comma: bool, true, "Put a trailing comma on enum declarations"; report_todo: ReportTactic, ReportTactic::Never, "Report all, none or unnumbered occurrences of TODO in source file comments"; report_fixme: ReportTactic, ReportTactic::Never, @@ -396,7 +393,6 @@ fn default() -> Config { wrap_match_arms: bool, true, "Wrap multiline match arms in blocks"; match_block_trailing_comma: bool, false, "Put a trailing comma after a block based match arm (non-block arms are not affected)"; - match_wildcard_trailing_comma: bool, true, "Put a trailing comma after a wildcard arm"; closure_block_indent_threshold: isize, 5, "How many lines a closure must have before it is \ block indented. -1 means never use block indent."; space_before_type_annotation: bool, false, diff --git a/src/expr.rs b/src/expr.rs index 52c0aee279d..cbec7d0a8af 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1150,12 +1150,6 @@ fn arm_end_pos(arm: &ast::Arm) -> BytePos { } fn arm_comma(config: &Config, arm: &ast::Arm, body: &ast::Expr) -> &'static str { - if !config.match_wildcard_trailing_comma { - if arm.pats.len() == 1 && arm.pats[0].node == ast::PatKind::Wild && arm.guard.is_none() { - return ""; - } - } - if config.match_block_trailing_comma { "," } else if let ast::ExprKind::Block(ref block) = body.node { @@ -1759,7 +1753,7 @@ enum StructLitField<'a> { trailing_separator: if base.is_some() { SeparatorTactic::Never } else { - context.config.struct_lit_trailing_comma + context.config.trailing_comma }, shape: nested_shape, ends_with_newline: ends_with_newline, diff --git a/src/items.rs b/src/items.rs index 4e5dc9d5765..45e2175b801 100644 --- a/src/items.rs +++ b/src/items.rs @@ -434,7 +434,7 @@ fn format_variant_list(&self, let fmt = ListFormatting { tactic: DefinitiveListTactic::Vertical, separator: ",", - trailing_separator: SeparatorTactic::from_bool(self.config.enum_trailing_comma), + trailing_separator: self.config.trailing_comma, shape: Shape::legacy(budget, self.block_indent), ends_with_newline: true, config: self.config, @@ -526,7 +526,6 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) - offset.block_only()), context.config.where_density, "{", - true, None)); if try_opt!(is_impl_single_line(context, &items, &result, &where_clause_str, &item)) { @@ -795,7 +794,6 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) offset.block_only()), where_density, "{", - has_body, None)); // If the where clause cannot fit on the same line, // put the where clause on a new line @@ -952,7 +950,7 @@ fn format_struct_struct(context: &RewriteContext, let fmt = ListFormatting { tactic: tactic, separator: ",", - trailing_separator: context.config.struct_trailing_comma, + trailing_separator: context.config.trailing_comma, shape: Shape::legacy(budget, item_indent), ends_with_newline: true, config: context.config, @@ -1010,7 +1008,6 @@ fn format_tuple_struct(context: &RewriteContext, Shape::legacy(where_budget, offset.block_only()), Density::Compressed, ";", - false, None)) } None => "".to_owned(), @@ -1103,7 +1100,6 @@ pub fn rewrite_type_alias(context: &RewriteContext, Shape::legacy(where_budget, indent), context.config.where_density, "=", - false, Some(span.hi))); result.push_str(&where_clause_str); result.push_str(" = "); @@ -1645,7 +1641,6 @@ fn rewrite_fn_base(context: &RewriteContext, Shape::legacy(where_budget, indent), where_density, "{", - has_body, Some(span.hi))); if last_line_width(&result) + where_clause_str.len() > context.config.max_width && @@ -1929,7 +1924,6 @@ fn rewrite_where_clause(context: &RewriteContext, shape: Shape, density: Density, terminator: &str, - allow_trailing_comma: bool, span_end: Option) -> Option { if where_clause.predicates.is_empty() { @@ -1969,12 +1963,17 @@ fn rewrite_where_clause(context: &RewriteContext, // FIXME: we don't need to collect here if the where_layout isn't // HorizontalVertical. let tactic = definitive_tactic(&item_vec, context.config.where_layout, budget); - let use_trailing_comma = allow_trailing_comma && context.config.where_trailing_comma; + + let mut comma_tactic = context.config.trailing_comma; + // Kind of a hack because we don't usually have trailing commas in where clauses. + if comma_tactic == SeparatorTactic::Vertical { + comma_tactic = SeparatorTactic::Never; + } let fmt = ListFormatting { tactic: tactic, separator: ",", - trailing_separator: SeparatorTactic::from_bool(use_trailing_comma), + trailing_separator: comma_tactic, shape: Shape::legacy(budget, offset), ends_with_newline: true, config: context.config, @@ -2034,7 +2033,6 @@ fn format_generics(context: &RewriteContext, offset.block_only()), Density::Tall, terminator, - true, Some(span.hi))); result.push_str(&where_clause_str); if !force_same_line_brace && diff --git a/tests/config/small_tabs.toml b/tests/config/small_tabs.toml index 820978b8c32..a8150020e56 100644 --- a/tests/config/small_tabs.toml +++ b/tests/config/small_tabs.toml @@ -12,12 +12,9 @@ where_density = "Tall" where_indent = "Tabbed" where_layout = "Vertical" where_pred_indent = "Visual" -where_trailing_comma = false generics_indent = "Visual" -struct_trailing_comma = "Vertical" -struct_lit_trailing_comma = "Vertical" +trailing_comma = "Vertical" struct_lit_style = "Block" -enum_trailing_comma = true report_todo = "Always" report_fixme = "Never" reorder_imports = false diff --git a/tests/source/enum-no_trailing_comma.rs b/tests/source/enum-no_trailing_comma.rs deleted file mode 100644 index 4b0347ab526..00000000000 --- a/tests/source/enum-no_trailing_comma.rs +++ /dev/null @@ -1,41 +0,0 @@ -// 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/source/match-wildcard-trailing-comma.rs b/tests/source/match-wildcard-trailing-comma.rs deleted file mode 100644 index b13e85c2d90..00000000000 --- a/tests/source/match-wildcard-trailing-comma.rs +++ /dev/null @@ -1,10 +0,0 @@ -// rustfmt-match_wildcard_trailing_comma: false - -fn match_wild(x: i32) -> i32 { - match x { - 1 => 1, - 2 => 2, - 3 => 3, - _ => 0, - } -} diff --git a/tests/source/where-trailing-comma.rs b/tests/source/where-trailing-comma.rs deleted file mode 100644 index c2c1a3185bc..00000000000 --- a/tests/source/where-trailing-comma.rs +++ /dev/null @@ -1,36 +0,0 @@ -// rustfmt-where_trailing_comma: true - -fn f(x: T, y: S) -> T where T: P, S: Q -{ - x -} - -impl Trait for T where T: P -{ - fn f(x: T) -> T where T: Q + R - { - x - } -} - -struct Pair where T: P, S: P + Q { - a: T, - b: S -} - -struct TupPair (S, T) where T: P, S: P + Q; - -enum E where S: P, T: P { - A {a: T}, -} - -type Double where T: P, T: Q = Pair; - -extern "C" { - fn f(x: T, y: S) -> T where T: P, S: Q; -} - -trait Q where T: P, S: R -{ - fn f(self, x: T, y: S, z: U) -> Self where U: P, V: P; -} diff --git a/tests/target/enum-no_trailing_comma.rs b/tests/target/enum-no_trailing_comma.rs deleted file mode 100644 index 2e5a5ad23e3..00000000000 --- a/tests/target/enum-no_trailing_comma.rs +++ /dev/null @@ -1,31 +0,0 @@ -// 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/match-wildcard-trailing-comma.rs b/tests/target/match-wildcard-trailing-comma.rs deleted file mode 100644 index e0fbf81e045..00000000000 --- a/tests/target/match-wildcard-trailing-comma.rs +++ /dev/null @@ -1,10 +0,0 @@ -// rustfmt-match_wildcard_trailing_comma: false - -fn match_wild(x: i32) -> i32 { - match x { - 1 => 1, - 2 => 2, - 3 => 3, - _ => 0 - } -} diff --git a/tests/target/where-trailing-comma.rs b/tests/target/where-trailing-comma.rs deleted file mode 100644 index 4d4d19cdef8..00000000000 --- a/tests/target/where-trailing-comma.rs +++ /dev/null @@ -1,56 +0,0 @@ -// rustfmt-where_trailing_comma: true - -fn f(x: T, y: S) -> T - where T: P, - S: Q, -{ - x -} - -impl Trait for T - where T: P, -{ - fn f(x: T) -> T - where T: Q + R, - { - x - } -} - -struct Pair - where T: P, - S: P + Q, -{ - a: T, - b: S, -} - -struct TupPair(S, T) - where T: P, - S: P + Q; - -enum E - where S: P, - T: P, -{ - A { a: T }, -} - -type Double - where T: P, - T: Q = Pair; - -extern "C" { - fn f(x: T, y: S) -> T - where T: P, - S: Q; -} - -trait Q - where T: P, - S: R, -{ - fn f(self, x: T, y: S, z: U) -> Self - where U: P, - V: P; -} -- 2.44.0