mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-28 02:57:37 +00:00
Merge pull request #744 from sanxiyn/wildcard-arm
Trailing commas for wildcard arms
This commit is contained in:
commit
f5bd7b76e0
@ -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";
|
||||
}
|
||||
|
12
src/expr.rs
12
src/expr.rs
@ -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()
|
||||
|
10
tests/source/match-wildcard-trailing-comma.rs
Normal file
10
tests/source/match-wildcard-trailing-comma.rs
Normal 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,
|
||||
}
|
||||
}
|
10
tests/target/match-wildcard-trailing-comma.rs
Normal file
10
tests/target/match-wildcard-trailing-comma.rs
Normal 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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user