Skip to content

Commit 92d017e

Browse files
committed
Fix idempotency issue when wrapping the last match arm in a block
The issues was related to the interaction between two differnt configuration options when applied to the last match arm. * trailing_comma = Never * match_block_trailing_comma = true Previously, rustfmt only consider the ``match_block_trailing_comma`` configuration value when wrapping match arms. This lead to rustfmt adding a trailing comma to all newly wrapped match arms. Now, rustfmt also considers the ``trailing_comma`` configuration value when deciding whether or not to add a trailing comma when wrapping the last match arm in a block.
1 parent a67d909 commit 92d017e

File tree

13 files changed

+196
-1
lines changed

13 files changed

+196
-1
lines changed

src/matches.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,10 @@ fn rewrite_match_body(
420420
let indent_str = shape.indent.to_string_with_newline(context.config);
421421
let (body_prefix, body_suffix) =
422422
if context.config.match_arm_blocks() && !context.inside_macro() {
423-
let comma = if context.config.match_block_trailing_comma() {
423+
let comma_never = context.config.trailing_comma() == SeparatorTactic::Never;
424+
let comma = if is_last && comma_never {
425+
""
426+
} else if context.config.match_block_trailing_comma() {
424427
","
425428
} else {
426429
""
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// rustfmt-max_width: 90
2+
// rustfmt-match_block_trailing_comma: false
3+
// rustfmt-trailing_comma: Always
4+
5+
fn main() {
6+
match FooBar::Foo {
7+
FooBar::Foo => unreachable!(),
8+
FooBar::Bar => {
9+
println!("match_block_trailing_comma: false");
10+
println!("trailing_comma: Always");
11+
}
12+
FooBar::Baz => println!(
13+
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit."
14+
)
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// rustfmt-max_width: 90
2+
// rustfmt-match_block_trailing_comma: false
3+
// rustfmt-trailing_comma: Never
4+
5+
fn main() {
6+
match FooBar::Foo {
7+
FooBar::Foo => unreachable!(),
8+
FooBar::Bar => {
9+
println!("match_block_trailing_comma: false");
10+
println!("trailing_comma: Never");
11+
}
12+
FooBar::Baz => println!(
13+
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit."
14+
)
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// rustfmt-max_width: 90
2+
// rustfmt-match_block_trailing_comma: false
3+
// rustfmt-trailing_comma: Vertical
4+
5+
fn main() {
6+
match FooBar::Foo {
7+
FooBar::Foo => unreachable!(),
8+
FooBar::Bar => {
9+
println!("match_block_trailing_comma: false");
10+
println!("trailing_comma: Vertical");
11+
}
12+
FooBar::Baz => println!(
13+
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit."
14+
)
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// rustfmt-max_width: 90
2+
// rustfmt-match_block_trailing_comma: true
3+
// rustfmt-trailing_comma: Always
4+
5+
fn main() {
6+
match FooBar::Foo {
7+
FooBar::Foo => unreachable!(),
8+
FooBar::Bar => {
9+
println!("match_block_trailing_comma: true");
10+
println!("trailing_comma: Always");
11+
}
12+
FooBar::Baz => println!(
13+
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit."
14+
)
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// rustfmt-max_width: 90
2+
// rustfmt-match_block_trailing_comma: true
3+
// rustfmt-trailing_comma: Never
4+
5+
fn main() {
6+
match FooBar::Foo {
7+
FooBar::Foo => unreachable!(),
8+
FooBar::Bar => {
9+
println!("match_block_trailing_comma: true");
10+
println!("trailing_comma: Never");
11+
}
12+
FooBar::Baz => println!(
13+
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit."
14+
)
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// rustfmt-max_width: 90
2+
// rustfmt-match_block_trailing_comma: true
3+
// rustfmt-trailing_comma: Vertical
4+
5+
fn main() {
6+
match FooBar::Foo {
7+
FooBar::Foo => unreachable!(),
8+
FooBar::Bar => {
9+
println!("match_block_trailing_comma: true");
10+
println!("trailing_comma: Vertical");
11+
}
12+
FooBar::Baz => println!(
13+
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit."
14+
)
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// rustfmt-max_width: 90
2+
// rustfmt-match_block_trailing_comma: false
3+
// rustfmt-trailing_comma: Always
4+
5+
fn main() {
6+
match FooBar::Foo {
7+
FooBar::Foo => unreachable!(),
8+
FooBar::Bar => {
9+
println!("match_block_trailing_comma: false");
10+
println!("trailing_comma: Always");
11+
}
12+
FooBar::Baz => {
13+
println!("Lorem ipsum dolor sit amet, consectetuer adipiscing elit.")
14+
}
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// rustfmt-max_width: 90
2+
// rustfmt-match_block_trailing_comma: false
3+
// rustfmt-trailing_comma: Never
4+
5+
fn main() {
6+
match FooBar::Foo {
7+
FooBar::Foo => unreachable!(),
8+
FooBar::Bar => {
9+
println!("match_block_trailing_comma: false");
10+
println!("trailing_comma: Never");
11+
}
12+
FooBar::Baz => {
13+
println!("Lorem ipsum dolor sit amet, consectetuer adipiscing elit.")
14+
}
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// rustfmt-max_width: 90
2+
// rustfmt-match_block_trailing_comma: false
3+
// rustfmt-trailing_comma: Vertical
4+
5+
fn main() {
6+
match FooBar::Foo {
7+
FooBar::Foo => unreachable!(),
8+
FooBar::Bar => {
9+
println!("match_block_trailing_comma: false");
10+
println!("trailing_comma: Vertical");
11+
}
12+
FooBar::Baz => {
13+
println!("Lorem ipsum dolor sit amet, consectetuer adipiscing elit.")
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)