mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-21 04:03:11 +00:00
Merge pull request #3012 from YaLTeR/fix-issue-2496
Fix match arm block flattening
This commit is contained in:
commit
01c14a2058
@ -1303,7 +1303,7 @@ fn main() {
|
||||
});
|
||||
|
||||
match lorem {
|
||||
None => if ipsum {
|
||||
None => |ipsum| {
|
||||
println!("Hello World");
|
||||
},
|
||||
Some(dolor) => foo(),
|
||||
@ -1324,7 +1324,7 @@ fn main() {
|
||||
|
||||
match lorem {
|
||||
None => {
|
||||
if ipsum {
|
||||
|ipsum| {
|
||||
println!("Hello World");
|
||||
}
|
||||
}
|
||||
|
@ -95,11 +95,13 @@ impl SeparatorPlace {
|
||||
) -> SeparatorPlace {
|
||||
match tactic {
|
||||
DefinitiveListTactic::Vertical => default,
|
||||
_ => if sep == "," {
|
||||
SeparatorPlace::Back
|
||||
} else {
|
||||
default
|
||||
},
|
||||
_ => {
|
||||
if sep == "," {
|
||||
SeparatorPlace::Back
|
||||
} else {
|
||||
default
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
12
src/expr.rs
12
src/expr.rs
@ -179,11 +179,13 @@ pub fn format_expr(
|
||||
Some(format!("break{}", id_str))
|
||||
}
|
||||
}
|
||||
ast::ExprKind::Yield(ref opt_expr) => if let Some(ref expr) = *opt_expr {
|
||||
rewrite_unary_prefix(context, "yield ", &**expr, shape)
|
||||
} else {
|
||||
Some("yield".to_string())
|
||||
},
|
||||
ast::ExprKind::Yield(ref opt_expr) => {
|
||||
if let Some(ref expr) = *opt_expr {
|
||||
rewrite_unary_prefix(context, "yield ", &**expr, shape)
|
||||
} else {
|
||||
Some("yield".to_string())
|
||||
}
|
||||
}
|
||||
ast::ExprKind::Closure(capture, asyncness, movability, ref fn_decl, ref body, _) => {
|
||||
closures::rewrite_closure(
|
||||
capture, asyncness, movability, fn_decl, body, expr.span, context, shape,
|
||||
|
@ -20,7 +20,7 @@ use comment::{combine_strs_with_missing_comments, rewrite_comment};
|
||||
use config::{Config, ControlBraceStyle, IndentStyle};
|
||||
use expr::{
|
||||
format_expr, is_empty_block, is_simple_block, is_unsafe_block, prefer_next_line,
|
||||
rewrite_multiple_patterns, ExprType, RhsTactics, ToExpr,
|
||||
rewrite_multiple_patterns, ExprType, RhsTactics,
|
||||
};
|
||||
use lists::{itemize_list, write_list, ListFormatting};
|
||||
use rewrite::{Rewrite, RewriteContext};
|
||||
@ -314,23 +314,21 @@ fn block_can_be_flattened<'a>(
|
||||
// @extend: true if the arm body can be put next to `=>`
|
||||
// @body: flattened body, if the body is block with a single expression
|
||||
fn flatten_arm_body<'a>(context: &'a RewriteContext, body: &'a ast::Expr) -> (bool, &'a ast::Expr) {
|
||||
let can_extend =
|
||||
|expr| !context.config.force_multiline_blocks() && can_flatten_block_around_this(expr);
|
||||
|
||||
if let Some(ref block) = block_can_be_flattened(context, body) {
|
||||
if let ast::StmtKind::Expr(ref expr) = block.stmts[0].node {
|
||||
if let ast::ExprKind::Block(..) = expr.node {
|
||||
flatten_arm_body(context, expr)
|
||||
} else {
|
||||
let can_extend_expr =
|
||||
!context.config.force_multiline_blocks() && can_flatten_block_around_this(expr);
|
||||
(can_extend_expr, &*expr)
|
||||
(can_extend(expr), &*expr)
|
||||
}
|
||||
} else {
|
||||
(false, &*body)
|
||||
}
|
||||
} else {
|
||||
(
|
||||
!context.config.force_multiline_blocks() && body.can_be_overflowed(context, 1),
|
||||
&*body,
|
||||
)
|
||||
(can_extend(body), &*body)
|
||||
}
|
||||
}
|
||||
|
||||
|
16
tests/source/issue-2496.rs
Normal file
16
tests/source/issue-2496.rs
Normal file
@ -0,0 +1,16 @@
|
||||
// rustfmt-indent_style: Visual
|
||||
fn main() {
|
||||
match option {
|
||||
None => some_function(first_reasonably_long_argument,
|
||||
second_reasonably_long_argument),
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
match option {
|
||||
None => {
|
||||
some_function(first_reasonably_long_argument,
|
||||
second_reasonably_long_argument)
|
||||
}
|
||||
}
|
||||
}
|
21
tests/source/match-flattening.rs
Normal file
21
tests/source/match-flattening.rs
Normal file
@ -0,0 +1,21 @@
|
||||
fn main() {
|
||||
match option {
|
||||
None => if condition {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
match option {
|
||||
None => {
|
||||
if condition {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
14
tests/target/issue-2496.rs
Normal file
14
tests/target/issue-2496.rs
Normal file
@ -0,0 +1,14 @@
|
||||
// rustfmt-indent_style: Visual
|
||||
fn main() {
|
||||
match option {
|
||||
None => some_function(first_reasonably_long_argument,
|
||||
second_reasonably_long_argument),
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
match option {
|
||||
None => some_function(first_reasonably_long_argument,
|
||||
second_reasonably_long_argument),
|
||||
}
|
||||
}
|
23
tests/target/match-flattening.rs
Normal file
23
tests/target/match-flattening.rs
Normal file
@ -0,0 +1,23 @@
|
||||
fn main() {
|
||||
match option {
|
||||
None => {
|
||||
if condition {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
match option {
|
||||
None => {
|
||||
if condition {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user