From ad3a162bc751f92be753e6fd5dec4835ee75ab35 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] Fix brace placement for multiline control flow --- src/expr.rs | 7 ++++++- tests/source/issue_7003_style_edition_2024.rs | 14 ++++++++++++++ tests/source/issue_7003_style_edition_2027.rs | 14 ++++++++++++++ tests/target/issue_7003_style_edition_2024.rs | 14 ++++++++++++++ tests/target/issue_7003_style_edition_2027.rs | 15 +++++++++++++++ 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tests/source/issue_7003_style_edition_2024.rs create mode 100644 tests/source/issue_7003_style_edition_2027.rs create mode 100644 tests/target/issue_7003_style_edition_2024.rs create mode 100644 tests/target/issue_7003_style_edition_2027.rs diff --git a/src/expr.rs b/src/expr.rs index fc91afb25e3..0b1ec0e7d93 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1009,10 +1009,15 @@ impl<'a> ControlFlow<'a> { .config .max_width() .saturating_sub(constr_shape.used_width() + offset + brace_overhead); + let first_line_indent = if context.config.style_edition() >= StyleEdition::Edition2027 { + shape.indent.width() + } else { + shape.used_width() + }; let force_newline_brace = (pat_expr_string.contains('\n') || pat_expr_string.len() > one_line_budget) && (!last_line_extendable(&pat_expr_string) - || last_line_offsetted(shape.used_width(), &pat_expr_string)); + || last_line_offsetted(first_line_indent, &pat_expr_string)); // Try to format if-else on single line. if self.allow_single_line && context.config.single_line_if_else_max_width() > 0 { diff --git a/tests/source/issue_7003_style_edition_2024.rs b/tests/source/issue_7003_style_edition_2024.rs new file mode 100644 index 00000000000..f2e21df15da --- /dev/null +++ b/tests/source/issue_7003_style_edition_2024.rs @@ -0,0 +1,14 @@ +// rustfmt-style_edition: 2024 + +fn main() { + _ = if let Some(term_node) = sema + .token_ancestors_with_macros(token.clone()) + .find(|node| { + matches!( + node.kind(), + BLOCK_EXPR | ARG_LIST | PAREN_EXPR | ARRAY_EXPR | MATCH_EXPR + ) + }) { + match term_node.kind() {} + }; +} diff --git a/tests/source/issue_7003_style_edition_2027.rs b/tests/source/issue_7003_style_edition_2027.rs new file mode 100644 index 00000000000..eecbd1f42fc --- /dev/null +++ b/tests/source/issue_7003_style_edition_2027.rs @@ -0,0 +1,14 @@ +// rustfmt-style_edition: 2027 + +fn main() { + _ = if let Some(term_node) = sema + .token_ancestors_with_macros(token.clone()) + .find(|node| { + matches!( + node.kind(), + BLOCK_EXPR | ARG_LIST | PAREN_EXPR | ARRAY_EXPR | MATCH_EXPR + ) + }) { + match term_node.kind() {} + }; +} diff --git a/tests/target/issue_7003_style_edition_2024.rs b/tests/target/issue_7003_style_edition_2024.rs new file mode 100644 index 00000000000..f2e21df15da --- /dev/null +++ b/tests/target/issue_7003_style_edition_2024.rs @@ -0,0 +1,14 @@ +// rustfmt-style_edition: 2024 + +fn main() { + _ = if let Some(term_node) = sema + .token_ancestors_with_macros(token.clone()) + .find(|node| { + matches!( + node.kind(), + BLOCK_EXPR | ARG_LIST | PAREN_EXPR | ARRAY_EXPR | MATCH_EXPR + ) + }) { + match term_node.kind() {} + }; +} diff --git a/tests/target/issue_7003_style_edition_2027.rs b/tests/target/issue_7003_style_edition_2027.rs new file mode 100644 index 00000000000..c32a1278eae --- /dev/null +++ b/tests/target/issue_7003_style_edition_2027.rs @@ -0,0 +1,15 @@ +// rustfmt-style_edition: 2027 + +fn main() { + _ = if let Some(term_node) = sema + .token_ancestors_with_macros(token.clone()) + .find(|node| { + matches!( + node.kind(), + BLOCK_EXPR | ARG_LIST | PAREN_EXPR | ARRAY_EXPR | MATCH_EXPR + ) + }) + { + match term_node.kind() {} + }; +}