Skip to content

Commit 10fc27e

Browse files
committed
feat: add Preserve option to match_arm_wrapping (rust-lang#4896)
1 parent c3794e2 commit 10fc27e

File tree

6 files changed

+67
-17
lines changed

6 files changed

+67
-17
lines changed

src/config/options.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,11 @@ pub enum MatchArmLeadingPipe {
448448
pub enum MatchArmWrapping {
449449
/// Follow the Style Guide Prescription
450450
Default,
451-
/// Don't block wrap when the first line can't fit
451+
/// Same as Default, except don't block wrap match arms when the opening line of its body
452+
/// can't fit on the same line as the `=>`.
452453
NoBlockFirstLine,
453-
/// Always wrap match arms
454+
/// Always block wrap match arms
454455
Always,
456+
/// Preserve the block wrapping on match arms
457+
Preserve,
455458
}

src/matches.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ fn flatten_arm_body<'a>(
326326
if let ast::ExprKind::Block(..) = expr.kind {
327327
flatten_arm_body(context, expr, None)
328328
} else {
329+
if context.config.match_arm_wrapping() == MatchArmWrapping::Preserve {
330+
return (false, body);
331+
}
332+
329333
let cond_becomes_muti_line = opt_shape
330334
.and_then(|shape| rewrite_cond(context, expr, shape))
331335
.map_or(false, |cond| cond.contains('\n'));
@@ -416,7 +420,8 @@ fn rewrite_match_body(
416420

417421
let indent_str = shape.indent.to_string_with_newline(context.config);
418422
let (body_prefix, body_suffix) = match context.config.match_arm_wrapping() {
419-
MatchArmWrapping::Default | MatchArmWrapping::Always if !context.inside_macro() => {
423+
MatchArmWrapping::NoBlockFirstLine => ("", String::from(",")),
424+
_ if !context.inside_macro() => {
420425
let comma = if context.config.match_block_trailing_comma() {
421426
","
422427
} else {

tests/source/configs/match_arm_wrapping/always.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33

44
fn main() {
55
match lorem {
6-
1 => foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
7-
2 => {
6+
1000 => foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
7+
2000 => {
88
println!("{}", sit)
99
}
10-
3 => panic!(),
11-
4 => (),
12-
y => {
10+
3000 => panic!(),
11+
4000 => (),
12+
ipsum => {
1313
// Some comment
14-
let ipsum = y - 1;
15-
println!("{}", ipsum);
14+
let dolor = ipsum % 2;
15+
println!("{}", dolor);
1616
}
1717
}
1818
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// rustfmt-match_arm_wrapping: Preserve
2+
// Wrap match-arms
3+
4+
fn main() {
5+
match lorem {
6+
1000 => foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
7+
2000 => {
8+
println!("{}", sit)
9+
}
10+
3000 => panic!(),
11+
4000 => {
12+
()
13+
}
14+
ipsum => {
15+
// Some comment
16+
let dolor = ipsum % 2;
17+
println!("{}", dolor);
18+
}
19+
}
20+
}

tests/target/configs/match_arm_wrapping/always.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33

44
fn main() {
55
match lorem {
6-
1 => {
6+
1000 => {
77
foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
88
}
9-
2 => {
9+
2000 => {
1010
println!("{}", sit)
1111
}
12-
3 => {
12+
3000 => {
1313
panic!()
1414
}
15-
4 => {
15+
4000 => {
1616
()
1717
}
18-
y => {
18+
ipsum => {
1919
// Some comment
20-
let ipsum = y - 1;
21-
println!("{}", ipsum);
20+
let dolor = ipsum % 2;
21+
println!("{}", dolor);
2222
}
2323
}
2424
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// rustfmt-match_arm_wrapping: Preserve
2+
// Wrap match-arms
3+
4+
fn main() {
5+
match lorem {
6+
1000 => {
7+
foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
8+
}
9+
2000 => {
10+
println!("{}", sit)
11+
}
12+
3000 => panic!(),
13+
4000 => {
14+
()
15+
}
16+
ipsum => {
17+
// Some comment
18+
let dolor = ipsum % 2;
19+
println!("{}", dolor);
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)