diff --git a/src/config/options.rs b/src/config/options.rs index 10c49977ef6..d58c8ed0f3b 100644 --- a/src/config/options.rs +++ b/src/config/options.rs @@ -448,8 +448,11 @@ pub enum MatchArmLeadingPipe { pub enum MatchArmWrapping { /// Follow the Style Guide Prescription Default, - /// Don't block wrap when the first line can't fit + /// Same as Default, except don't block wrap match arms when the opening line of its body + /// can't fit on the same line as the `=>`. NoBlockFirstLine, - /// Always wrap match arms + /// Always block wrap match arms Always, + /// Preserve the block wrapping on match arms + Preserve, } diff --git a/src/matches.rs b/src/matches.rs index 072c436def8..f9f2b5b0b07 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -326,6 +326,10 @@ fn flatten_arm_body<'a>( if let ast::ExprKind::Block(..) = expr.kind { flatten_arm_body(context, expr, None) } else { + if context.config.match_arm_wrapping() == MatchArmWrapping::Preserve { + return (false, body); + } + let cond_becomes_muti_line = opt_shape .and_then(|shape| rewrite_cond(context, expr, shape)) .map_or(false, |cond| cond.contains('\n')); @@ -416,7 +420,8 @@ fn rewrite_match_body( let indent_str = shape.indent.to_string_with_newline(context.config); let (body_prefix, body_suffix) = match context.config.match_arm_wrapping() { - MatchArmWrapping::Default | MatchArmWrapping::Always if !context.inside_macro() => { + MatchArmWrapping::NoBlockFirstLine => ("", String::from(",")), + _ if !context.inside_macro() => { let comma = if context.config.match_block_trailing_comma() { "," } else { diff --git a/tests/source/configs/match_arm_wrapping/always.rs b/tests/source/configs/match_arm_wrapping/always.rs index ac51e4cef88..debb491f8b1 100644 --- a/tests/source/configs/match_arm_wrapping/always.rs +++ b/tests/source/configs/match_arm_wrapping/always.rs @@ -3,16 +3,16 @@ fn main() { match lorem { - 1 => foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x), - 2 => { + 1000 => foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x), + 2000 => { println!("{}", sit) } - 3 => panic!(), - 4 => (), - y => { + 3000 => panic!(), + 4000 => (), + ipsum => { // Some comment - let ipsum = y - 1; - println!("{}", ipsum); + let dolor = ipsum % 2; + println!("{}", dolor); } } } diff --git a/tests/source/configs/match_arm_wrapping/preserve.rs b/tests/source/configs/match_arm_wrapping/preserve.rs new file mode 100644 index 00000000000..c1c387b3622 --- /dev/null +++ b/tests/source/configs/match_arm_wrapping/preserve.rs @@ -0,0 +1,20 @@ +// rustfmt-match_arm_wrapping: Preserve +// Wrap match-arms + +fn main() { + match lorem { + 1000 => foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x), + 2000 => { + println!("{}", sit) + } + 3000 => panic!(), + 4000 => { + () + } + ipsum => { + // Some comment + let dolor = ipsum % 2; + println!("{}", dolor); + } + } +} diff --git a/tests/target/configs/match_arm_wrapping/always.rs b/tests/target/configs/match_arm_wrapping/always.rs index 7f410121698..571149692e9 100644 --- a/tests/target/configs/match_arm_wrapping/always.rs +++ b/tests/target/configs/match_arm_wrapping/always.rs @@ -3,22 +3,22 @@ fn main() { match lorem { - 1 => { + 1000 => { foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x) } - 2 => { + 2000 => { println!("{}", sit) } - 3 => { + 3000 => { panic!() } - 4 => { + 4000 => { () } - y => { + ipsum => { // Some comment - let ipsum = y - 1; - println!("{}", ipsum); + let dolor = ipsum % 2; + println!("{}", dolor); } } } diff --git a/tests/target/configs/match_arm_wrapping/preserve.rs b/tests/target/configs/match_arm_wrapping/preserve.rs new file mode 100644 index 00000000000..75a5b1a9be3 --- /dev/null +++ b/tests/target/configs/match_arm_wrapping/preserve.rs @@ -0,0 +1,22 @@ +// rustfmt-match_arm_wrapping: Preserve +// Wrap match-arms + +fn main() { + match lorem { + 1000 => { + foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x) + } + 2000 => { + println!("{}", sit) + } + 3000 => panic!(), + 4000 => { + () + } + ipsum => { + // Some comment + let dolor = ipsum % 2; + println!("{}", dolor); + } + } +}