From 041f11315923b0e4b0328b3bb6366de9148c7b6b Mon Sep 17 00:00:00 2001 From: GambitingMan <68272188+GambitingMan@users.noreply.github.com> Date: Tue, 24 Oct 2023 02:27:26 +0200 Subject: [PATCH] Fixed error caused by combination of match_arm_blocks and control_brace_style Fixes 5912 When `control_brace_style = "AlwaysNextLine"`, the code seems to always assume that `body_prefix` is `{`. This is however not the case when `match_arm_blocks = false`. This causes `block_sep` to introduce extra white space that causes the error. The fix was to check if `body_prefix` is empty before matching on `ControlBraceStyle::AlwaysNextLine`. --- src/matches.rs | 2 +- tests/rustfmt/main.rs | 16 ++++++++++++++++ tests/source/issue_5912.rs | 15 +++++++++++++++ tests/target/issue_5912.rs | 15 +++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tests/source/issue_5912.rs create mode 100644 tests/target/issue_5912.rs diff --git a/src/matches.rs b/src/matches.rs index 95b0ed16db8..45f8bd90206 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -451,8 +451,8 @@ fn rewrite_match_body( }; let block_sep = match context.config.control_brace_style() { - ControlBraceStyle::AlwaysNextLine => format!("{}{}", alt_block_sep, body_prefix), _ if body_prefix.is_empty() => "".to_owned(), + ControlBraceStyle::AlwaysNextLine => format!("{}{}", alt_block_sep, body_prefix), _ if forbid_same_line || !arrow_comment.is_empty() => { format!("{}{}", alt_block_sep, body_prefix) } diff --git a/tests/rustfmt/main.rs b/tests/rustfmt/main.rs index 7dcf7c8416e..87b55ca1d1d 100644 --- a/tests/rustfmt/main.rs +++ b/tests/rustfmt/main.rs @@ -184,3 +184,19 @@ fn dont_emit_ICE() { assert!(!stderr.contains("thread 'main' panicked")); } } + +#[test] +fn rustfmt_emits_error_when_control_brace_style_is_always_next_line() { + // See also https://github.com/rust-lang/rustfmt/issues/5912 + let args = [ + "--config=color=Never", + "--config", + "control_brace_style=AlwaysNextLine", + "--config", + "match_arm_blocks=false", + "tests/target/issue_5912.rs", + ]; + + let (_stdout, stderr) = rustfmt(&args); + assert!(!stderr.contains("error[internal]: left behind trailing whitespace")) +} diff --git a/tests/source/issue_5912.rs b/tests/source/issue_5912.rs new file mode 100644 index 00000000000..2265fd2146c --- /dev/null +++ b/tests/source/issue_5912.rs @@ -0,0 +1,15 @@ +// rustfmt-match_arm_blocks: false +// rustfmt-control_brace_style: AlwaysNextLine + +fn foo() { + match 0 { + 0 => { + aaaaaaaaaaaaaaaaaaaaaaaa + + bbbbbbbbbbbbbbbbbbbbbbbbb + + bbbbbbbbbbbbbbbbbbbbbbbbb + + bbbbbbbbbbbbbbbbbbbbbbbbb + + bbbbbbbbbbbbbbbbbbbbbbbbb + } + _ => 2, + } +} diff --git a/tests/target/issue_5912.rs b/tests/target/issue_5912.rs new file mode 100644 index 00000000000..835f2aba971 --- /dev/null +++ b/tests/target/issue_5912.rs @@ -0,0 +1,15 @@ +// rustfmt-match_arm_blocks: false +// rustfmt-control_brace_style: AlwaysNextLine + +fn foo() { + match 0 + { + 0 => + aaaaaaaaaaaaaaaaaaaaaaaa + + bbbbbbbbbbbbbbbbbbbbbbbbb + + bbbbbbbbbbbbbbbbbbbbbbbbb + + bbbbbbbbbbbbbbbbbbbbbbbbb + + bbbbbbbbbbbbbbbbbbbbbbbbb, + _ => 2, + } +}