Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing struct field separators under certain conditions #5159

Merged
merged 3 commits into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/vertical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,18 @@ 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)?;

// if another group follows, we must force a separator
let force_separator = !rest.is_empty();

let result = rewrite_aligned_items_inner(
context,
init,
init_span,
shape.indent,
one_line_width,
force_separator,
)?;
if rest.is_empty() {
Some(result + spaces)
} else {
Expand Down Expand Up @@ -201,6 +211,7 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
span: Span,
offset: Indent,
one_line_width: usize,
force_trailing_separator: bool,
) -> Option<String> {
// 1 = ","
let item_shape = Shape::indented(offset, context.config).sub_width(1)?;
Expand Down Expand Up @@ -246,9 +257,15 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
});
}

let separator_tactic = if force_trailing_separator {
SeparatorTactic::Always
} else {
context.config.trailing_comma()
};

let fmt = ListFormatting::new(item_shape, context.config)
.tactic(tactic)
.trailing_separator(context.config.trailing_comma())
.trailing_separator(separator_tactic)
.preserve_newline(true);
write_list(&items, &fmt)
}
Expand Down
14 changes: 14 additions & 0 deletions tests/source/issue-4791/buggy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// rustfmt-struct_field_align_threshold: 30
// rustfmt-trailing_comma: Never

struct Foo {
group_a: u8,

group_b: u8,
}

struct Bar {
group_a: u8,

group_b: u8
}
14 changes: 14 additions & 0 deletions tests/source/issue-4791/trailing_comma.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// rustfmt-struct_field_align_threshold: 30
// rustfmt-trailing_comma: Always

struct Foo {
group_a: u8,

group_b: u8
}

struct Bar {
group_a: u8,

group_b: u8,
}
14 changes: 14 additions & 0 deletions tests/target/issue-4791/buggy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// rustfmt-struct_field_align_threshold: 30
// rustfmt-trailing_comma: Never

struct Foo {
group_a: u8,

group_b: u8
}

struct Bar {
group_a: u8,

group_b: u8
}
70 changes: 70 additions & 0 deletions tests/target/issue-4791/issue_4928.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// rustfmt-brace_style: SameLineWhere
// rustfmt-comment_width: 100
// rustfmt-edition: 2018
// rustfmt-fn_args_layout: Compressed
// rustfmt-hard_tabs: false
// rustfmt-match_block_trailing_comma: true
// rustfmt-max_width: 100
// rustfmt-merge_derives: false
// rustfmt-newline_style: Unix
// rustfmt-normalize_doc_attributes: true
// rustfmt-overflow_delimited_expr: true
// rustfmt-reorder_imports: false
// rustfmt-reorder_modules: true
// rustfmt-struct_field_align_threshold: 20
// rustfmt-tab_spaces: 4
// rustfmt-trailing_comma: Never
// rustfmt-use_small_heuristics: Max
// rustfmt-use_try_shorthand: true
// rustfmt-wrap_comments: true

/// Lorem ipsum dolor sit amet.
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
pub struct BufferAttr {
/* NOTE: Blah blah blah blah blah. */
/// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
/// ut labore et dolore magna aliqua. Morbi quis commodo odio aenean sed adipiscing. Nunc
/// congue nisi vitae suscipit tellus mauris a. Consectetur adipiscing elit pellentesque
/// habitant morbi tristique senectus.
pub foo: u32,

/// Elit eget gravida cum sociis natoque penatibus et magnis dis. Consequat semper viverra nam
/// libero. Accumsan in nisl nisi scelerisque eu. Pellentesque id nibh tortor id aliquet. Sed
/// velit dignissim sodales ut. Facilisis sed odio morbi quis commodo odio aenean sed. Et
/// ultrices neque ornare aenean euismod elementum. Condimentum lacinia quis vel eros donec ac
/// odio tempor.
///
/// Lacinia at quis risus sed vulputate odio ut enim. Etiam erat velit scelerisque in dictum.
/// Nibh tellus molestie nunc non blandit massa enim nec. Nascetur ridiculus mus mauris vitae.
pub bar: u32,

/// Mi proin sed libero enim sed faucibus turpis. Amet consectetur adipiscing elit duis
/// tristique sollicitudin nibh sit amet. Congue quisque egestas diam in arcu cursus euismod
/// quis viverra. Cum sociis natoque penatibus et magnis dis parturient montes. Enim sit amet
/// venenatis urna cursus eget nunc scelerisque viverra. Cras semper auctor neque vitae tempus
/// quam pellentesque. Tortor posuere ac ut consequat semper viverra nam libero justo. Vitae
/// auctor eu augue ut lectus arcu bibendum at. Faucibus vitae aliquet nec ullamcorper sit amet
/// risus nullam. Maecenas accumsan lacus vel facilisis volutpat. Arcu non odio euismod
/// lacinia.
///
/// [`FooBar::beep()`]: crate::foobar::FooBar::beep
/// [`FooBar::boop()`]: crate::foobar::FooBar::boop
/// [`foobar::BazBaq::BEEP_BOOP`]: crate::foobar::BazBaq::BEEP_BOOP
pub baz: u32,

/// Eu consequat ac felis donec et odio pellentesque diam. Ut eu sem integer vitae justo eget.
/// Consequat ac felis donec et odio pellentesque diam volutpat.
pub baq: u32,

/// Amet consectetur adipiscing elit pellentesque habitant. Ut morbi tincidunt augue interdum
/// velit euismod in pellentesque. Imperdiet sed euismod nisi porta lorem. Nec tincidunt
/// praesent semper feugiat. Facilisis leo vel fringilla est. Egestas diam in arcu cursus
/// euismod quis viverra. Sagittis eu volutpat odio facilisis mauris sit amet. Posuere morbi
/// leo urna molestie at.
///
/// Pretium aenean pharetra magna ac. Nisl condimentum id venenatis a condimentum vitae. Semper
/// quis lectus nulla at volutpat diam ut venenatis tellus. Egestas tellus rutrum tellus
/// pellentesque eu tincidunt tortor aliquam.
pub foobar: u32
}
8 changes: 8 additions & 0 deletions tests/target/issue-4791/no_trailing_comma.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// rustfmt-struct_field_align_threshold: 0
// rustfmt-trailing_comma: Never

pub struct Baz {
group_a: u8,

group_b: u8
}
14 changes: 14 additions & 0 deletions tests/target/issue-4791/trailing_comma.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// rustfmt-struct_field_align_threshold: 30
// rustfmt-trailing_comma: Always

struct Foo {
group_a: u8,

group_b: u8,
}

struct Bar {
group_a: u8,

group_b: u8,
}