Skip to content

Commit d613932

Browse files
committed
Do not break empty blocks in match arm patterns
Closes rust-lang#4125
1 parent 99edc88 commit d613932

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

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

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

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

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

@@ -479,7 +488,7 @@ fn rewrite_match_body(
479488

480489
match rewrite {
481490
Some(ref body_str)
482-
if is_block
491+
if (is_block && (!is_empty_block || !body_str.contains('\n')))
483492
|| (!body_str.contains('\n')
484493
&& unicode_str_width(body_str) <= body_shape.width) =>
485494
{
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)