about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorNick Cameron <nrc@ncameron.org>2015-10-22 10:56:02 +1300
committerNick Cameron <nrc@ncameron.org>2015-10-22 10:56:02 +1300
commit032e6ae8332dd3a4bc10617e53ebd71835bb713d (patch)
treeede5911024109e42891364eade16c7b67baf1be4 /src
parentca50af7f03f7be722f1406a4fbcd2fb74cd751dc (diff)
parente720218ffb6553764b516e074fee96eb0efb79e9 (diff)
Merge pull request #517 from eefriedman/match-comma
Improve handling of commas after match arms.
Diffstat (limited to 'src')
-rw-r--r--src/expr.rs19
1 files changed, 14 insertions, 5 deletions
diff --git a/src/expr.rs b/src/expr.rs
index f0a877db215..c619376f276 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -779,6 +779,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));
         }
     }
     // BytePos(1) = closing match brace.
@@ -809,6 +810,18 @@ 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 {
+        if let ast::DefaultBlock = block.rules {
+            ""
+        } else {
+            ","
+        }
+    } else {
+        ","
+    }
+}
+
 // Match arms.
 impl Rewrite for ast::Arm {
     fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
@@ -881,11 +894,7 @@ impl Rewrite for ast::Arm {
             line_start += offset.width();
         }
 
-        let comma = if let ast::ExprBlock(_) = body.node {
-            ""
-        } else {
-            ","
-        };
+        let comma = arm_comma(body);
 
         // Let's try and get the arm body on the same line as the condition.
         // 4 = ` => `.len()