From 50aae64e1607fdd847cd4c86cd1663002be5cbfe Mon Sep 17 00:00:00 2001 From: Bryan Deng Date: Tue, 28 Jul 2026 22:31:30 -0400 Subject: [PATCH 1/3] fix: keep array type on one line for statics --- src/items.rs | 32 +++++++++++++++++++++++++------- tests/source/issue-6976.rs | 3 +++ tests/target/issue-6976.rs | 3 +++ 3 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 tests/source/issue-6976.rs create mode 100644 tests/target/issue-6976.rs diff --git a/src/items.rs b/src/items.rs index e662113232c..a6a8f6ca520 100644 --- a/src/items.rs +++ b/src/items.rs @@ -2128,9 +2128,9 @@ fn rewrite_static( ) }) .map_or("".into(), |x| format!("{x}")); - let colon = colon_spaces(context.config); + let (before_colon, after_colon) = type_annotation_spacing(context.config); let mut prefix = format!( - "{}{}{}{} {}{}{}{}", + "{}{}{}{} {}{}{}{}:", format_visibility(context, static_parts.vis), static_parts.defaultness.map_or("", format_defaultness), format_safety(static_parts.safety), @@ -2138,12 +2138,17 @@ fn rewrite_static( format_mutability(static_parts.mutability), rewrite_ident(context, static_parts.ident), generics, - colon + before_colon ); - // 2 = " =".len() + let ty_offset = if static_parts.expr_opt.is_some() { + " =".len() + } else { + ";".len() + } + after_colon.len(); + let shape = Shape::indented(offset.block_only(), context.config); let ty_shape = Shape::indented(offset.block_only(), context.config) - .offset_left_opt(last_line_width(&prefix) + 2)?; + .offset_left_opt(last_line_width(&prefix) + ty_offset)?; let ty_str = match static_parts.ty.rewrite(context, ty_shape) { Some(ty_str) => ty_str, None => { @@ -2161,12 +2166,25 @@ fn rewrite_static( } }; + let prefix_with_ty = if is_single_line(&ty_str) { + format!("{prefix}{after_colon}{ty_str}") + } else { + rewrite_assign_rhs( + context, + &prefix, + &*static_parts.ty, + &RhsAssignKind::Ty, + shape.offset_left_opt(2).unwrap(), + ) + .unwrap() + }; + if let Some(expr) = static_parts.expr_opt { let comments_lo = context.snippet_provider.span_after(static_parts.span, "="); let expr_lo = expr.span.lo(); let comments_span = mk_sp(comments_lo, expr_lo); - let lhs = format!("{prefix}{ty_str} ="); + let lhs = format!("{prefix_with_ty} ="); // 1 = ; let remaining_width = context.budget(offset.block_indent + 1); @@ -2184,7 +2202,7 @@ fn rewrite_static( .map(|res| recover_comment_removed(res, static_parts.span, context)) .map(|s| if s.ends_with(';') { s } else { s + ";" }) } else { - Some(format!("{prefix}{ty_str};")) + Some(format!("{prefix_with_ty};")) } } diff --git a/tests/source/issue-6976.rs b/tests/source/issue-6976.rs new file mode 100644 index 00000000000..919437b40ae --- /dev/null +++ b/tests/source/issue-6976.rs @@ -0,0 +1,3 @@ +pub(super) const ENCRYPTED_USERNAME_ENTROPY: [u8; + usernames::constants::USERNAME_LINK_ENTROPY_SIZE] = + const_str::hex!("4302c613c092a51c5394becffeb6f697300a605348e93f03c3db95e0b03d28f1"); diff --git a/tests/target/issue-6976.rs b/tests/target/issue-6976.rs new file mode 100644 index 00000000000..a12c614ca4d --- /dev/null +++ b/tests/target/issue-6976.rs @@ -0,0 +1,3 @@ +pub(super) const ENCRYPTED_USERNAME_ENTROPY: + [u8; usernames::constants::USERNAME_LINK_ENTROPY_SIZE] = + const_str::hex!("4302c613c092a51c5394becffeb6f697300a605348e93f03c3db95e0b03d28f1"); From ac72e0fb91cfb77f71abc40cd61e8c6d953567e5 Mon Sep 17 00:00:00 2001 From: Bryan Deng Date: Tue, 28 Jul 2026 22:51:29 -0400 Subject: [PATCH 2/3] use correct after type offset in rewrite_assign_rhs shape, remove unwraps --- src/items.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/items.rs b/src/items.rs index a6a8f6ca520..cc66e7aefde 100644 --- a/src/items.rs +++ b/src/items.rs @@ -2141,14 +2141,14 @@ fn rewrite_static( before_colon ); - let ty_offset = if static_parts.expr_opt.is_some() { + let after_ty_offset = if static_parts.expr_opt.is_some() { " =".len() } else { ";".len() - } + after_colon.len(); + }; let shape = Shape::indented(offset.block_only(), context.config); - let ty_shape = Shape::indented(offset.block_only(), context.config) - .offset_left_opt(last_line_width(&prefix) + ty_offset)?; + let ty_shape = + shape.offset_left_opt(last_line_width(&prefix) + after_colon.len() + after_ty_offset)?; let ty_str = match static_parts.ty.rewrite(context, ty_shape) { Some(ty_str) => ty_str, None => { @@ -2174,9 +2174,9 @@ fn rewrite_static( &prefix, &*static_parts.ty, &RhsAssignKind::Ty, - shape.offset_left_opt(2).unwrap(), + shape.offset_left_opt(after_ty_offset)?, ) - .unwrap() + .ok()? }; if let Some(expr) = static_parts.expr_opt { From a7e87007668f94de465dae9014a174c45db794bf Mon Sep 17 00:00:00 2001 From: Bryan Deng Date: Sun, 2 Aug 2026 19:47:12 -0400 Subject: [PATCH 3/3] gate changes behind 2024 style edition --- src/items.rs | 37 +++++++++++++------ ...76.rs => issue_6976_style_edition_2024.rs} | 2 + tests/source/issue_6976_style_edition_2027.rs | 5 +++ tests/target/issue_6976_style_edition_2024.rs | 5 +++ ...76.rs => issue_6976_style_edition_2027.rs} | 2 + 5 files changed, 39 insertions(+), 12 deletions(-) rename tests/source/{issue-6976.rs => issue_6976_style_edition_2024.rs} (85%) create mode 100644 tests/source/issue_6976_style_edition_2027.rs create mode 100644 tests/target/issue_6976_style_edition_2024.rs rename tests/target/{issue-6976.rs => issue_6976_style_edition_2027.rs} (85%) diff --git a/src/items.rs b/src/items.rs index cc66e7aefde..87f776c6266 100644 --- a/src/items.rs +++ b/src/items.rs @@ -2141,14 +2141,23 @@ fn rewrite_static( before_colon ); + let style_edition = context.config.style_edition(); + if style_edition <= StyleEdition::Edition2024 { + prefix.push_str(after_colon); + } + let after_ty_offset = if static_parts.expr_opt.is_some() { " =".len() } else { ";".len() }; let shape = Shape::indented(offset.block_only(), context.config); - let ty_shape = - shape.offset_left_opt(last_line_width(&prefix) + after_colon.len() + after_ty_offset)?; + let ty_shape = if style_edition <= StyleEdition::Edition2024 { + // 2 = " =".len() + shape.offset_left_opt(last_line_width(&prefix) + 2)? + } else { + shape.offset_left_opt(last_line_width(&prefix) + after_colon.len() + after_ty_offset)? + }; let ty_str = match static_parts.ty.rewrite(context, ty_shape) { Some(ty_str) => ty_str, None => { @@ -2166,17 +2175,21 @@ fn rewrite_static( } }; - let prefix_with_ty = if is_single_line(&ty_str) { - format!("{prefix}{after_colon}{ty_str}") + let prefix_with_ty = if style_edition <= StyleEdition::Edition2024 { + format!("{prefix}{ty_str}") } else { - rewrite_assign_rhs( - context, - &prefix, - &*static_parts.ty, - &RhsAssignKind::Ty, - shape.offset_left_opt(after_ty_offset)?, - ) - .ok()? + if is_single_line(&ty_str) { + format!("{prefix}{after_colon}{ty_str}") + } else { + rewrite_assign_rhs( + context, + &prefix, + &*static_parts.ty, + &RhsAssignKind::Ty, + shape.offset_left_opt(after_ty_offset)?, + ) + .ok()? + } }; if let Some(expr) = static_parts.expr_opt { diff --git a/tests/source/issue-6976.rs b/tests/source/issue_6976_style_edition_2024.rs similarity index 85% rename from tests/source/issue-6976.rs rename to tests/source/issue_6976_style_edition_2024.rs index 919437b40ae..568f454a6bb 100644 --- a/tests/source/issue-6976.rs +++ b/tests/source/issue_6976_style_edition_2024.rs @@ -1,3 +1,5 @@ +// rustfmt-style_edition: 2024 + pub(super) const ENCRYPTED_USERNAME_ENTROPY: [u8; usernames::constants::USERNAME_LINK_ENTROPY_SIZE] = const_str::hex!("4302c613c092a51c5394becffeb6f697300a605348e93f03c3db95e0b03d28f1"); diff --git a/tests/source/issue_6976_style_edition_2027.rs b/tests/source/issue_6976_style_edition_2027.rs new file mode 100644 index 00000000000..4e4d5ecefa4 --- /dev/null +++ b/tests/source/issue_6976_style_edition_2027.rs @@ -0,0 +1,5 @@ +// rustfmt-style_edition: 2027 + +pub(super) const ENCRYPTED_USERNAME_ENTROPY: [u8; + usernames::constants::USERNAME_LINK_ENTROPY_SIZE] = + const_str::hex!("4302c613c092a51c5394becffeb6f697300a605348e93f03c3db95e0b03d28f1"); diff --git a/tests/target/issue_6976_style_edition_2024.rs b/tests/target/issue_6976_style_edition_2024.rs new file mode 100644 index 00000000000..568f454a6bb --- /dev/null +++ b/tests/target/issue_6976_style_edition_2024.rs @@ -0,0 +1,5 @@ +// rustfmt-style_edition: 2024 + +pub(super) const ENCRYPTED_USERNAME_ENTROPY: [u8; + usernames::constants::USERNAME_LINK_ENTROPY_SIZE] = + const_str::hex!("4302c613c092a51c5394becffeb6f697300a605348e93f03c3db95e0b03d28f1"); diff --git a/tests/target/issue-6976.rs b/tests/target/issue_6976_style_edition_2027.rs similarity index 85% rename from tests/target/issue-6976.rs rename to tests/target/issue_6976_style_edition_2027.rs index a12c614ca4d..ae472d0b02d 100644 --- a/tests/target/issue-6976.rs +++ b/tests/target/issue_6976_style_edition_2027.rs @@ -1,3 +1,5 @@ +// rustfmt-style_edition: 2027 + pub(super) const ENCRYPTED_USERNAME_ENTROPY: [u8; usernames::constants::USERNAME_LINK_ENTROPY_SIZE] = const_str::hex!("4302c613c092a51c5394becffeb6f697300a605348e93f03c3db95e0b03d28f1");