From 6e36eb22ffb7b5aaeac7d009b7e3c5b4d1297577 Mon Sep 17 00:00:00 2001 From: subotac <73706465+subotac@users.noreply.github.com> Date: Mon, 3 Aug 2026 00:34:48 +0300 Subject: [PATCH 1/2] Fix chain formatting with pre-existing overflow --- src/chains.rs | 14 +++++++++++++- tests/source/issue_6977_style_edition_2024.rs | 11 +++++++++++ tests/source/issue_6977_style_edition_2027.rs | 11 +++++++++++ tests/target/issue_6977_style_edition_2024.rs | 11 +++++++++++ tests/target/issue_6977_style_edition_2027.rs | 12 ++++++++++++ 5 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/source/issue_6977_style_edition_2024.rs create mode 100644 tests/source/issue_6977_style_edition_2027.rs create mode 100644 tests/target/issue_6977_style_edition_2024.rs create mode 100644 tests/target/issue_6977_style_edition_2027.rs diff --git a/src/chains.rs b/src/chains.rs index 90adb67ad43..0f281170d82 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -582,7 +582,19 @@ impl Rewrite for Chain { formatter.format_last_child(context, shape, child_shape)?; let result = formatter.join_rewrites(context, child_shape)?; - wrap_str(result, context.config.max_width(), shape).max_width_error(shape.width, full_span) + let max_width = if context.config.style_edition() >= StyleEdition::Edition2027 { + // Keep formatting the chain when a nested expression contains an unavoidable, + // pre-existing overflow, but do not allow the rewrite to make it any wider. + crate::comment::filter_normal_code(context.snippet(full_span)) + .lines() + .map(utils::unicode_str_width) + .max() + .unwrap_or(0) + .max(context.config.max_width()) + } else { + context.config.max_width() + }; + wrap_str(result, max_width, shape).max_width_error(shape.width, full_span) } } diff --git a/tests/source/issue_6977_style_edition_2024.rs b/tests/source/issue_6977_style_edition_2024.rs new file mode 100644 index 00000000000..9d9ce30a5c3 --- /dev/null +++ b/tests/source/issue_6977_style_edition_2024.rs @@ -0,0 +1,11 @@ +// rustfmt-style_edition: 2024 + +fn main() { + some_struct.foo(format!( + "{}", if true { + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + } else { + "" + }, + )); +} diff --git a/tests/source/issue_6977_style_edition_2027.rs b/tests/source/issue_6977_style_edition_2027.rs new file mode 100644 index 00000000000..fe5bdc9a0fd --- /dev/null +++ b/tests/source/issue_6977_style_edition_2027.rs @@ -0,0 +1,11 @@ +// rustfmt-style_edition: 2027 + +fn main() { + some_struct.foo(format!( + "{}", if true { + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + } else { + "" + }, + )); +} diff --git a/tests/target/issue_6977_style_edition_2024.rs b/tests/target/issue_6977_style_edition_2024.rs new file mode 100644 index 00000000000..9d9ce30a5c3 --- /dev/null +++ b/tests/target/issue_6977_style_edition_2024.rs @@ -0,0 +1,11 @@ +// rustfmt-style_edition: 2024 + +fn main() { + some_struct.foo(format!( + "{}", if true { + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + } else { + "" + }, + )); +} diff --git a/tests/target/issue_6977_style_edition_2027.rs b/tests/target/issue_6977_style_edition_2027.rs new file mode 100644 index 00000000000..8af0f1392f0 --- /dev/null +++ b/tests/target/issue_6977_style_edition_2027.rs @@ -0,0 +1,12 @@ +// rustfmt-style_edition: 2027 + +fn main() { + some_struct.foo(format!( + "{}", + if true { + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + } else { + "" + }, + )); +} From 5f804ff584a887233ace15e3530ce26d2d1f2203 Mon Sep 17 00:00:00 2001 From: subotac <73706465+subotac@users.noreply.github.com> Date: Wed, 30 Sep 2026 23:10:03 +0300 Subject: [PATCH 2/2] Format chain width check --- src/chains.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/chains.rs b/src/chains.rs index cb3a63ed8e5..d3beb8907b9 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -603,13 +603,8 @@ impl Rewrite for Chain { } else { context.config.max_width() }; - wrap_str( - result, - max_width, - context.config.tab_spaces(), - shape, - ) - .max_width_error(shape.width, full_span) + wrap_str(result, max_width, context.config.tab_spaces(), shape) + .max_width_error(shape.width, full_span) } }