]> git.lizzy.rs Git - rust.git/commitdiff
Add extra tests for match arm placement
authorMarcus Klaas <mail@marcusklaas.nl>
Sun, 27 Sep 2015 09:58:26 +0000 (11:58 +0200)
committerMarcus Klaas <mail@marcusklaas.nl>
Sun, 27 Sep 2015 09:58:26 +0000 (11:58 +0200)
src/expr.rs
tests/source/match.rs
tests/target/match.rs

index b8a52a02cc78cf823b26efbe0962e03490d4f39f..38305fc7d62e6e0a782dbb4ef7fb897d3ee15685 100644 (file)
@@ -918,16 +918,13 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
                                                                     .block_indent(context.config)),
                                                 body_budget);
 
-        let (body_str, break_line) = try_opt!(match_arm_heuristic(same_line_body.as_ref()
-                                                                                .map(|x| &x[..]),
-                                                                  next_line_body.as_ref()
-                                                                                .map(|x| &x[..])));
-
-        let spacer = if break_line {
-            format!("\n{}",
-                    offset.block_indent(context.config).to_string(context.config))
-        } else {
-            " ".to_owned()
+        let body_str = try_opt!(match_arm_heuristic(same_line_body.as_ref().map(|x| &x[..]),
+                                                    next_line_body.as_ref().map(|x| &x[..])));
+
+        let spacer = match same_line_body {
+            Some(ref body) if body == body_str => " ".to_owned(),
+            _ => format!("\n{}",
+                         offset.block_indent(context.config).to_string(context.config)),
         };
 
         Some(format!("{}{} =>{}{},",
@@ -939,17 +936,14 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
 }
 
 // Takes two possible rewrites for the match arm body and chooses the "nicest".
-// Bool marks break line or no.
-fn match_arm_heuristic<'a>(former: Option<&'a str>,
-                           latter: Option<&'a str>)
-                           -> Option<(&'a str, bool)> {
+fn match_arm_heuristic<'a>(former: Option<&'a str>, latter: Option<&'a str>) -> Option<&'a str> {
     match (former, latter) {
-        (Some(f), None) => Some((f, false)),
+        (f @ Some(..), None) => f,
         (Some(f), Some(l)) if f.chars().filter(|&c| c == '\n').count() <=
                               l.chars().filter(|&c| c == '\n').count() => {
-            Some((f, false))
+            Some(f)
         }
-        (_, l) => l.map(|s| (s, true)),
+        (_, l) => l,
     }
 }
 
index 9fc32ef1904c93a1a0b5db540f646b0848963da2..49e66e4c0df32d0be53d4d335037afbed5419443 100644 (file)
@@ -209,3 +209,15 @@ fn issue355() {
                                                                               dddddddddd),
     }
 }
+
+fn issue280() {
+    {
+        match x {
+            CompressionMode::DiscardNewline | CompressionMode::CompressWhitespaceNewline => ch ==
+                                                                                            '\n',
+            ast::ItemConst(ref typ, ref expr) => self.process_static_or_const_item(item,
+                                                                                   &typ,
+                                                                                   &expr),
+        }
+    }
+}
index d7227a0da59cc1be76012d5fb70d065640cd026a..6e330505a9b8af75dd8adba46ec380034284d4d3 100644 (file)
@@ -199,3 +199,14 @@ fn issue355() {
                                                                                 dddddddddd),
     }
 }
+
+fn issue280() {
+    {
+        match x {
+            CompressionMode::DiscardNewline | CompressionMode::CompressWhitespaceNewline =>
+                ch == '\n',
+            ast::ItemConst(ref typ, ref expr) =>
+                self.process_static_or_const_item(item, &typ, &expr),
+        }
+    }
+}