Skip to content

Commit 6a58d91

Browse files
committed
Coalesce trailing comma options
1 parent 7ad3522 commit 6a58d91

10 files changed

+14
-213
lines changed

src/config.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ configuration_option_enum! { TypeDensity:
104104
Wide,
105105
}
106106

107+
107108
impl Density {
108109
pub fn to_list_tactic(self) -> ListTactic {
109110
match self {
@@ -348,6 +349,8 @@ create_config! {
348349
control_brace_style: ControlBraceStyle, ControlBraceStyle::AlwaysSameLine,
349350
"Brace style for control flow constructs";
350351
impl_empty_single_line: bool, true, "Put empty-body implementations on a single line";
352+
trailing_comma: SeparatorTactic, SeparatorTactic::Vertical,
353+
"How to handle trailing commas for lists";
351354
fn_empty_single_line: bool, true, "Put empty-body functions on a single line";
352355
fn_single_line: bool, false, "Put single-expression functions on a single line";
353356
fn_return_indent: ReturnIndent, ReturnIndent::WithArgs,
@@ -366,16 +369,10 @@ create_config! {
366369
where_layout: ListTactic, ListTactic::Vertical, "Element layout inside a where clause";
367370
where_pred_indent: BlockIndentStyle, BlockIndentStyle::Visual,
368371
"Indentation style of a where predicate";
369-
where_trailing_comma: bool, false, "Put a trailing comma on where clauses";
370372
generics_indent: BlockIndentStyle, BlockIndentStyle::Visual, "Indentation of generics";
371-
struct_trailing_comma: SeparatorTactic, SeparatorTactic::Vertical,
372-
"If there is a trailing comma on structs";
373-
struct_lit_trailing_comma: SeparatorTactic, SeparatorTactic::Vertical,
374-
"If there is a trailing comma on literal structs";
375373
struct_lit_style: StructLitStyle, StructLitStyle::Block, "Style of struct definition";
376374
struct_lit_multiline_style: MultilineStyle, MultilineStyle::PreferSingle,
377375
"Multiline style on literal structs";
378-
enum_trailing_comma: bool, true, "Put a trailing comma on enum declarations";
379376
report_todo: ReportTactic, ReportTactic::Never,
380377
"Report all, none or unnumbered occurrences of TODO in source file comments";
381378
report_fixme: ReportTactic, ReportTactic::Never,
@@ -396,7 +393,6 @@ create_config! {
396393
wrap_match_arms: bool, true, "Wrap multiline match arms in blocks";
397394
match_block_trailing_comma: bool, false,
398395
"Put a trailing comma after a block based match arm (non-block arms are not affected)";
399-
match_wildcard_trailing_comma: bool, true, "Put a trailing comma after a wildcard arm";
400396
closure_block_indent_threshold: isize, 5, "How many lines a closure must have before it is \
401397
block indented. -1 means never use block indent.";
402398
space_before_type_annotation: bool, false,

src/expr.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -1150,12 +1150,6 @@ fn arm_end_pos(arm: &ast::Arm) -> BytePos {
11501150
}
11511151

11521152
fn arm_comma(config: &Config, arm: &ast::Arm, body: &ast::Expr) -> &'static str {
1153-
if !config.match_wildcard_trailing_comma {
1154-
if arm.pats.len() == 1 && arm.pats[0].node == ast::PatKind::Wild && arm.guard.is_none() {
1155-
return "";
1156-
}
1157-
}
1158-
11591153
if config.match_block_trailing_comma {
11601154
","
11611155
} else if let ast::ExprKind::Block(ref block) = body.node {
@@ -1759,7 +1753,7 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
17591753
trailing_separator: if base.is_some() {
17601754
SeparatorTactic::Never
17611755
} else {
1762-
context.config.struct_lit_trailing_comma
1756+
context.config.trailing_comma
17631757
},
17641758
shape: nested_shape,
17651759
ends_with_newline: ends_with_newline,

src/items.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ impl<'a> FmtVisitor<'a> {
434434
let fmt = ListFormatting {
435435
tactic: DefinitiveListTactic::Vertical,
436436
separator: ",",
437-
trailing_separator: SeparatorTactic::from_bool(self.config.enum_trailing_comma),
437+
trailing_separator: self.config.trailing_comma,
438438
shape: Shape::legacy(budget, self.block_indent),
439439
ends_with_newline: true,
440440
config: self.config,
@@ -526,7 +526,6 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -
526526
offset.block_only()),
527527
context.config.where_density,
528528
"{",
529-
true,
530529
None));
531530

532531
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)
795794
offset.block_only()),
796795
where_density,
797796
"{",
798-
has_body,
799797
None));
800798
// If the where clause cannot fit on the same line,
801799
// put the where clause on a new line
@@ -952,7 +950,7 @@ fn format_struct_struct(context: &RewriteContext,
952950
let fmt = ListFormatting {
953951
tactic: tactic,
954952
separator: ",",
955-
trailing_separator: context.config.struct_trailing_comma,
953+
trailing_separator: context.config.trailing_comma,
956954
shape: Shape::legacy(budget, item_indent),
957955
ends_with_newline: true,
958956
config: context.config,
@@ -1010,7 +1008,6 @@ fn format_tuple_struct(context: &RewriteContext,
10101008
Shape::legacy(where_budget, offset.block_only()),
10111009
Density::Compressed,
10121010
";",
1013-
false,
10141011
None))
10151012
}
10161013
None => "".to_owned(),
@@ -1103,7 +1100,6 @@ pub fn rewrite_type_alias(context: &RewriteContext,
11031100
Shape::legacy(where_budget, indent),
11041101
context.config.where_density,
11051102
"=",
1106-
false,
11071103
Some(span.hi)));
11081104
result.push_str(&where_clause_str);
11091105
result.push_str(" = ");
@@ -1645,7 +1641,6 @@ fn rewrite_fn_base(context: &RewriteContext,
16451641
Shape::legacy(where_budget, indent),
16461642
where_density,
16471643
"{",
1648-
has_body,
16491644
Some(span.hi)));
16501645

