Skip to content

Commit ee13051

Browse files
cassaundracalebcartwright
authored andcommitted
Fix missing struct field separators under certain conditions
When struct_field_align_threshold is non-zero and trailing_comma is set to "Never," struct field separators are omitted between field groups. This issue is resolved by forcing separators between groups. Fixes #4791. A test is included with a minimal reproducible example.
1 parent 272fb42 commit ee13051

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

Diff for: src/vertical.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,18 @@ pub(crate) fn rewrite_with_alignment<T: AlignedItem>(
160160
};
161161
let init_span = mk_sp(span.lo(), init_last_pos);
162162
let one_line_width = if rest.is_empty() { one_line_width } else { 0 };
163-
let result =
164-
rewrite_aligned_items_inner(context, init, init_span, shape.indent, one_line_width)?;
163+
164+
// if another group follows, we must force a separator
165+
let force_separator = !rest.is_empty();
166+
167+
let result = rewrite_aligned_items_inner(
168+
context,
169+
init,
170+
init_span,
171+
shape.indent,
172+
one_line_width,
173+
force_separator,
174+
)?;
165175
if rest.is_empty() {
166176
Some(result + spaces)
167177
} else {
@@ -201,6 +211,7 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
201211
span: Span,
202212
offset: Indent,
203213
one_line_width: usize,
214+
force_trailing_separator: bool,
204215
) -> Option<String> {
205216
// 1 = ","
206217
let item_shape = Shape::indented(offset, context.config).sub_width(1)?;
@@ -246,9 +257,15 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
246257
});
247258
}
248259

260+
let separator_tactic = if force_trailing_separator {
261+
SeparatorTactic::Always
262+
} else {
263+
context.config.trailing_comma()
264+
};
265+
249266
let fmt = ListFormatting::new(item_shape, context.config)
250267
.tactic(tactic)
251-
.trailing_separator(context.config.trailing_comma())
268+
.trailing_separator(separator_tactic)
252269
.preserve_newline(true);
253270
write_list(&items, &fmt)
254271
}

Diff for: tests/source/issue_4791.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// rustfmt-struct_field_align_threshold: 30
2+
// rustfmt-trailing_comma: Never
3+
4+
struct Foo {
5+
group_a: u8,
6+
7+
group_b: u8,
8+
}
9+
10+
struct Bar {
11+
group_a: u8,
12+
13+
group_b: u8
14+
}

Diff for: tests/target/issue_4791.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// rustfmt-struct_field_align_threshold: 30
2+
// rustfmt-trailing_comma: Never
3+
4+
struct Foo {
5+
group_a: u8,
6+
7+
group_b: u8
8+
}
9+
10+
struct Bar {
11+
group_a: u8,
12+
13+
group_b: u8
14+
}

0 commit comments

Comments
 (0)