Skip to content

Commit

Permalink
fix(items): Gate formatting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
malikolivier committed Aug 19, 2024
1 parent 933e997 commit 103d13e
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 2 deletions.
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 {
// 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

0 comments on commit 103d13e

Please sign in to comment.