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
6 changes: 6 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ pub(crate) fn format_expr(
// not the `ast::Block` node we're about to rewrite. To prevent dropping inner
// attributes call `rewrite_block` directly.
// See https://github.com/rust-lang/rustfmt/issues/6158
let shape = if context.config.style_edition() >= StyleEdition::Edition2027 {
// Shrink the shape by `"const ".len()` before rewriting the block
shape.offset_left(6, expr.span)?
} else {
shape
};
Comment on lines +189 to +194

@ytmimi ytmimi Aug 25, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the gate. This looks right to me, but I want to confirm something first before moving forward. #7055 mentioned that unsafe blocks didn't have this problem. I'd like to understand why that's the case. Maybe there's something going on inside rewrite_block where the shape is properly updated?

There are also try, gen, and async blocks. Might be good to check that we're handling those correctly too.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there is unsafe , we get it in prefix - let prefix = block_prefix(context, block, shape)?; and in function rewrite_single_line_block result contains it

   let result = format!("{prefix}{label_str}{{ {expr_str} }}");
        if result.len() <= shape.width && !result.contains('\n') {
            return Ok(result);
        }

but for const - prefix is empty

@AsthaMishra AsthaMishra Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try , async , and gen blocks pass their prefix into rewrite_single_line_block , so these works fine

rewrite_block(block, Some(&expr.attrs), opt_label, context, shape)?
}
_ => anon_const.rewrite_result(context, shape)?,
Expand Down
29 changes: 29 additions & 0 deletions tests/source/issue-7055.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// rustfmt-style_edition: 2027

struct S;

impl S {
const fn new(_: &str) -> Self {
S
}

fn go(&self) {}
}

fn main() {
const {
S::new(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
)
}
.go();
}

fn removes_trailing_whitespace() {
const {
S::new(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
)
}
.go();
}
Comment on lines +13 to +29

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between main and second_issue?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a space after S::new( in second_issue. #7055 should provide context

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@astral4 thanks for pointing that out. @AsthaMishra the trailing whitespace isn't so obvious. Instead of naming it second_issue Maybe you could name the case something like removes_trailing_whitespace, which would make the intent clearer in my opinion.

25 changes: 25 additions & 0 deletions tests/target/issue-7055.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// rustfmt-style_edition: 2027

struct S;

impl S {
const fn new(_: &str) -> Self {
S
}

fn go(&self) {}
}

fn main() {
const {
S::new("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
}
.go();
}

fn removes_trailing_whitespace() {
const {
S::new("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
}
.go();
}