16511646
if last_line_width(&result) + where_clause_str.len() > context.config.max_width &&
@@ -1929,7 +1924,6 @@ fn rewrite_where_clause(context: &RewriteContext,
19291924
shape: Shape,
19301925
density: Density,
19311926
terminator: &str,
1932-
allow_trailing_comma: bool,
19331927
span_end: Option<BytePos>)
19341928
-> Option<String> {
19351929
if where_clause.predicates.is_empty() {
@@ -1969,12 +1963,17 @@ fn rewrite_where_clause(context: &RewriteContext,
19691963
// FIXME: we don't need to collect here if the where_layout isn't
19701964
// HorizontalVertical.
19711965
let tactic = definitive_tactic(&item_vec, context.config.where_layout, budget);
1972-
let use_trailing_comma = allow_trailing_comma && context.config.where_trailing_comma;
1966+
1967+
let mut comma_tactic = context.config.trailing_comma;
1968+
// Kind of a hack because we don't usually have trailing commas in where clauses.
1969+
if comma_tactic == SeparatorTactic::Vertical {
1970+
comma_tactic = SeparatorTactic::Never;
1971+
}
19731972

19741973
let fmt = ListFormatting {
19751974
tactic: tactic,
19761975
separator: ",",
1977-
trailing_separator: SeparatorTactic::from_bool(use_trailing_comma),
1976+
trailing_separator: comma_tactic,
19781977
shape: Shape::legacy(budget, offset),
19791978
ends_with_newline: true,
19801979
config: context.config,
@@ -2034,7 +2033,6 @@ fn format_generics(context: &RewriteContext,
20342033
offset.block_only()),
20352034
Density::Tall,
20362035
terminator,
2037-
true,
20382036
Some(span.hi)));
20392037
result.push_str(&where_clause_str);
20402038
if !force_same_line_brace &&

tests/config/small_tabs.toml

+1-4
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@ where_density = "Tall"
1212
where_indent = "Tabbed"
1313
where_layout = "Vertical"
1414
where_pred_indent = "Visual"
15-
where_trailing_comma = false
1615
generics_indent = "Visual"
17-
struct_trailing_comma = "Vertical"
18-
struct_lit_trailing_comma = "Vertical"
16+
trailing_comma = "Vertical"
1917
struct_lit_style = "Block"
20-
enum_trailing_comma = true
2118
report_todo = "Always"
2219
report_fixme = "Never"
2320
reorder_imports = false

tests/source/enum-no_trailing_comma.rs

-41
This file was deleted.

tests/source/match-wildcard-trailing-comma.rs

-10
This file was deleted.

tests/source/where-trailing-comma.rs

-36
This file was deleted.

tests/target/enum-no_trailing_comma.rs

-31
This file was deleted.

tests/target/match-wildcard-trailing-comma.rs

-10
This file was deleted.

tests/target/where-trailing-comma.rs

-56
This file was deleted.

0 commit comments

Comments
 (0)