Skip to content

Commit

Permalink
Do not break empty blocks in match arm patterns (rust-lang#4187)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayazhafiz authored May 21, 2020
1 parent 3626a0b commit e952ff9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
15 changes: 12 additions & 3 deletions rustfmt-core/rustfmt-lib/src/formatting/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,17 @@ fn rewrite_match_arm(
};

// Patterns
// 5 = ` => {`
let pat_shape = shape.sub_width(5)?.offset_left(pipe_offset)?;
let is_empty_block = if let ast::ExprKind::Block(ref block, _) = arm.body.kind {
is_empty_block(context, block, Some(&arm.body.attrs))
} else {
false
};
let sub_width = if is_empty_block {
" => {}".len()
} else {
" => {".len()
};
let pat_shape = shape.sub_width(sub_width)?.offset_left(pipe_offset)?;

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

Expand Down Expand Up @@ -480,7 +489,7 @@ fn rewrite_match_body(

match rewrite {
Some(ref body_str)
if is_block
if (is_block && (!is_empty_block || !body_str.contains('\n')))
|| (!body_str.contains('\n')
&& unicode_str_width(body_str) <= body_shape.width) =>
{
Expand Down
19 changes: 19 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/source/issue-4125.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// rustfmt-max_width: 60

fn main() {
match (pat, num) {
(PatternOne, 1) | (RoomForBlockOnSameLine, 10) => {}

(PatternOne, 1) | (PatternStretchedToBounds, 2) => {}

UnsplitableVeryLongArmPatternWithRoomForBlock0 => {}

UnsplitableVeryLongArmPatternWithBraceAtEndOfLn => {}

UnsplitableVeryLongArmPatternWithArrowAtColnWidth => {}

UnsplitableVeryLongArmPatternWithArrowPastColnWidth => {}

UnsplitableVeryLongArmPatternGoingBeyondMaxColumnWidth => {}
}
}
24 changes: 24 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/target/issue-4125.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// rustfmt-max_width: 60

fn main() {
match (pat, num) {
(PatternOne, 1) | (RoomForBlockOnSameLine, 10) => {}

(PatternOne, 1)
| (PatternStretchedToBounds, 2) => {}

UnsplitableVeryLongArmPatternWithRoomForBlock0 => {}

UnsplitableVeryLongArmPatternWithBraceAtEndOfLn =>
{}

UnsplitableVeryLongArmPatternWithArrowAtColnWidth =>
{}

UnsplitableVeryLongArmPatternWithArrowPastColnWidth =>
{}

UnsplitableVeryLongArmPatternGoingBeyondMaxColumnWidth =>
{}
}
}
4 changes: 2 additions & 2 deletions rustfmt-core/rustfmt-lib/tests/target/match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ fn issue339() {
// collapsing here is safe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff => {}
// collapsing here exceeds line length
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffg => {
}
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffg =>
{}
h => { // comment above block
}
i => {} // comment below block
Expand Down

0 comments on commit e952ff9

Please sign in to comment.