From 14a4d02f876b4cb06c407244c3fcc7af5ea0ebd9 Mon Sep 17 00:00:00 2001 From: Ashvin Arsakularatne Date: Wed, 21 Jul 2021 15:05:00 +0530 Subject: [PATCH] feat: add Always option to match_arm_wrapping, with tests (#4896) --- src/config/options.rs | 2 + src/matches.rs | 41 ++++++++++--------- .../configs/match_arm_wrapping/always.rs | 18 ++++++++ .../configs/match_arm_wrapping/always.rs | 24 +++++++++++ 4 files changed, 65 insertions(+), 20 deletions(-) create mode 100644 tests/source/configs/match_arm_wrapping/always.rs create mode 100644 tests/target/configs/match_arm_wrapping/always.rs diff --git a/src/config/options.rs b/src/config/options.rs index 6ab31eb33f7..10c49977ef6 100644 --- a/src/config/options.rs +++ b/src/config/options.rs @@ -450,4 +450,6 @@ pub enum MatchArmWrapping { Default, /// Don't block wrap when the first line can't fit NoBlockFirstLine, + /// Always wrap match arms + Always, } diff --git a/src/matches.rs b/src/matches.rs index 686729aba1d..072c436def8 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -415,27 +415,23 @@ fn rewrite_match_body( } let indent_str = shape.indent.to_string_with_newline(context.config); - let (body_prefix, body_suffix) = if context.config.match_arm_wrapping() - == MatchArmWrapping::Default - && !context.inside_macro() - { - let comma = if context.config.match_block_trailing_comma() { - "," - } else { - "" - }; - let semicolon = if context.config.version() == Version::One { - "" - } else { - if semicolon_for_expr(context, body) { - ";" + let (body_prefix, body_suffix) = match context.config.match_arm_wrapping() { + MatchArmWrapping::Default | MatchArmWrapping::Always if !context.inside_macro() => { + let comma = if context.config.match_block_trailing_comma() { + "," } else { "" - } - }; - ("{", format!("{}{}}}{}", semicolon, indent_str, comma)) - } else { - ("", String::from(",")) + }; + + let semicolon = match context.config.version() { + Version::One => "", + _ if semicolon_for_expr(context, body) => ";", + _ => "", + }; + + ("{", format!("{}{}}}{}", semicolon, indent_str, comma)) + } + _ => ("", String::from(",")), }; let block_sep = match context.config.control_brace_style() { @@ -464,7 +460,10 @@ fn rewrite_match_body( let orig_body_shape = shape .offset_left(extra_offset(pats_str, shape) + 4) .and_then(|shape| shape.sub_width(comma.len())); - let orig_body = if forbid_same_line || !arrow_comment.is_empty() { + let orig_body = if forbid_same_line + || !arrow_comment.is_empty() + || (!is_block && context.config.match_arm_wrapping() == MatchArmWrapping::Always) + { None } else if let Some(body_shape) = orig_body_shape { let rewrite = nop_block_collapse( @@ -485,6 +484,7 @@ fn rewrite_match_body( } else { None }; + let orig_budget = orig_body_shape.map_or(0, |shape| shape.width); // Try putting body on the next line and see if it looks better. @@ -493,6 +493,7 @@ fn rewrite_match_body( format_expr(body, ExprType::Statement, context, next_line_body_shape), next_line_body_shape.width, ); + match (orig_body, next_line_body) { (Some(ref orig_str), Some(ref next_line_str)) if prefer_next_line(orig_str, next_line_str, RhsTactics::Default) => diff --git a/tests/source/configs/match_arm_wrapping/always.rs b/tests/source/configs/match_arm_wrapping/always.rs new file mode 100644 index 00000000000..ac51e4cef88 --- /dev/null +++ b/tests/source/configs/match_arm_wrapping/always.rs @@ -0,0 +1,18 @@ +// rustfmt-match_arm_wrapping: Always +// Wrap match-arms + +fn main() { + match lorem { + 1 => foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x), + 2 => { + println!("{}", sit) + } + 3 => panic!(), + 4 => (), + y => { + // Some comment + let ipsum = y - 1; + println!("{}", ipsum); + } + } +} diff --git a/tests/target/configs/match_arm_wrapping/always.rs b/tests/target/configs/match_arm_wrapping/always.rs new file mode 100644 index 00000000000..7f410121698 --- /dev/null +++ b/tests/target/configs/match_arm_wrapping/always.rs @@ -0,0 +1,24 @@ +// rustfmt-match_arm_wrapping: Always +// Wrap match-arms + +fn main() { + match lorem { + 1 => { + foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x) + } + 2 => { + println!("{}", sit) + } + 3 => { + panic!() + } + 4 => { + () + } + y => { + // Some comment + let ipsum = y - 1; + println!("{}", ipsum); + } + } +}