Skip to content

Commit 6cfeb1f

Browse files
authored
Merge pull request #2041 from osa1/match_arm_newline
[not ready] Implement match_arm_forces_newline option (#2039)
2 parents 0a59654 + 48bdecf commit 6cfeb1f

File tree

5 files changed

+123
-6
lines changed

5 files changed

+123
-6
lines changed

Configurations.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,48 @@ struct Dolor<T>
12291229
}
12301230
```
12311231

1232+
## `match_arm_forces_newline`
1233+
1234+
Consistently put match arms (block based or not) in a newline.
1235+
1236+
- **Default value**: `false`
1237+
- **Possible values**: `true`, `false`
1238+
1239+
#### `false` (default):
1240+
1241+
```rust
1242+
match x {
1243+
// a non-empty block
1244+
X0 => {
1245+
f();
1246+
}
1247+
// an empty block
1248+
X1 => {}
1249+
// a non-block
1250+
X2 => println!("ok"),
1251+
}
1252+
```
1253+
1254+
#### `true`:
1255+
1256+
```rust
1257+
match x {
1258+
// a non-empty block
1259+
X0 => {
1260+
f();
1261+
}
1262+
// an empty block
1263+
X1 =>
1264+
{}
1265+
// a non-block
1266+
X2 => {
1267+
println!("ok")
1268+
}
1269+
}
1270+
```
1271+
1272+
See also: [`wrap_match_arms`](#wrap_match_arms).
1273+
12321274
## `match_block_trailing_comma`
12331275

12341276
Put a trailing comma after a block based match arm (non-block arms are not affected)

src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,8 @@ create_config! {
597597
the same line with the pattern of arms";
598598
match_block_trailing_comma: bool, false,
599599
"Put a trailing comma after a block based match arm (non-block arms are not affected)";
600+
match_arm_forces_newline: bool, false,
601+
"Force match arm bodies to be in a new lines";
600602
indent_match_arms: bool, true, "Indent match arms instead of keeping them at the same \
601603
indentation level as the match keyword";
602604
match_pattern_separator_break_point: SeparatorPlace, SeparatorPlace::Back,

src/expr.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,15 +1695,20 @@ fn rewrite_match_body(
16951695
is_last: bool,
16961696
) -> Option<String> {
16971697
let (extend, body) = flatten_arm_body(context, body);
1698-
1699-
let comma = arm_comma(context.config, body, is_last);
1700-
let alt_block_sep = String::from("\n") + &shape.indent.block_only().to_string(context.config);
1701-
let alt_block_sep = alt_block_sep.as_str();
17021698
let (is_block, is_empty_block) = if let ast::ExprKind::Block(ref block) = body.node {
17031699
(true, is_empty_block(block, context.codemap))
17041700
} else {
17051701
(false, false)
17061702
};
1703+
let extend = if context.config.match_arm_forces_newline() {
1704+
is_block
1705+
} else {
1706+
extend
1707+
};
1708+
1709+
let comma = arm_comma(context.config, body, is_last);
1710+
let alt_block_sep = String::from("\n") + &shape.indent.block_only().to_string(context.config);
1711+
let alt_block_sep = alt_block_sep.as_str();
17071712

17081713
let combine_orig_body = |body_str: &str| {
17091714
let block_sep = match context.config.control_brace_style() {
@@ -1716,7 +1721,11 @@ fn rewrite_match_body(
17161721

17171722
let forbid_same_line = has_guard && pats_str.contains('\n') && !is_empty_block;
17181723
let next_line_indent = if is_block {
1719-
shape.indent
1724+
if is_empty_block {
1725+
shape.indent.block_indent(context.config)
1726+
} else {
1727+
shape.indent
1728+
}
17201729
} else {
17211730
shape.indent.block_indent(context.config)
17221731
};
@@ -1772,7 +1781,7 @@ fn rewrite_match_body(
17721781

17731782
match rewrite {
17741783
Some(ref body_str)
1775-
if !forbid_same_line
1784+
if !forbid_same_line && !context.config.match_arm_forces_newline()
17761785
&& (is_block
17771786
|| (!body_str.contains('\n') && body_str.len() <= body_shape.width)) =>
17781787
{
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// rustfmt-match_arm_forces_newline: true
2+
// rustfmt-wrap_match_arms: false
3+
4+
// match_arm_forces_newline puts all match arms bodies in a newline and indents
5+
// them.
6+
7+
fn main() {
8+
match x() {
9+
// a short non-empty block
10+
X0 => { f(); }
11+
// a long non-empty block
12+
X1 => { some.really.long.expression.fooooooooooooooooooooooooooooooooooooooooo().baaaaarrrrrrrrrrrrrrrrrrrrrrrrrr(); }
13+
// an empty block
14+
X2 => {}
15+
// a short non-block
16+
X3 => println!("ok"),
17+
// a long non-block
18+
X4 => foo.bar.baz.test.x.y.z.a.s.d.fasdfasdf.asfads.fasd.fasdfasdf.dfasfdsaf(),
19+
}
20+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// rustfmt-match_arm_forces_newline: true
2+
// rustfmt-wrap_match_arms: false
3+
4+
// match_arm_forces_newline puts all match arms bodies in a newline and indents
5+
// them.
6+
7+
fn main() {
8+
match x() {
9+
// a short non-empty block
10+
X0 => {
11+
f();
12+
}
13+
// a long non-empty block
14+
X1 => {
15+
some.really
16+
.long
17+
.expression
18+
.fooooooooooooooooooooooooooooooooooooooooo()
19+
.baaaaarrrrrrrrrrrrrrrrrrrrrrrrrr();
20+
}
21+
// an empty block
22+
X2 =>
23+
{}
24+
// a short non-block
25+
X3 =>
26+
println!("ok"),
27+
// a long non-block
28+
X4 =>
29+
foo.bar
30+
.baz
31+
.test
32+
.x
33+
.y
34+
.z
35+
.a
36+
.s
37+
.d
38+
.fasdfasdf
39+
.asfads
40+
.fasd
41+
.fasdfasdf
42+
.dfasfdsaf(),
43+
}
44+
}

0 commit comments

Comments
 (0)