Merge pull request #744 from sanxiyn/wildcard-arm

Trailing commas for wildcard arms
This commit is contained in:
Nick Cameron 2016-01-06 16:48:11 +13:00
commit f5bd7b76e0
4 changed files with 30 additions and 3 deletions

View File

@ -312,4 +312,5 @@ create_config! {
wrap_match_arms: bool, true, "Wrap multiline match arms in blocks";
match_block_trailing_comma: bool, false,
"Put a trailing comma after a block based match arm (non-block arms are not affected)";
match_wildcard_trailing_comma: bool, true, "Put a trailing comma after a wildcard arm";
}

View File

@ -829,7 +829,7 @@ fn rewrite_match(context: &RewriteContext,
// We couldn't format the arm, just reproduce the source.
let snippet = context.snippet(mk_sp(arm_start_pos(arm), arm_end_pos(arm)));
result.push_str(&snippet);
result.push_str(arm_comma(&context.config, &arm.body));
result.push_str(arm_comma(&context.config, &arm, &arm.body));
}
}
// BytePos(1) = closing match brace.
@ -860,7 +860,13 @@ fn arm_end_pos(arm: &ast::Arm) -> BytePos {
arm.body.span.hi
}
fn arm_comma(config: &Config, body: &ast::Expr) -> &'static str {
fn arm_comma(config: &Config, arm: &ast::Arm, body: &ast::Expr) -> &'static str {
if !config.match_wildcard_trailing_comma {
if arm.pats.len() == 1 && arm.pats[0].node == ast::PatWild && arm.guard.is_none() {
return "";
}
}
if config.match_block_trailing_comma {
","
} else if let ast::ExprBlock(ref block) = body.node {
@ -958,7 +964,7 @@ impl Rewrite for ast::Arm {
ref x => x,
};
let comma = arm_comma(&context.config, body);
let comma = arm_comma(&context.config, self, body);
// Let's try and get the arm body on the same line as the condition.
// 4 = ` => `.len()

View File

@ -0,0 +1,10 @@
// rustfmt-match_wildcard_trailing_comma: false
fn match_wild(x: i32) -> i32 {
match x {
1 => 1,
2 => 2,
3 => 3,
_ => 0,
}
}

View File

@ -0,0 +1,10 @@
// rustfmt-match_wildcard_trailing_comma: false
fn match_wild(x: i32) -> i32 {
match x {
1 => 1,
2 => 2,
3 => 3,
_ => 0
}
}