]> git.lizzy.rs Git - rust.git/commitdiff
Trailing commas for match block arms
authorBryce Van Dyk <bryce@vandyk.net.nz>
Mon, 30 Nov 2015 08:51:20 +0000 (21:51 +1300)
committerBryce Van Dyk <bryce@vandyk.net.nz>
Mon, 30 Nov 2015 08:51:20 +0000 (21:51 +1300)
Attempt to implement an option for trailing commas for block based match arms (issue
173). Put in place test files to verify this behaviour.

src/config.rs
src/expr.rs
tests/source/match-block-trailing-comma.rs [new file with mode: 0644]
tests/target/match-block-trailing-comma.rs [new file with mode: 0644]

index c02d618437fa3620648a893630191b0babf2ccfe..2ea175c385f308fd6bbc6487492285a30af20f32 100644 (file)
@@ -308,4 +308,6 @@ fn default() -> Config {
     hard_tabs: bool, false, "Use tab characters for indentation, spaces for alignment";
     wrap_comments: bool, false, "Break comments to fit on the line";
     wrap_match_arms: bool, true, "Wrap multiline match arms in blocks";
+    match_block_trailing_comma: bool, false,
+        "Put a trailing comma after a block based match arm (non-block arms are not affected)";
 }
index 040c83488a72ed375a63e768366e495b24ef30e2..9338160d98fdc3f283bf81dec9fd9722de7d4e9a 100644 (file)
@@ -823,7 +823,7 @@ fn rewrite_match(context: &RewriteContext,
             // We couldn't format the arm, just reproduce the source.
             let snippet = context.snippet(mk_sp(arm_start_pos(arm), arm_end_pos(arm)));
             result.push_str(&snippet);
-            result.push_str(arm_comma(&arm.body));
+            result.push_str(arm_comma(context, &arm.body));
         }
     }
     // BytePos(1) = closing match brace.
@@ -854,8 +854,10 @@ fn arm_end_pos(arm: &ast::Arm) -> BytePos {
     arm.body.span.hi
 }
 
-fn arm_comma(body: &ast::Expr) -> &'static str {
-    if let ast::ExprBlock(ref block) = body.node {
+fn arm_comma(context: &RewriteContext, body: &ast::Expr) -> &'static str {
+    if context.config.match_block_trailing_comma {
+        ","
+    } else if let ast::ExprBlock(ref block) = body.node {
         if let ast::DefaultBlock = block.rules {
             ""
         } else {
@@ -950,7 +952,7 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
             ref x => x,
         };
 
-        let comma = arm_comma(body);
+        let comma = arm_comma(context, body);
 
         // Let's try and get the arm body on the same line as the condition.
         // 4 = ` => `.len()
@@ -961,7 +963,8 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
 
             match rewrite {
                 Some(ref body_str) if !body_str.contains('\n') || !context.config.wrap_match_arms ||
-                                      comma.is_empty() => {
+                                      comma.is_empty() ||
+                                      context.config.match_block_trailing_comma => {
                     return Some(format!("{}{} => {}{}",
                                         attr_str.trim_left(),
                                         pats_str,
diff --git a/tests/source/match-block-trailing-comma.rs b/tests/source/match-block-trailing-comma.rs
new file mode 100644 (file)
index 0000000..7f7ddd0
--- /dev/null
@@ -0,0 +1,13 @@
+// rustfmt-match_block_trailing_comma: true
+// Match expressions, no unwrapping of block arms or wrapping of multiline
+// expressions.
+
+fn foo() {
+    match x {
+        a => {
+            "line1";
+            "line2"
+        }
+        b => "bar",
+    }
+}
diff --git a/tests/target/match-block-trailing-comma.rs b/tests/target/match-block-trailing-comma.rs
new file mode 100644 (file)
index 0000000..c845bae
--- /dev/null
@@ -0,0 +1,13 @@
+// rustfmt-match_block_trailing_comma: true
+// Match expressions, no unwrapping of block arms or wrapping of multiline
+// expressions.
+
+fn foo() {
+    match x {
+        a => {
+            "line1";
+            "line2"
+        },
+        b => "bar",
+    }
+}