Skip to content

Commit 0c33b92

Browse files
committed
Do not break empty blocks in match arm patterns
Closes #4125
1 parent e44a71c commit 0c33b92

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

rustfmt-core/rustfmt-lib/src/formatting/matches.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,17 @@ fn rewrite_match_arm(
249249
};
250250

251251
// Patterns
252-
// 5 = ` => {`
253-
let pat_shape = shape.sub_width(5)?.offset_left(pipe_offset)?;
252+
let is_empty_block = if let ast::ExprKind::Block(ref block, _) = arm.body.kind {
253+
is_empty_block(context, block, Some(&arm.body.attrs))
254+
} else {
255+
false
256+
};
257+
let sub_width = if is_empty_block {
258+
" => {}".len()
259+
} else {
260+
" => {".len()
261+
};
262+
let pat_shape = shape.sub_width(sub_width)?.offset_left(pipe_offset)?;
254263

255264
let pats_str = arm.pat.rewrite(context, pat_shape)?;
256265

@@ -480,7 +489,7 @@ fn rewrite_match_body(
480489

481490
match rewrite {
482491
Some(ref body_str)
483-
if is_block
492+
if (is_block && (!is_empty_block || !body_str.contains('\n')))
484493
|| (!body_str.contains('\n')
485494
&& unicode_str_width(body_str) <= body_shape.width) =>
486495
{
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// rustfmt-max_width: 60
2+
3+
fn main() {
4+
match (pat, num) {
5+
(PatternOne, 1) | (RoomForBlockOnSameLine, 10) => {}
6+
7+
(PatternOne, 1) | (PatternStretchedToBounds, 2) => {}
8+
9+
UnsplitableVeryLongArmPatternWithRoomForBlock0 => {}
10+
11+
UnsplitableVeryLongArmPatternWithBraceAtEndOfLn => {}
12+
13+
UnsplitableVeryLongArmPatternWithArrowAtColnWidth => {}
14+
15+
UnsplitableVeryLongArmPatternWithArrowPastColnWidth => {}
16+
17+
UnsplitableVeryLongArmPatternGoingBeyondMaxColumnWidth => {}
18+
}
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// rustfmt-max_width: 60
2+
3+
fn main() {
4+
match (pat, num) {
5+
(PatternOne, 1) | (RoomForBlockOnSameLine, 10) => {}
6+
7+
(PatternOne, 1)
8+
| (PatternStretchedToBounds, 2) => {}
9+
10+
UnsplitableVeryLongArmPatternWithRoomForBlock0 => {}
11+
12+
UnsplitableVeryLongArmPatternWithBraceAtEndOfLn =>
13+
{}
14+
15+
UnsplitableVeryLongArmPatternWithArrowAtColnWidth =>
16+
{}
17+
18+
UnsplitableVeryLongArmPatternWithArrowPastColnWidth =>
19+
{}
20+
21+
UnsplitableVeryLongArmPatternGoingBeyondMaxColumnWidth =>
22+
{}
23+
}
24+
}

rustfmt-core/rustfmt-lib/tests/target/match.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ fn issue339() {
124124
// collapsing here is safe
125125
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff => {}
126126
// collapsing here exceeds line length
127-
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffg => {
128-
}
127+
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffg =>
128+
{}
129129
h => { // comment above block
130130
}
131131
i => {} // comment below block

0 commit comments

Comments
 (0)