Skip to content

Commit 5b9b7d5

Browse files
committed
Do not collapse block around expr with condition on match arm
Closes rust-lang#2376.
1 parent 88589f2 commit 5b9b7d5

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

src/matches.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ fn flatten_arm_body<'a>(context: &'a RewriteContext, body: &'a ast::Expr) -> (bo
309309
{
310310
if let ast::StmtKind::Expr(ref expr) = block.stmts[0].node {
311311
(
312-
!context.config.force_multiline_blocks() && can_extend_match_arm_body(expr),
312+
!context.config.force_multiline_blocks() && can_flatten_block_around_this(expr),
313313
&*expr,
314314
)
315315
} else {
@@ -503,15 +503,17 @@ fn nop_block_collapse(block_str: Option<String>, budget: usize) -> Option<String
503503
})
504504
}
505505

506-
fn can_extend_match_arm_body(body: &ast::Expr) -> bool {
506+
fn can_flatten_block_around_this(body: &ast::Expr) -> bool {
507507
match body.node {
508508
// We do not allow `if` to stay on the same line, since we could easily mistake
509509
// `pat => if cond { ... }` and `pat if cond => { ... }`.
510510
ast::ExprKind::If(..) | ast::ExprKind::IfLet(..) => false,
511-
ast::ExprKind::ForLoop(..)
512-
| ast::ExprKind::Loop(..)
513-
| ast::ExprKind::While(..)
514-
| ast::ExprKind::WhileLet(..)
511+
// We do not allow collapsing a block around expression with condition
512+
// to avoid it being cluttered with match arm.
513+
ast::ExprKind::ForLoop(..) | ast::ExprKind::While(..) | ast::ExprKind::WhileLet(..) => {
514+
false
515+
}
516+
ast::ExprKind::Loop(..)
515517
| ast::ExprKind::Match(..)
516518
| ast::ExprKind::Block(..)
517519
| ast::ExprKind::Closure(..)
@@ -525,7 +527,7 @@ fn can_extend_match_arm_body(body: &ast::Expr) -> bool {
525527
| ast::ExprKind::Box(ref expr)
526528
| ast::ExprKind::Try(ref expr)
527529
| ast::ExprKind::Unary(_, ref expr)
528-
| ast::ExprKind::Cast(ref expr, _) => can_extend_match_arm_body(expr),
530+
| ast::ExprKind::Cast(ref expr, _) => can_flatten_block_around_this(expr),
529531
_ => false,
530532
}
531533
}

tests/source/match.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,26 @@ fn match_with_beginning_vert() {
462462
| Foo::C => println!("C"),
463463
}
464464
}
465+
466+
// #2376
467+
// Preserve block around expressions with condition.
468+
fn issue_2376() {
469+
let mut x = None;
470+
match x {
471+
Some(0) => {
472+
for i in 1..11 {
473+
x = Some(i);
474+
}
475+
}
476+
Some(ref mut y) => {
477+
while *y < 10 {
478+
*y += 1;
479+
}
480+
}
481+
None => {
482+
while let None = x {
483+
x = Some(10);
484+
}
485+
}
486+
}
487+
}

tests/target/match.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,3 +493,26 @@ fn match_with_beginning_vert() {
493493
| Foo::C => println!("C"),
494494
}
495495
}
496+
497+
// #2376
498+
// Preserve block around expressions with condition.
499+
fn issue_2376() {
500+
let mut x = None;
501+
match x {
502+
Some(0) => {
503+
for i in 1..11 {
504+
x = Some(i);
505+
}
506+
}
507+
Some(ref mut y) => {
508+
while *y < 10 {
509+
*y += 1;
510+
}
511+
}
512+
None => {
513+
while let None = x {
514+
x = Some(10);
515+
}
516+
}
517+
}
518+
}

0 commit comments

Comments
 (0)