]> git.lizzy.rs Git - rust.git/commitdiff
Update trailing comma match logic, add tests
authorBryce Van Dyk <bryce@vandyk.net.nz>
Tue, 1 Dec 2015 07:10:57 +0000 (20:10 +1300)
committerBryce Van Dyk <bryce@vandyk.net.nz>
Tue, 1 Dec 2015 07:10:57 +0000 (20:10 +1300)
Updates the traling comma code to attempt to handle multiline non-block bodies when
adding traling commas to blocks. Also add and update tests to cover better
the interactions between trailing commas and wrapping match arms.

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

index 9338160d98fdc3f283bf81dec9fd9722de7d4e9a..cc518003e8805525e54f203efb7abb48a68da019 100644 (file)
@@ -20,7 +20,7 @@
 use utils::{span_after, extra_offset, last_line_width, wrap_str, binary_search, first_line_width,
             semicolon_for_stmt};
 use visitor::FmtVisitor;
-use config::{StructLitStyle, MultilineStyle};
+use config::{Config, StructLitStyle, MultilineStyle};
 use comment::{FindUncommented, rewrite_comment, contains_comment};
 use types::rewrite_path;
 use items::{span_lo_for_arg, span_hi_for_arg};
@@ -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(context, &arm.body));
+            result.push_str(arm_comma(&context.config, &arm.body));
         }
     }
     // BytePos(1) = closing match brace.
@@ -854,8 +854,8 @@ fn arm_end_pos(arm: &ast::Arm) -> BytePos {
     arm.body.span.hi
 }
 
-fn arm_comma(context: &RewriteContext, body: &ast::Expr) -> &'static str {
-    if context.config.match_block_trailing_comma {
+fn arm_comma(config: &Config, body: &ast::Expr) -> &'static str {
+    if config.match_block_trailing_comma {
         ","
     } else if let ast::ExprBlock(ref block) = body.node {
         if let ast::DefaultBlock = block.rules {
@@ -952,7 +952,7 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
             ref x => x,
         };
 
-        let comma = arm_comma(context, body);
+        let comma = arm_comma(&context.config, body);
 
         // Let's try and get the arm body on the same line as the condition.
         // 4 = ` => `.len()
@@ -960,11 +960,14 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
             let budget = context.config.max_width - line_start - comma.len() - 4;
             let offset = Indent::new(offset.block_indent, line_start + 4 - offset.block_indent);
             let rewrite = nop_block_collapse(body.rewrite(context, budget, offset), budget);
+            let is_body_str_block = match rewrite {
+                Some(ref s) => s.trim().starts_with("{") && s.trim().ends_with("}"),
+                None => false,
+            };
 
             match rewrite {
                 Some(ref body_str) if !body_str.contains('\n') || !context.config.wrap_match_arms ||
-                                      comma.is_empty() ||
-                                      context.config.match_block_trailing_comma => {
+                                      is_body_str_block => {
                     return Some(format!("{}{} => {}{}",
                                         attr_str.trim_left(),
                                         pats_str,
@@ -986,7 +989,11 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
                                                          body_budget));
         let indent_str = offset.block_indent(context.config).to_string(context.config);
         let (body_prefix, body_suffix) = if context.config.wrap_match_arms {
-            (" {", "}")
+            if context.config.match_block_trailing_comma {
+                (" {", "},")
+            } else {
+                (" {", "}")
+            }
         } else {
             ("", "")
         };
index 7f7ddd0a6b4f157d5cb8aaf61a53771690a6cda3..e9daac13bf96f03674eed676d5b8552e5397908e 100644 (file)
@@ -8,6 +8,7 @@ fn foo() {
             "line1";
             "line2"
         }
-        b => "bar",
+        b => (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
+              bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb),
     }
 }
diff --git a/tests/source/match-nowrap-trailing-comma.rs b/tests/source/match-nowrap-trailing-comma.rs
new file mode 100644 (file)
index 0000000..f6e8840
--- /dev/null
@@ -0,0 +1,15 @@
+// rustfmt-wrap_match_arms: false
+// 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 => (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
+              bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb),
+    }
+}
index c845baed376c7a33cef7f174e5aa04bce9e602eb..b00def007e8a14efa719b6f156cf6a50e304115f 100644 (file)
@@ -8,6 +8,9 @@ fn foo() {
             "line1";
             "line2"
         },
-        b => "bar",
+        b => {
+            (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
+             bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb)
+        },
     }
 }
diff --git a/tests/target/match-nowrap-trailing-comma.rs b/tests/target/match-nowrap-trailing-comma.rs
new file mode 100644 (file)
index 0000000..6b2e430
--- /dev/null
@@ -0,0 +1,15 @@
+// rustfmt-wrap_match_arms: false
+// 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 => (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
+              bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb),
+    }
+}