Skip to content

Commit 57ac92b

Browse files
ytmimicalebcartwright
authored andcommitted
Prevent duplicate comma when formatting struct pattern with ".."
Fixes 5066 When a struct pattern that contained a ".." was formatted, it was assumed that a trailing comma should always be added if the struct fields weren't formatted vertically. Now, a trailing comma is only added if not already included in the reformatted struct fields.
1 parent 740fb57 commit 57ac92b

7 files changed

+54
-3
lines changed

Diff for: src/patterns.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -318,19 +318,20 @@ fn rewrite_struct_pat(
318318
let mut fields_str = write_list(&item_vec, &fmt)?;
319319
let one_line_width = h_shape.map_or(0, |shape| shape.width);
320320

321+
let has_trailing_comma = fmt.needs_trailing_separator();
322+
321323
if ellipsis {
322324
if fields_str.contains('\n') || fields_str.len() > one_line_width {
323325
// Add a missing trailing comma.
324-
if context.config.trailing_comma() == SeparatorTactic::Never {
326+
if !has_trailing_comma {
325327
fields_str.push(',');
326328
}
327329
fields_str.push('\n');
328330
fields_str.push_str(&nested_shape.indent.to_string(context.config));
329331
} else {
330332
if !fields_str.is_empty() {
331333
// there are preceding struct fields being matched on
332-
if tactic == DefinitiveListTactic::Vertical {
333-
// if the tactic is Vertical, write_list already added a trailing ,
334+
if has_trailing_comma {
334335
fields_str.push(' ');
335336
} else {
336337
fields_str.push_str(", ");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-trailing_comma: Always
2+
// rustfmt-struct_lit_single_line: false
3+
// rustfmt-struct_lit_width: 0
4+
5+
fn main() {
6+
let Foo {
7+
a,
8+
..
9+
} = b;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-trailing_comma: Never
2+
// rustfmt-struct_lit_single_line: false
3+
// rustfmt-struct_lit_width: 0
4+
5+
fn main() {
6+
let Foo {
7+
a,
8+
..
9+
} = b;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-trailing_comma: Always
2+
// rustfmt-struct_lit_single_line: false
3+
4+
// There is an issue with how this is formatted.
5+
// formatting should look like ./multi_line_struct_trailing_comma_always_struct_lit_width_0.rs
6+
fn main() {
7+
let Foo {
8+
a, ..
9+
} = b;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-trailing_comma: Never
2+
// rustfmt-struct_lit_single_line: false
3+
4+
// There is an issue with how this is formatted.
5+
// formatting should look like ./multi_line_struct_trailing_comma_never_struct_lit_width_0.rs
6+
fn main() {
7+
let Foo {
8+
a, ..
9+
} = b;
10+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-trailing_comma: Always
2+
3+
fn main() {
4+
let Foo { a, .. } = b;
5+
}

Diff for: tests/target/issue-5066/with_trailing_comma_never.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-trailing_comma: Never
2+
3+
fn main() {
4+
let Foo { a, .. } = b;
5+
}

0 commit comments

Comments
 (0)