]> git.lizzy.rs Git - rust.git/commitdiff
Implement match_arm_forces_newline option (#2039)
authorÖmer Sinan Ağacan <omeragacan@gmail.com>
Sat, 7 Oct 2017 12:44:28 +0000 (15:44 +0300)
committerÖmer Sinan Ağacan <omeragacan@gmail.com>
Thu, 26 Oct 2017 17:20:36 +0000 (20:20 +0300)
src/config.rs
src/expr.rs

index 8aff7747dd2793001bed727d838b7a7bbe0659e3..8c200302c5b0894bb017e22c812231752ba782d6 100644 (file)
@@ -597,6 +597,8 @@ pub fn get_toml_path(dir: &Path) -> Result<Option<PathBuf>, Error> {
                                   the same line with the pattern of arms";
     match_block_trailing_comma: bool, false,
         "Put a trailing comma after a block based match arm (non-block arms are not affected)";
+    match_arm_forces_newline: bool, false,
+        "Force match arm bodies to be in a new lines";
     indent_match_arms: bool, true, "Indent match arms instead of keeping them at the same \
                                     indentation level as the match keyword";
     match_pattern_separator_break_point: SeparatorPlace, SeparatorPlace::Back,
index 8bef270cf763a74d2d6c86aaa67b1cd403976133..51dc7a9bb7a688f0939ded99b0bb74fa9adbb763 100644 (file)
@@ -1695,15 +1695,20 @@ fn rewrite_match_body(
     is_last: bool,
 ) -> Option<String> {
     let (extend, body) = flatten_arm_body(context, body);
-
-    let comma = arm_comma(context.config, body, is_last);
-    let alt_block_sep = String::from("\n") + &shape.indent.block_only().to_string(context.config);
-    let alt_block_sep = alt_block_sep.as_str();
     let (is_block, is_empty_block) = if let ast::ExprKind::Block(ref block) = body.node {
         (true, is_empty_block(block, context.codemap))
     } else {
         (false, false)
     };
+    let extend = if context.config.match_arm_forces_newline() {
+        is_block
+    } else {
+        extend
+    };
+
+    let comma = arm_comma(context.config, body, is_last);
+    let alt_block_sep = String::from("\n") + &shape.indent.block_only().to_string(context.config);
+    let alt_block_sep = alt_block_sep.as_str();
 
     let combine_orig_body = |body_str: &str| {
         let block_sep = match context.config.control_brace_style() {
@@ -1716,7 +1721,11 @@ fn rewrite_match_body(
 
     let forbid_same_line = has_guard && pats_str.contains('\n') && !is_empty_block;
     let next_line_indent = if is_block {
-        shape.indent
+        if is_empty_block {
+            shape.indent.block_indent(context.config)
+        } else {
+            shape.indent
+        }
     } else {
         shape.indent.block_indent(context.config)
     };
@@ -1772,7 +1781,7 @@ fn rewrite_match_body(
 
         match rewrite {
             Some(ref body_str)
-                if !forbid_same_line
+                if !forbid_same_line && !context.config.match_arm_forces_newline()
                     && (is_block
                         || (!body_str.contains('\n') && body_str.len() <= body_shape.width)) =>
             {