]> git.lizzy.rs Git - rust.git/commitdiff
Put guard on newline if it exceeds max width
authortopecongiro <seuchida@gmail.com>
Mon, 12 Jun 2017 08:25:38 +0000 (17:25 +0900)
committertopecongiro <seuchida@gmail.com>
Mon, 12 Jun 2017 08:25:38 +0000 (17:25 +0900)
src/expr.rs
tests/target/configs-control_style-rfc.rs
tests/target/match.rs

index e6889991b07e91747d4d1478d3caa7ae4913d650..06993c729b7592649b6e6756a82ceb43a37d2243 100644 (file)
@@ -1495,28 +1495,22 @@ fn rewrite_guard(context: &RewriteContext,
     if let Some(ref guard) = *guard {
         // First try to fit the guard string on the same line as the pattern.
         // 4 = ` if `, 5 = ` => {`
-        let overhead = pattern_width + 4 + 5;
-        if overhead < shape.width {
-            let cond_shape = shape
-                .shrink_left(pattern_width + 4)
-                .unwrap()
-                .sub_width(5)
-                .unwrap();
-            let cond_str = guard.rewrite(context, cond_shape);
-            if let Some(cond_str) = cond_str {
+        if let Some(cond_shape) = shape
+               .shrink_left(pattern_width + 4)
+               .and_then(|s| s.sub_width(5)) {
+            if let Some(cond_str) = guard
+                   .rewrite(context, cond_shape)
+                   .and_then(|s| s.rewrite(context, cond_shape)) {
                 return Some(format!(" if {}", cond_str));
             }
         }
 
         // Not enough space to put the guard after the pattern, try a newline.
-        let overhead = shape.indent.block_indent(context.config).width() + 4 + 5;
-        if overhead < shape.width {
-            let cond_str = guard.rewrite(context,
-                                         Shape::legacy(shape.width - overhead,
-                                                       // 3 == `if `
-                                                       shape.indent.block_indent(context.config) +
-                                                       3));
-            if let Some(cond_str) = cond_str {
+        // 3 == `if `
+        if let Some(cond_shape) = Shape::indented(shape.indent.block_indent(context.config) + 3,
+                                                  context.config)
+               .sub_width(3) {
+            if let Some(cond_str) = guard.rewrite(context, cond_shape) {
                 return Some(format!("\n{}if {}",
                                     shape
                                         .indent
index a7213c34dfb770eab1d6bfe5295985fa1a86a657..43a10e92339d185da0e3b8cdc7189a3b837e17a4 100644 (file)
@@ -21,3 +21,20 @@ fn main() {
         }
     }
 }
+
+fn issue1656() {
+    {
+        {
+            match rewrite {
+                Some(ref body_str)
+                    if (!body_str.contains('\n') && body_str.len() <= arm_shape.width) ||
+                           !context.config.wrap_match_arms() ||
+                           (extend && first_line_width(body_str) <= arm_shape.width) ||
+                           is_block => {
+                    return None;
+                }
+                _ => {}
+            }
+        }
+    }
+}
index 6acab043e67565d00b6d9301e9652c4b47b2c669..45f62ca68454f1e8fd6d463626ed966ab3a28b26 100644 (file)
@@ -306,8 +306,8 @@ fn guards() {
                                                                       barrrrrrrrrrrr => {}
         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
             if fooooooooooooooooooooo &&
-               (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ||
-                cccccccccccccccccccccccccccccccccccccccc) => {}
+               (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb || cccccccccccccccccccccccccccccccccccccccc) => {
+        }
     }
 }