Skip to content

Commit e47881b

Browse files
fix: ensure idempotency on empty match blocks (#4271)
1 parent 9cdb827 commit e47881b

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

Diff for: src/formatting/matches.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -377,15 +377,17 @@ fn rewrite_match_body(
377377
let comma = arm_comma(context.config, body, is_last);
378378
let alt_block_sep = &shape.indent.to_string_with_newline(context.config);
379379

380-
let combine_orig_body = |mut body_str: &str| {
380+
let combine_orig_body = |body_str: &str| {
381381
let block_sep = match context.config.control_brace_style() {
382382
ControlBraceStyle::AlwaysNextLine if is_block => alt_block_sep,
383383
_ => " ",
384384
};
385-
if body_str == "()" {
386-
body_str = "{}";
387-
}
388-
Some(format!("{} =>{}{}{}", pats_str, block_sep, body_str, comma))
385+
let (body, comma) = match body_str {
386+
"()" if context.config.match_block_trailing_comma() => ("{}", ","),
387+
"()" => ("{}", ""),
388+
_ => (body_str, comma),
389+
};
390+
Some(format!("{} =>{}{}{}", pats_str, block_sep, body, comma))
389391
};
390392

391393
let next_line_indent = if !is_block || is_empty_block {

Diff for: tests/source/issue_4267.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
match foo {
3+
a => (),
4+
b => {},
5+
c => {}
6+
d => ()
7+
}
8+
}

Diff for: tests/target/issue_4267.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
match foo {
3+
a => {}
4+
b => {}
5+
c => {}
6+
d => {}
7+
}
8+
}

0 commit comments

Comments
 (0)