Skip to content

Commit 15e7cab

Browse files
authored
#4099: trailing_comma + struct_field_align_threshold -> removing a struct's commas (#4201)
1 parent 001a0cc commit 15e7cab

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

rustfmt-core/rustfmt-lib/src/formatting/vertical.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ pub(crate) fn rewrite_with_alignment<T: AlignedItem>(
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 };
163163
let result =
164-
rewrite_aligned_items_inner(context, init, init_span, shape.indent, one_line_width)?;
164+
rewrite_aligned_items_inner(context, init, rest, init_span, shape.indent, one_line_width)?;
165165
if rest.is_empty() {
166166
Some(result + spaces)
167167
} else {
@@ -198,6 +198,7 @@ fn struct_field_prefix_max_min_width<T: AlignedItem>(
198198
fn rewrite_aligned_items_inner<T: AlignedItem>(
199199
context: &RewriteContext<'_>,
200200
fields: &[T],
201+
remaining_fields: &[T],
201202
span: Span,
202203
offset: Indent,
203204
one_line_width: usize,
@@ -248,7 +249,12 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
248249

249250
let fmt = ListFormatting::new(item_shape, context.config)
250251
.tactic(tactic)
251-
.trailing_separator(context.config.trailing_comma())
252+
.trailing_separator(if remaining_fields.is_empty() {
253+
// trailing commas should only be removed on the last field
254+
context.config.trailing_comma()
255+
} else {
256+
SeparatorTactic::Always
257+
})
252258
.preserve_newline(true);
253259
write_list(&items, &fmt)
254260
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// rustfmt-struct_field_align_threshold: 127
2+
// rustfmt-trailing_comma: Never
3+
4+
struct S {
5+
aaa: f32,
6+
7+
bbb: f32,
8+
9+
ccc: f32,
10+
}
11+
12+
struct S2 {
13+
aaa: f32,
14+
bbb: f32,
15+
ccc: f32,
16+
}
17+
18+
struct S3 {
19+
aaa: f32,
20+
bbb: f32,
21+
22+
ccc: f32,
23+
}
24+
25+
struct S4 {
26+
aaa: f32,
27+
28+
bbb: f32,
29+
ccc: f32,
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// rustfmt-struct_field_align_threshold: 127
2+
// rustfmt-trailing_comma: Never
3+
4+
struct S {
5+
aaa: f32,
6+
7+
bbb: f32,
8+
9+
ccc: f32
10+
}
11+
12+
struct S2 {
13+
aaa: f32,
14+
bbb: f32,
15+
ccc: f32
16+
}
17+
18+
struct S3 {
19+
aaa: f32,
20+
bbb: f32,
21+
22+
ccc: f32
23+
}
24+
25+
struct S4 {
26+
aaa: f32,
27+
28+
bbb: f32,
29+
ccc: f32
30+
}

0 commit comments

Comments
 (0)