From 5b9b7d5b9f357252e7f8c42afcfcc073f110968c Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Wed, 21 Mar 2018 22:20:03 +0900 Subject: [PATCH] Do not collapse block around expr with condition on match arm Closes #2376. --- src/matches.rs | 16 +++++++++------- tests/source/match.rs | 23 +++++++++++++++++++++++ tests/target/match.rs | 23 +++++++++++++++++++++++ 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/src/matches.rs b/src/matches.rs index b97fbd874177..83ff70ea1968 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -309,7 +309,7 @@ fn flatten_arm_body<'a>(context: &'a RewriteContext, body: &'a ast::Expr) -> (bo { if let ast::StmtKind::Expr(ref expr) = block.stmts[0].node { ( - !context.config.force_multiline_blocks() && can_extend_match_arm_body(expr), + !context.config.force_multiline_blocks() && can_flatten_block_around_this(expr), &*expr, ) } else { @@ -503,15 +503,17 @@ fn nop_block_collapse(block_str: Option, budget: usize) -> Option bool { +fn can_flatten_block_around_this(body: &ast::Expr) -> bool { match body.node { // We do not allow `if` to stay on the same line, since we could easily mistake // `pat => if cond { ... }` and `pat if cond => { ... }`. ast::ExprKind::If(..) | ast::ExprKind::IfLet(..) => false, - ast::ExprKind::ForLoop(..) - | ast::ExprKind::Loop(..) - | ast::ExprKind::While(..) - | ast::ExprKind::WhileLet(..) + // We do not allow collapsing a block around expression with condition + // to avoid it being cluttered with match arm. + ast::ExprKind::ForLoop(..) | ast::ExprKind::While(..) | ast::ExprKind::WhileLet(..) => { + false + } + ast::ExprKind::Loop(..) | ast::ExprKind::Match(..) | ast::ExprKind::Block(..) | ast::ExprKind::Closure(..) @@ -525,7 +527,7 @@ fn can_extend_match_arm_body(body: &ast::Expr) -> bool { | ast::ExprKind::Box(ref expr) | ast::ExprKind::Try(ref expr) | ast::ExprKind::Unary(_, ref expr) - | ast::ExprKind::Cast(ref expr, _) => can_extend_match_arm_body(expr), + | ast::ExprKind::Cast(ref expr, _) => can_flatten_block_around_this(expr), _ => false, } } diff --git a/tests/source/match.rs b/tests/source/match.rs index f38bf7cca977..31034f494022 100644 --- a/tests/source/match.rs +++ b/tests/source/match.rs @@ -462,3 +462,26 @@ fn match_with_beginning_vert() { | Foo::C => println!("C"), } } + +// #2376 +// Preserve block around expressions with condition. +fn issue_2376() { + let mut x = None; + match x { + Some(0) => { + for i in 1..11 { + x = Some(i); + } + } + Some(ref mut y) => { + while *y < 10 { + *y += 1; + } + } + None => { + while let None = x { + x = Some(10); + } + } + } +} diff --git a/tests/target/match.rs b/tests/target/match.rs index 72ffa966c01d..087fb80ef092 100644 --- a/tests/target/match.rs +++ b/tests/target/match.rs @@ -493,3 +493,26 @@ fn match_with_beginning_vert() { | Foo::C => println!("C"), } } + +// #2376 +// Preserve block around expressions with condition. +fn issue_2376() { + let mut x = None; + match x { + Some(0) => { + for i in 1..11 { + x = Some(i); + } + } + Some(ref mut y) => { + while *y < 10 { + *y += 1; + } + } + None => { + while let None = x { + x = Some(10); + } + } + } +}