Repository: rust-lang/rustfmt (rust)

Issue:
brace_style AlwaysNextLine fails on nightly
```toml
unstable_features = true
condense_wildcard_suffixes = true
edition = "2021"
max_width = 128
group_imports = "StdExternalCrate"
tab_spaces = 3
use_field_init_shorthand = true
use_try_shorthand = true
where_single_line = true
combine_control_expr = false
# condense_wildcard_suffixes = true
format_code_in_doc_comments = true
format_strings = true
imports_layout = "HorizontalVertical"
match_arm_blocks = false
match_block_trailing_comma = true
imports_granularity = "Crate"
reorder_impl_items = true
reorder_imports = true
reorder_modules = true
wrap_comments = true
use_small_heuristics = "Max"
trailing_comma = "Never"
brace_style = "AlwaysNextLine"
control_brace_style = "AlwaysNextLine"
```
```
error[internal]: left behind trailing whitespace
   --> /home/aspizu/Projects/onyo/src/value.rs:207:207:1
    |
207 |
    | ^^^^^^^^^
    |

error[internal]: left behind trailing whitespace
   --> /home/aspizu/Projects/onyo/src/value.rs:472:472:1
    |
472 |
    | ^^^^^^^^^
    |

warning: rustfmt has failed to format. See previous 2 errors.
```

Pull request:
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`](https://github.com/rust-lang/rustfmt/blob/a1fabbf3865c1044c7e96e8ac337131ebdf411b6/src/matches.rs#L453C1-L460C32) to introduce extra white space that causes the error.

The fix was to check if `body_prefix` is empty before matching on `ControlBraceStyle::AlwaysNextLine`. If you prefer however, I can add a match guard instead of changing the ordering of the match arms.

Patch:
diff --git a/src/matches.rs b/src/matches.rs
index 95b0ed16..45f8bd90 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)
             }


Test patch:
diff --git a/tests/rustfmt/main.rs b/tests/rustfmt/main.rs
index 7dcf7c84..87b55ca1 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 00000000..2265fd21
--- /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 00000000..835f2aba
--- /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,
+    }
+}