From 4e987e15e20093751437ed3487b3413e8a04e008 Mon Sep 17 00:00:00 2001 From: GambitingMan Date: Thu, 7 Sep 2023 20:59:12 +0200 Subject: [PATCH 1/2] Fix #5624 --- src/closures.rs | 4 ++++ tests/source/issue-5624.rs | 13 +++++++++++++ tests/target/issue-5624.rs | 11 +++++++++++ 3 files changed, 28 insertions(+) create mode 100644 tests/source/issue-5624.rs create mode 100644 tests/target/issue-5624.rs diff --git a/src/closures.rs b/src/closures.rs index a09146e9592..649f88f0526 100644 --- a/src/closures.rs +++ b/src/closures.rs @@ -199,6 +199,10 @@ fn rewrite_closure_expr( | ast::ExprKind::Unary(_, ref expr) | ast::ExprKind::Cast(ref expr, _) => allow_multi_line(expr), + ast::ExprKind::Binary(_, ref expr1, ref expr2) => { + allow_multi_line(expr1) && allow_multi_line(expr2) + } + _ => false, } } diff --git a/tests/source/issue-5624.rs b/tests/source/issue-5624.rs new file mode 100644 index 00000000000..0ca81930ef4 --- /dev/null +++ b/tests/source/issue-5624.rs @@ -0,0 +1,13 @@ +fn func usize>(f: F) -> usize { + f(0) +} + +fn main() { + let _result = func(|x| + match x { + _ => 0, + } + match 1 { + _ => 1, + } + ); +} diff --git a/tests/target/issue-5624.rs b/tests/target/issue-5624.rs new file mode 100644 index 00000000000..ba0395cd0c1 --- /dev/null +++ b/tests/target/issue-5624.rs @@ -0,0 +1,11 @@ +fn func usize>(f: F) -> usize { + f(0) +} + +fn main() { + let _result = func(|x| match x { + _ => 0, + } + match 1 { + _ => 1, + }); +} From 939315ddc31c4e9311abb5243cc0f8699eaa2ff9 Mon Sep 17 00:00:00 2001 From: GambitingMan Date: Wed, 13 Sep 2023 22:43:19 +0200 Subject: [PATCH 2/2] Gate b2b-match-block fix behind Version Two --- src/closures.rs | 12 +++++++----- tests/source/issue-5624.rs | 2 ++ tests/target/issue-5624.rs | 2 ++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/closures.rs b/src/closures.rs index 649f88f0526..534e83ce26e 100644 --- a/src/closures.rs +++ b/src/closures.rs @@ -185,7 +185,7 @@ fn rewrite_closure_expr( context: &RewriteContext<'_>, shape: Shape, ) -> Option { - fn allow_multi_line(expr: &ast::Expr) -> bool { + fn allow_multi_line(expr: &ast::Expr, context: &RewriteContext<'_>) -> bool { match expr.kind { ast::ExprKind::Match(..) | ast::ExprKind::Async(..) @@ -197,10 +197,12 @@ fn rewrite_closure_expr( ast::ExprKind::AddrOf(_, _, ref expr) | ast::ExprKind::Try(ref expr) | ast::ExprKind::Unary(_, ref expr) - | ast::ExprKind::Cast(ref expr, _) => allow_multi_line(expr), + | ast::ExprKind::Cast(ref expr, _) => allow_multi_line(expr, context), - ast::ExprKind::Binary(_, ref expr1, ref expr2) => { - allow_multi_line(expr1) && allow_multi_line(expr2) + ast::ExprKind::Binary(_, ref expr1, ref expr2) + if context.config.version() == Version::Two => + { + allow_multi_line(expr1, context) && allow_multi_line(expr2, context) } _ => false, @@ -209,7 +211,7 @@ fn rewrite_closure_expr( // When rewriting closure's body without block, we require it to fit in a single line // unless it is a block-like expression or we are inside macro call. - let veto_multiline = (!allow_multi_line(expr) && !context.inside_macro()) + let veto_multiline = (!allow_multi_line(expr, context) && !context.inside_macro()) || context.config.force_multiline_blocks(); expr.rewrite(context, shape) .and_then(|rw| { diff --git a/tests/source/issue-5624.rs b/tests/source/issue-5624.rs index 0ca81930ef4..625d541daad 100644 --- a/tests/source/issue-5624.rs +++ b/tests/source/issue-5624.rs @@ -1,3 +1,5 @@ +// rustfmt-version: Two + fn func usize>(f: F) -> usize { f(0) } diff --git a/tests/target/issue-5624.rs b/tests/target/issue-5624.rs index ba0395cd0c1..571b011c0fd 100644 --- a/tests/target/issue-5624.rs +++ b/tests/target/issue-5624.rs @@ -1,3 +1,5 @@ +// rustfmt-version: Two + fn func usize>(f: F) -> usize { f(0) }