]> git.lizzy.rs Git - rust.git/commitdiff
Merge pull request #2090 from topecongiro/issue-2087
authorNick Cameron <nrc@ncameron.org>
Fri, 27 Oct 2017 09:48:13 +0000 (15:18 +0530)
committerGitHub <noreply@github.com>
Fri, 27 Oct 2017 09:48:13 +0000 (15:18 +0530)
Only read the trailing comma of outermost fn call

Configurations.md
src/config.rs
src/expr.rs
tests/source/closure.rs
tests/source/configs-match_arm_forces_newline-true.rs [new file with mode: 0644]
tests/target/closure.rs
tests/target/configs-match_arm_forces_newline-true.rs [new file with mode: 0644]

index 16849d11880a5f1c768264e9376607ecf7558bb2..bd67caef09fcda5b79cb3da6f3f9c51bbf98d83e 100644 (file)
@@ -1229,6 +1229,48 @@ struct Dolor<T>
 }
 ```
 
+## `match_arm_forces_newline`
+
+Consistently put match arms (block based or not) in a newline.
+
+- **Default value**: `false`
+- **Possible values**: `true`, `false`
+
+#### `false` (default):
+
+```rust
+match x {
+    // a non-empty block
+    X0 => {
+        f();
+    }
+    // an empty block
+    X1 => {}
+    // a non-block
+    X2 => println!("ok"),
+}
+```
+
+#### `true`:
+
+```rust
+match x {
+    // a non-empty block
+    X0 => {
+        f();
+    }
+    // an empty block
+    X1 =>
+        {}
+    // a non-block
+    X2 => {
+        println!("ok")
+    }
+}
+```
+
+See also: [`wrap_match_arms`](#wrap_match_arms).
+
 ## `match_block_trailing_comma`
 
 Put a trailing comma after a block based match arm (non-block arms are not affected)
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 33dc3a40e21ab337dd43fdbb7c40902e835a1426..9a020646927599c683b1011414e26c77ba39b8b6 100644 (file)
@@ -899,9 +899,6 @@ fn rewrite_cond(context: &RewriteContext, expr: &ast::Expr, shape: Shape) -> Opt
             };
             cond.rewrite(context, cond_shape)
         }
-        ast::ExprKind::Block(ref block) if block.stmts.len() == 1 => {
-            stmt_expr(&block.stmts[0]).and_then(|e| rewrite_cond(context, e, shape))
-        }
         _ => to_control_flow(expr, ExprType::SubExpression).and_then(|control_flow| {
             let alt_block_sep =
                 String::from("\n") + &shape.indent.block_only().to_string(context.config);
@@ -1695,15 +1692,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 +1718,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 +1778,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)) =>
             {
@@ -2219,7 +2225,7 @@ fn rewrite_last_closure(
 ) -> Option<String> {
     if let ast::ExprKind::Closure(capture, ref fn_decl, ref body, _) = expr.node {
         let body = match body.node {
-            ast::ExprKind::Block(ref block) if block.stmts.len() == 1 => {
+            ast::ExprKind::Block(ref block) if is_simple_block(block, context.codemap) => {
                 stmt_expr(&block.stmts[0]).unwrap_or(body)
             }
             _ => body,
index 39059f40807fd80d4847a5b34b45830c6f2a5188..5bf177838420bb76f733584b9e56e45649ebcd7f 100644 (file)
@@ -41,7 +41,16 @@ fn main() {
     let closure_with_return_type = |aaaaaaaaaaaaaaaaaaaaaaarg1, aaaaaaaaaaaaaaaaaaaaaaarg2| -> Strong { "sup".to_owned() };
 
     |arg1, arg2, _, _, arg3, arg4| { let temp = arg4 + arg3;
-                                     arg2 * arg1 - temp }
+                                     arg2 * arg1 - temp };
+
+    let block_body_with_comment = args.iter()
+        .map(|a| {
+            // Emitting only dep-info is possible only for final crate type, as
+            // as others may emit required metadata for dependent crate types
+            if a.starts_with("--emit") && is_final_crate_type && !self.workspace_mode {
+                "--emit=dep-info"
+            } else { a }
+        });
 }
 
 fn issue311() {
diff --git a/tests/source/configs-match_arm_forces_newline-true.rs b/tests/source/configs-match_arm_forces_newline-true.rs
new file mode 100644 (file)
index 0000000..e9c8d57
--- /dev/null
@@ -0,0 +1,20 @@
+// rustfmt-match_arm_forces_newline: true
+// rustfmt-wrap_match_arms: false
+
+// match_arm_forces_newline puts all match arms bodies in a newline and indents
+// them.
+
+fn main() {
+    match x() {
+        // a short non-empty block
+        X0 => { f(); }
+        // a long non-empty block
+        X1 => { some.really.long.expression.fooooooooooooooooooooooooooooooooooooooooo().baaaaarrrrrrrrrrrrrrrrrrrrrrrrrr(); }
+        // an empty block
+        X2 => {}
+        // a short non-block
+        X3 => println!("ok"),
+        // a long non-block
+        X4 => foo.bar.baz.test.x.y.z.a.s.d.fasdfasdf.asfads.fasd.fasdfasdf.dfasfdsaf(),
+    }
+}
index 9d59bfb64cbdb63281cc34628d893fca08153959..2005d85bf175fc26f088ba64f7fc64d57045e1de 100644 (file)
@@ -60,7 +60,17 @@ fn main() {
     |arg1, arg2, _, _, arg3, arg4| {
         let temp = arg4 + arg3;
         arg2 * arg1 - temp
-    }
+    };
+
+    let block_body_with_comment = args.iter().map(|a| {
+        // Emitting only dep-info is possible only for final crate type, as
+        // as others may emit required metadata for dependent crate types
+        if a.starts_with("--emit") && is_final_crate_type && !self.workspace_mode {
+            "--emit=dep-info"
+        } else {
+            a
+        }
+    });
 }
 
 fn issue311() {
diff --git a/tests/target/configs-match_arm_forces_newline-true.rs b/tests/target/configs-match_arm_forces_newline-true.rs
new file mode 100644 (file)
index 0000000..5629a56
--- /dev/null
@@ -0,0 +1,44 @@
+// rustfmt-match_arm_forces_newline: true
+// rustfmt-wrap_match_arms: false
+
+// match_arm_forces_newline puts all match arms bodies in a newline and indents
+// them.
+
+fn main() {
+    match x() {
+        // a short non-empty block
+        X0 => {
+            f();
+        }
+        // a long non-empty block
+        X1 => {
+            some.really
+                .long
+                .expression
+                .fooooooooooooooooooooooooooooooooooooooooo()
+                .baaaaarrrrrrrrrrrrrrrrrrrrrrrrrr();
+        }
+        // an empty block
+        X2 =>
+            {}
+        // a short non-block
+        X3 =>
+            println!("ok"),
+        // a long non-block
+        X4 =>
+            foo.bar
+                .baz
+                .test
+                .x
+                .y
+                .z
+                .a
+                .s
+                .d
+                .fasdfasdf
+                .asfads
+                .fasd
+                .fasdfasdf
+                .dfasfdsaf(),
+    }
+}