Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 01c14a2

Browse files
authored
Merge pull request rust-lang#3012 from YaLTeR/fix-issue-2496
Fix match arm block flattening
2 parents 81a4235 + 838df8d commit 01c14a2

File tree

8 files changed

+96
-20
lines changed

8 files changed

+96
-20
lines changed

Configurations.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,7 @@ fn main() {
13031303
});
13041304

13051305
match lorem {
1306-
None => if ipsum {
1306+
None => |ipsum| {
13071307
println!("Hello World");
13081308
},
13091309
Some(dolor) => foo(),
@@ -1324,7 +1324,7 @@ fn main() {
13241324

13251325
match lorem {
13261326
None => {
1327-
if ipsum {
1327+
|ipsum| {
13281328
println!("Hello World");
13291329
}
13301330
}

src/config/lists.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,13 @@ impl SeparatorPlace {
9595
) -> SeparatorPlace {
9696
match tactic {
9797
DefinitiveListTactic::Vertical => default,
98-
_ => if sep == "," {
99-
SeparatorPlace::Back
100-
} else {
101-
default
102-
},
98+
_ => {
99+
if sep == "," {
100+
SeparatorPlace::Back
101+
} else {
102+
default
103+
}
104+
}
103105
}
104106
}
105107
}

src/expr.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,13 @@ pub fn format_expr(
179179
Some(format!("break{}", id_str))
180180
}
181181
}
182-
ast::ExprKind::Yield(ref opt_expr) => if let Some(ref expr) = *opt_expr {
183-
rewrite_unary_prefix(context, "yield ", &**expr, shape)
184-
} else {
185-
Some("yield".to_string())
186-
},
182+
ast::ExprKind::Yield(ref opt_expr) => {
183+
if let Some(ref expr) = *opt_expr {
184+
rewrite_unary_prefix(context, "yield ", &**expr, shape)
185+
} else {
186+
Some("yield".to_string())
187+
}
188+
}
187189
ast::ExprKind::Closure(capture, asyncness, movability, ref fn_decl, ref body, _) => {
188190
closures::rewrite_closure(
189191
capture, asyncness, movability, fn_decl, body, expr.span, context, shape,

src/matches.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use comment::{combine_strs_with_missing_comments, rewrite_comment};
2020
use config::{Config, ControlBraceStyle, IndentStyle};
2121
use expr::{
2222
format_expr, is_empty_block, is_simple_block, is_unsafe_block, prefer_next_line,
23-
rewrite_multiple_patterns, ExprType, RhsTactics, ToExpr,
23+
rewrite_multiple_patterns, ExprType, RhsTactics,
2424
};
2525
use lists::{itemize_list, write_list, ListFormatting};
2626
use rewrite::{Rewrite, RewriteContext};
@@ -314,23 +314,21 @@ fn block_can_be_flattened<'a>(
314314
// @extend: true if the arm body can be put next to `=>`
315315
// @body: flattened body, if the body is block with a single expression
316316
fn flatten_arm_body<'a>(context: &'a RewriteContext, body: &'a ast::Expr) -> (bool, &'a ast::Expr) {
317+
let can_extend =
318+
|expr| !context.config.force_multiline_blocks() && can_flatten_block_around_this(expr);
319+
317320
if let Some(ref block) = block_can_be_flattened(context, body) {
318321
if let ast::StmtKind::Expr(ref expr) = block.stmts[0].node {
319322
if let ast::ExprKind::Block(..) = expr.node {
320323
flatten_arm_body(context, expr)
321324
} else {
322-
let can_extend_expr =
323-
!context.config.force_multiline_blocks() && can_flatten_block_around_this(expr);
324-
(can_extend_expr, &*expr)
325+
(can_extend(expr), &*expr)
325326
}
326327
} else {
327328
(false, &*body)
328329
}
329330
} else {
330-
(
331-
!context.config.force_multiline_blocks() && body.can_be_overflowed(context, 1),
332-
&*body,
333-
)
331+
(can_extend(body), &*body)
334332
}
335333
}
336334

tests/source/issue-2496.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// rustfmt-indent_style: Visual
2+
fn main() {
3+
match option {
4+
None => some_function(first_reasonably_long_argument,
5+
second_reasonably_long_argument),
6+
}
7+
}
8+
9+
fn main() {
10+
match option {
11+
None => {
12+
some_function(first_reasonably_long_argument,
13+
second_reasonably_long_argument)
14+
}
15+
}
16+
}

tests/source/match-flattening.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
fn main() {
2+
match option {
3+
None => if condition {
4+
true
5+
} else {
6+
false
7+
},
8+
}
9+
}
10+
11+
fn main() {
12+
match option {
13+
None => {
14+
if condition {
15+
true
16+
} else {
17+
false
18+
}
19+
}
20+
}
21+
}

tests/target/issue-2496.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// rustfmt-indent_style: Visual
2+
fn main() {
3+
match option {
4+
None => some_function(first_reasonably_long_argument,
5+
second_reasonably_long_argument),
6+
}
7+
}
8+
9+
fn main() {
10+
match option {
11+
None => some_function(first_reasonably_long_argument,
12+
second_reasonably_long_argument),
13+
}
14+
}

tests/target/match-flattening.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
fn main() {
2+
match option {
3+
None => {
4+
if condition {
5+
true
6+
} else {
7+
false
8+
}
9+
}
10+
}
11+
}
12+
13+
fn main() {
14+
match option {
15+
None => {
16+
if condition {
17+
true
18+
} else {
19+
false
20+
}
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)