Skip to content

Commit

Permalink
rust-lang#4099: trailing_comma + struct_field_align_threshold -> remo…
Browse files Browse the repository at this point in the history
…ving a struct's commas (rust-lang#4201)
  • Loading branch information
theo-lw authored and bradleypmartin committed May 25, 2020
1 parent 5936675 commit 0a45e91
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
10 changes: 8 additions & 2 deletions rustfmt-core/rustfmt-lib/src/formatting/vertical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub(crate) fn rewrite_with_alignment<T: AlignedItem>(
let init_span = mk_sp(span.lo(), init_last_pos);
let one_line_width = if rest.is_empty() { one_line_width } else { 0 };
let result =
rewrite_aligned_items_inner(context, init, init_span, shape.indent, one_line_width)?;
rewrite_aligned_items_inner(context, init, rest, init_span, shape.indent, one_line_width)?;
if rest.is_empty() {
Some(result + spaces)
} else {
Expand Down Expand Up @@ -198,6 +198,7 @@ fn struct_field_prefix_max_min_width<T: AlignedItem>(
fn rewrite_aligned_items_inner<T: AlignedItem>(
context: &RewriteContext<'_>,
fields: &[T],
remaining_fields: &[T],
span: Span,
offset: Indent,
one_line_width: usize,
Expand Down Expand Up @@ -248,7 +249,12 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(

let fmt = ListFormatting::new(item_shape, context.config)
.tactic(tactic)
.trailing_separator(context.config.trailing_comma())
.trailing_separator(if remaining_fields.is_empty() {
// trailing commas should only be removed on the last field
context.config.trailing_comma()
} else {
SeparatorTactic::Always
})
.preserve_newline(true);
write_list(&items, &fmt)
}
Expand Down
30 changes: 30 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/source/issue-4099.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// rustfmt-struct_field_align_threshold: 127
// rustfmt-trailing_comma: Never

struct S {
aaa: f32,

bbb: f32,

ccc: f32,
}

struct S2 {
aaa: f32,
bbb: f32,
ccc: f32,
}

struct S3 {
aaa: f32,
bbb: f32,

ccc: f32,
}

struct S4 {
aaa: f32,

bbb: f32,
ccc: f32,
}
30 changes: 30 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/target/issue-4099.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// rustfmt-struct_field_align_threshold: 127
// rustfmt-trailing_comma: Never

struct S {
aaa: f32,

bbb: f32,

ccc: f32
}

struct S2 {
aaa: f32,
bbb: f32,
ccc: f32
}

struct S3 {
aaa: f32,
bbb: f32,

ccc: f32
}

struct S4 {
aaa: f32,

bbb: f32,
ccc: f32
}

0 comments on commit 0a45e91

Please sign in to comment.