Skip to content

Commit

Permalink
Fixed error caused by combination of match_arm_blocks and control_bra…
Browse files Browse the repository at this point in the history
…ce_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`.
  • Loading branch information
GambitingMan authored Oct 24, 2023
1 parent 81fe905 commit 041f113
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
16 changes: 16 additions & 0 deletions tests/rustfmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
15 changes: 15 additions & 0 deletions tests/source/issue_5912.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
15 changes: 15 additions & 0 deletions tests/target/issue_5912.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}

0 comments on commit 041f113

Please sign in to comment.