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 handling of attribute in enum #6286

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
10 changes: 8 additions & 2 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,8 +645,14 @@ impl<'a> FmtVisitor<'a> {
let mut items: Vec<_> = itemize_list_with(self.config.struct_variant_width());

// If one of the variants use multiple lines, use multi-lined formatting for all variants.
fn is_multi_line_variant(item: &ListItem) -> bool {
let is_multi_line_variant = |item: &ListItem| -> bool {
let variant_str = item.inner_as_ref();
if self.config.style_edition() < StyleEdition::Edition2024 {
malikolivier marked this conversation as resolved.
Show resolved Hide resolved
// Fall back to previous naive implementation (#5662) because of
// rustfmt's stability guarantees
return variant_str.contains('\n');
}

let mut first_line_is_read = false;
for line in variant_str.split('\n') {
if first_line_is_read {
Expand All @@ -663,7 +669,7 @@ impl<'a> FmtVisitor<'a> {
}

true
}
};
let has_multiline_variant = items.iter().any(is_multi_line_variant);
let has_single_line_variant = items.iter().any(|item| !is_multi_line_variant(item));
if has_multiline_variant && has_single_line_variant {
Expand Down
1 change: 1 addition & 0 deletions tests/target/attribute-in-enum/horizontal-no-doc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// rustfmt-style_edition: 2024
enum MyType {
A { field1: bool, field2: bool },
B { field1: bool, field2: bool },
Expand Down
1 change: 1 addition & 0 deletions tests/target/attribute-in-enum/horizontal-with-doc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// rustfmt-style_edition: 2024
enum MyType {
A { field1: bool, field2: bool },
B { field1: bool, field2: bool },
Expand Down
1 change: 1 addition & 0 deletions tests/target/attribute-in-enum/vertical-macro-one-line.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// rustfmt-style_edition: 2024
enum A {
B {
a: usize,
Expand Down
1 change: 1 addition & 0 deletions tests/target/attribute-in-enum/vertical-no-doc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// rustfmt-style_edition: 2024
enum A {
B {
a: usize,
Expand Down
1 change: 1 addition & 0 deletions tests/target/attribute-in-enum/vertical-with-doc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// rustfmt-style_edition: 2024
enum A {
B {
a: usize,
Expand Down
5 changes: 4 additions & 1 deletion tests/target/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ pub enum EnumWithAttributes {
SkippedItem(String,String,), // Post-comment
#[another_attr]
#[attr2]
ItemStruct { x: usize, y: usize }, /* Comment AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */
ItemStruct {
x: usize,
y: usize,
}, /* Comment AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */
// And another
ForcedPreflight, /* AAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
* AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */
Expand Down
Loading