Skip to content

Commit abca1de

Browse files
C4K3nrc
authored andcommitted
Add indent_match_arms option (#1404)
Makes it optional whether to indent arms in match expressions. Setting this to false may be desirable for people wishing to avoid double-indents, in that if the match arm is a block, the block will cause an extra indentation level, and if it isn't a block but just a single line, it's still easy to see the logic at a glance. This style is preferred in certain other languages with switch statements, e.g. Linux style C and the most common Java style.
1 parent 0dd0cc1 commit abca1de

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

src/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ create_config! {
388388
wrap_match_arms: bool, true, "Wrap multiline match arms in blocks";
389389
match_block_trailing_comma: bool, false,
390390
"Put a trailing comma after a block based match arm (non-block arms are not affected)";
391+
indent_match_arms: bool, true, "Indent match arms instead of keeping them at the same \
392+
indentation level as the match keyword";
391393
closure_block_indent_threshold: isize, 7, "How many lines a closure must have before it is \
392394
block indented. -1 means never use block indent.";
393395
space_before_type_annotation: bool, false,

src/expr.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,12 @@ fn rewrite_match(context: &RewriteContext,
11371137
};
11381138
let mut result = format!("match {}{}{{", cond_str, block_sep);
11391139

1140-
let arm_shape = shape.block_indent(context.config.tab_spaces);
1140+
let arm_shape = if context.config.indent_match_arms {
1141+
shape.block_indent(context.config.tab_spaces)
1142+
} else {
1143+
shape.block_indent(0)
1144+
};
1145+
11411146
let arm_indent_str = arm_shape.indent.to_string(context.config);
11421147

11431148
let open_brace_pos = context.codemap.span_after(mk_sp(cond.span.hi, arm_start_pos(&arms[0])),

tests/source/indent_match_arms.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// rustfmt-indent_match_arms: false
2+
3+
fn main() {
4+
match x {
5+
1 => "one",
6+
2 => "two",
7+
3 => "three",
8+
4 => "four",
9+
5 => "five",
10+
_ => "something else",
11+
}
12+
13+
match x {
14+
1 => "one",
15+
2 => "two",
16+
3 => "three",
17+
4 => "four",
18+
5 => match y {
19+
'a' => 'A',
20+
'b' => 'B',
21+
'c' => 'C',
22+
_ => "Nope",
23+
},
24+
_ => "something else",
25+
}
26+
27+
}

tests/target/indent_match_arms.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// rustfmt-indent_match_arms: false
2+
3+
fn main() {
4+
match x {
5+
1 => "one",
6+
2 => "two",
7+
3 => "three",
8+
4 => "four",
9+
5 => "five",
10+
_ => "something else",
11+
}
12+
13+
match x {
14+
1 => "one",
15+
2 => "two",
16+
3 => "three",
17+
4 => "four",
18+
5 => {
19+
match y {
20+
'a' => 'A',
21+
'b' => 'B',
22+
'c' => 'C',
23+
_ => "Nope",
24+
}
25+
}
26+
_ => "something else",
27+
}
28+
29+
}

0 commit comments

Comments
 (0)