Skip to content

Commit 663d7bc

Browse files
committed
feat: add Always option to match_arm_wrapping, with tests (rust-lang#4896)
1 parent d9b12c9 commit 663d7bc

File tree

4 files changed

+65
-20
lines changed

4 files changed

+65
-20
lines changed

Diff for: src/config/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -450,4 +450,6 @@ pub enum MatchArmWrapping {
450450
Default,
451451
/// Don't block wrap when the first line can't fit
452452
NoBlockFirstLine,
453+
/// Always wrap match arms
454+
Always,
453455
}

Diff for: src/matches.rs

+21-20
Original file line numberDiff line numberDiff line change
@@ -415,27 +415,23 @@ fn rewrite_match_body(
415415
}
416416

417417
let indent_str = shape.indent.to_string_with_newline(context.config);
418-
let (body_prefix, body_suffix) = if context.config.match_arm_wrapping()
419-
== MatchArmWrapping::Default
420-
&& !context.inside_macro()
421-
{
422-
let comma = if context.config.match_block_trailing_comma() {
423-
","
424-
} else {
425-
""
426-
};
427-
let semicolon = if context.config.version() == Version::One {
428-
""
429-
} else {
430-
if semicolon_for_expr(context, body) {
431-
";"
418+
let (body_prefix, body_suffix) = match context.config.match_arm_wrapping() {
419+
MatchArmWrapping::Default | MatchArmWrapping::Always if !context.inside_macro() => {
420+
let comma = if context.config.match_block_trailing_comma() {
421+
","
432422
} else {
433423
""
434-
}
435-
};
436-
("{", format!("{}{}}}{}", semicolon, indent_str, comma))
437-
} else {
438-
("", String::from(","))
424+
};
425+
426+
let semicolon = match context.config.version() {
427+
Version::One => "",
428+
_ if semicolon_for_expr(context, body) => ";",
429+
_ => "",
430+
};
431+
432+
("{", format!("{}{}}}{}", semicolon, indent_str, comma))
433+
}
434+
_ => ("", String::from(",")),
439435
};
440436

441437
let block_sep = match context.config.control_brace_style() {
@@ -464,7 +460,10 @@ fn rewrite_match_body(
464460
let orig_body_shape = shape
465461
.offset_left(extra_offset(pats_str, shape) + 4)
466462
.and_then(|shape| shape.sub_width(comma.len()));
467-
let orig_body = if forbid_same_line || !arrow_comment.is_empty() {
463+
let orig_body = if forbid_same_line
464+
|| !arrow_comment.is_empty()
465+
|| (!is_block && context.config.match_arm_wrapping() == MatchArmWrapping::Always)
466+
{
468467
None
469468
} else if let Some(body_shape) = orig_body_shape {
470469
let rewrite = nop_block_collapse(
@@ -485,6 +484,7 @@ fn rewrite_match_body(
485484
} else {
486485
None
487486
};
487+
488488
let orig_budget = orig_body_shape.map_or(0, |shape| shape.width);
489489

490490
// Try putting body on the next line and see if it looks better.
@@ -493,6 +493,7 @@ fn rewrite_match_body(
493493
format_expr(body, ExprType::Statement, context, next_line_body_shape),
494494
next_line_body_shape.width,
495495
);
496+
496497
match (orig_body, next_line_body) {
497498
(Some(ref orig_str), Some(ref next_line_str))
498499
if prefer_next_line(orig_str, next_line_str, RhsTactics::Default) =>

Diff for: tests/source/configs/match_arm_wrapping/always.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// rustfmt-match_arm_wrapping: Always
2+
// Wrap match-arms
3+
4+
fn main() {
5+
match lorem {
6+
1 => foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
7+
2 => {
8+
println!("{}", sit)
9+
}
10+
3 => panic!(),
11+
4 => (),
12+
y => {
13+
// Some comment
14+
let ipsum = y - 1;
15+
println!("{}", ipsum);
16+
}
17+
}
18+
}

Diff for: tests/target/configs/match_arm_wrapping/always.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// rustfmt-match_arm_wrapping: Always
2+
// Wrap match-arms
3+
4+
fn main() {
5+
match lorem {
6+
1 => {
7+
foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
8+
}
9+
2 => {
10+
println!("{}", sit)
11+
}
12+
3 => {
13+
panic!()
14+
}
15+
4 => {
16+
()
17+
}
18+
y => {
19+
// Some comment
20+
let ipsum = y - 1;
21+
println!("{}", ipsum);
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)