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 @@ -1593,6 +1593,12 @@ pub(crate) fn can_be_overflowed_expr(
| ast::ExprKind::While(..) => {
context.config.combine_control_expr() && context.use_block_indent() && args_len == 1
}
ast::ExprKind::Lit(lit) => {
context.config.style_edition() >= StyleEdition::Edition2027
&& args_len == 1
&& lit.kind == LitKind::Str
&& lit.symbol.as_str().contains('\n')
}
Comment on lines +1596 to +1601

@ytmimi ytmimi Oct 14, 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.

Shouldn't we also check that the first line of the string literal fits within max_width?

Can you add some test cases where the first line of the string literal is at exactly the max_width after reformatting and then max_width + 1.

View changes since the review

@penguinawesome1 penguinawesome1 Oct 17, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I added the test cases, and the formatter moves the string to a new line once the line reaches max_width - 1.
Is this intentional, or possibly a known issue? To my understanding, it should only do that once the line exceeds max_width.


// Handle always block-like expressions
ast::ExprKind::Gen(..)
Expand Down
95 changes: 95 additions & 0 deletions tests/source/issue_5029.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//@ rustfmt-style_edition: 2027

pub const MAC: &str = foo!("
foo
bar
");

pub const SHORT_MAC: &str = f!("
foo
bar
");

pub const LONG_MAC: &str = long_foo!("
foo
bar
");

pub const FUN: &str = foo("
foo
bar
");

pub const SHORT_FUN: &str = f("
foo
bar
");

pub const LONG_FUN: &str = long_foo("
foo
bar
");

pub const BAR: &str = bar!("
foo bar
");

pub const BAZ: &str = baz!(
"
foo
bar
"
);

fn main() {
let bytes1 = hex!("
00010203 04050607
08090a0b 0c0d0e0f
");
}

// Default settings: `fn_call_width` is 60. `max_width` is 100.

// functions:

// First line string length is 59. Line length is 98.
pub const ABCDEFGHIJKLMNO: &str = foo("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456
next_line
");

// First line string length is 60. Line length is 98.
pub const ABCDEFGHIJKLMN: &str = foo("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567
next_line
");

// First line string length is 59. Line length is 99.
pub const ABCDEFGHIJKLMNOP: &str = foo("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456
next_line
");

// First line string length is 60. Line length is 99.
pub const ABCDEFGHIJKLMNO: &str = foo("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567
next_line
");

// function macros:

// First line string length is 59. Line length is 98.
pub const ABCDEFGHIJKLMN: &str = foo!("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456
next_line
");

// First line string length is 60. Line length is 98.
pub const ABCDEFGHIJKLM: &str = foo!("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567
next_line
");

// First line string length is 59. Line length is 99.
pub const ABCDEFGHIJKLMNO: &str = foo!("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456
next_line
");

// First line string length is 60. Line length is 99.
pub const ABCDEFGHIJKLMN: &str = foo!("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567
next_line
");
103 changes: 103 additions & 0 deletions tests/target/issue_5029.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//@ rustfmt-style_edition: 2027

pub const MAC: &str = foo!("
foo
bar
");

pub const SHORT_MAC: &str = f!("
foo
bar
");

pub const LONG_MAC: &str = long_foo!("
foo
bar
");

pub const FUN: &str = foo("
foo
bar
");

pub const SHORT_FUN: &str = f("
foo
bar
");

pub const LONG_FUN: &str = long_foo("
foo
bar
");

pub const BAR: &str = bar!("
foo bar
");

pub const BAZ: &str = baz!("
foo
bar
");

fn main() {
let bytes1 = hex!("
00010203 04050607
08090a0b 0c0d0e0f
");
}

// Default settings: `fn_call_width` is 60. `max_width` is 100.

// functions:

// First line string length is 59. Line length is 98.
pub const ABCDEFGHIJKLMNO: &str = foo("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456
next_line
");

// First line string length is 60. Line length is 98.
pub const ABCDEFGHIJKLMN: &str = foo(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567
next_line
",
);

// First line string length is 59. Line length is 99.
pub const ABCDEFGHIJKLMNOP: &str =
foo("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456
next_line
");

// First line string length is 60. Line length is 99.
pub const ABCDEFGHIJKLMNO: &str = foo(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567
next_line
",
);

// function macros:

// First line string length is 59. Line length is 98.
pub const ABCDEFGHIJKLMN: &str = foo!("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456
next_line
");

// First line string length is 60. Line length is 98.
pub const ABCDEFGHIJKLM: &str = foo!(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567
next_line
"
);

// First line string length is 59. Line length is 99.
pub const ABCDEFGHIJKLMNO: &str =
foo!("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456
next_line
");

// First line string length is 60. Line length is 99.
pub const ABCDEFGHIJKLMN: &str = foo!(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567
next_line
"
);
Loading