Skip to content
Open
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
47 changes: 39 additions & 8 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2128,22 +2128,36 @@ 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),
static_parts.prefix,
format_mutability(static_parts.mutability),
rewrite_ident(context, static_parts.ident),
generics,
colon
before_colon
);

// 2 = " =".len()
let ty_shape = Shape::indented(offset.block_only(), context.config)
.offset_left_opt(last_line_width(&prefix) + 2)?;
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 = 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 => {
Expand All @@ -2161,12 +2175,29 @@ fn rewrite_static(
}
};

let prefix_with_ty = if style_edition <= StyleEdition::Edition2024 {
format!("{prefix}{ty_str}")
} else {
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 {
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);
Expand All @@ -2184,7 +2215,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};"))
}
}

Expand Down
5 changes: 5 additions & 0 deletions tests/source/issue_6976_style_edition_2024.rs
Comment thread
Blackgaurd marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -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");
5 changes: 5 additions & 0 deletions tests/source/issue_6976_style_edition_2027.rs
Original file line number Diff line number Diff line change
@@ -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");
5 changes: 5 additions & 0 deletions tests/target/issue_6976_style_edition_2024.rs
Original file line number Diff line number Diff line change
@@ -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");
5 changes: 5 additions & 0 deletions tests/target/issue_6976_style_edition_2027.rs
Original file line number Diff line number Diff line change
@@ -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");