Skip to content

Commit f5bd7b7

Browse files
committed
Merge pull request #744 from sanxiyn/wildcard-arm
Trailing commas for wildcard arms
2 parents d290271 + f9f7235 commit f5bd7b7

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,5 @@ create_config! {
312312
wrap_match_arms: bool, true, "Wrap multiline match arms in blocks";
313313
match_block_trailing_comma: bool, false,
314314
"Put a trailing comma after a block based match arm (non-block arms are not affected)";
315+
match_wildcard_trailing_comma: bool, true, "Put a trailing comma after a wildcard arm";
315316
}

src/expr.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ fn rewrite_match(context: &RewriteContext,
829829
// We couldn't format the arm, just reproduce the source.
830830
let snippet = context.snippet(mk_sp(arm_start_pos(arm), arm_end_pos(arm)));
831831
result.push_str(&snippet);
832-
result.push_str(arm_comma(&context.config, &arm.body));
832+
result.push_str(arm_comma(&context.config, &arm, &arm.body));
833833
}
834834
}
835835
// BytePos(1) = closing match brace.
@@ -860,7 +860,13 @@ fn arm_end_pos(arm: &ast::Arm) -> BytePos {
860860
arm.body.span.hi
861861
}
862862

863-
fn arm_comma(config: &Config, body: &ast::Expr) -> &'static str {
863+
fn arm_comma(config: &Config, arm: &ast::Arm, body: &ast::Expr) -> &'static str {
864+
if !config.match_wildcard_trailing_comma {
865+
if arm.pats.len() == 1 && arm.pats[0].node == ast::PatWild && arm.guard.is_none() {
866+
return "";
867+
}
868+
}
869+
864870
if config.match_block_trailing_comma {
865871
","
866872
} else if let ast::ExprBlock(ref block) = body.node {
@@ -958,7 +964,7 @@ impl Rewrite for ast::Arm {
958964
ref x => x,
959965
};
960966

961-
let comma = arm_comma(&context.config, body);
967+
let comma = arm_comma(&context.config, self, body);
962968

963969
// Let's try and get the arm body on the same line as the condition.
964970
// 4 = ` => `.len()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-match_wildcard_trailing_comma: false
2+
3+
fn match_wild(x: i32) -> i32 {
4+
match x {
5+
1 => 1,
6+
2 => 2,
7+
3 => 3,
8+
_ => 0,
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-match_wildcard_trailing_comma: false
2+
3+
fn match_wild(x: i32) -> i32 {
4+
match x {
5+
1 => 1,
6+
2 => 2,
7+
3 => 3,
8+
_ => 0
9+
}
10+
}

0 commit comments

Comments
 (0)