Skip to content

Commit abf8f43

Browse files
committed
Implement match_arm_forces_newline option (#2039)
1 parent e48dd81 commit abf8f43

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

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
{

0 commit comments

Comments
 (0)