Skip to content

Commit 103d13e

Browse files
committed
fix(items): Gate formatting changes
Any formatting change we make must be gated [1, 2]. We wish for this change to be included in the Rust 2024 style edition, which is currently nightly only [3]. [1] https://github.com/rust-lang/rustfmt/blob/40f507526993651ad3b92eda89d5b1cebd0ed374/Contributing.md#gate-formatting-changes [2] https://rust-lang.github.io/rfcs/3338-style-evolution.html [3] https://doc.rust-lang.org/nightly/style-guide/editions.html#rust-2024-style-edition
1 parent 933e997 commit 103d13e

File tree

6 files changed

+13
-2
lines changed

6 files changed

+13
-2
lines changed

src/items.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,14 @@ impl<'a> FmtVisitor<'a> {
645645
let mut items: Vec<_> = itemize_list_with(self.config.struct_variant_width());
646646

647647
// If one of the variants use multiple lines, use multi-lined formatting for all variants.
648-
fn is_multi_line_variant(item: &ListItem) -> bool {
648+
let is_multi_line_variant = |item: &ListItem| -> bool {
649649
let variant_str = item.inner_as_ref();
650+
if self.config.style_edition() < StyleEdition::Edition2024 {
651+
// Fall back to previous naive implementation (#5662) because of
652+
// rustfmt's stability guarantees
653+
return variant_str.contains('\n');
654+
}
655+
650656
let mut first_line_is_read = false;
651657
for line in variant_str.split('\n') {
652658
if first_line_is_read {
@@ -663,7 +669,7 @@ impl<'a> FmtVisitor<'a> {
663669
}
664670

665671
true
666-
}
672+
};
667673
let has_multiline_variant = items.iter().any(is_multi_line_variant);
668674
let has_single_line_variant = items.iter().any(|item| !is_multi_line_variant(item));
669675
if has_multiline_variant && has_single_line_variant {

tests/target/attribute-in-enum/horizontal-no-doc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-style_edition: 2024
12
enum MyType {
23
A { field1: bool, field2: bool },
34
B { field1: bool, field2: bool },

tests/target/attribute-in-enum/horizontal-with-doc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-style_edition: 2024
12
enum MyType {
23
A { field1: bool, field2: bool },
34
B { field1: bool, field2: bool },

tests/target/attribute-in-enum/vertical-macro-one-line.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-style_edition: 2024
12
enum A {
23
B {
34
a: usize,

tests/target/attribute-in-enum/vertical-no-doc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-style_edition: 2024
12
enum A {
23
B {
34
a: usize,

tests/target/attribute-in-enum/vertical-with-doc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-style_edition: 2024
12
enum A {
23
B {
34
a: usize,

0 commit comments

Comments
 (0)