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`.
This commit is contained in:
GambitingMan 2023-10-24 02:27:26 +02:00 committed by GitHub
parent 81fe905ca8
commit 041f113159
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 1 deletions

View File

@ -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)
}

View File

@ -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"))
}

View File

@ -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,
}
}

View File

@ -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,
}
}