From d1e124b03a93a6fa0a72ab79d73f83d96e9e0c1e Mon Sep 17 00:00:00 2001
From: topecongiro <seuchida@gmail.com>
Date: Mon, 10 Jul 2017 16:52:07 +0900
Subject: [PATCH 1/3] Fix up rewriting match arm pattern

---
 src/expr.rs | 32 +++++++++++++-------------------
 1 file changed, 13 insertions(+), 19 deletions(-)

diff --git a/src/expr.rs b/src/expr.rs
index b214a42a3db..9fc4df80cff 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -1701,19 +1701,14 @@ impl Rewrite for ast::Arm {
         };
         let pats_str = try_opt!(write_list(&items, &fmt));
 
-        let guard_shape = if pats_str.contains('\n') {
-            shape.with_max_width(context.config)
-        } else {
-            shape
-        };
-
         let guard_str = try_opt!(rewrite_guard(
             context,
             guard,
-            guard_shape,
+            shape,
             trimmed_last_line_width(&pats_str),
         ));
 
+        let pats_len = pats_str.len();
         let pats_str = format!("{}{}", pats_str, guard_str);
 
         let (mut extend, body) = match body.node {
@@ -1766,6 +1761,9 @@ impl Rewrite for ast::Arm {
                            is_block => {
                     let block_sep = match context.config.control_brace_style() {
                         ControlBraceStyle::AlwaysNextLine if is_block => alt_block_sep.as_str(),
+                        _ if guard.is_some() && pats_str.contains('\n') && is_block &&
+                            body_str != "{}" &&
+                            pats_len > context.config.tab_spaces() => alt_block_sep.as_str(),
                         _ => " ",
                     };
 
@@ -1848,33 +1846,30 @@ fn rewrite_guard(
         // First try to fit the guard string on the same line as the pattern.
         // 4 = ` if `, 5 = ` => {`
         if let Some(cond_shape) = shape
-            .shrink_left(pattern_width + 4)
+            .offset_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))
             {
-                if !cond_str.contains('\n') {
+                if !cond_str.contains('\n') || pattern_width <= context.config.tab_spaces() {
                     return Some(format!(" if {}", cond_str));
                 }
             }
         }
 
         // Not enough space to put the guard after the pattern, try a newline.
-        // 3 == `if `
-        if let Some(cond_shape) = Shape::indented(
-            shape.indent.block_indent(context.config) + 3,
-            context.config,
-        ).sub_width(3)
+        // 3 = `if `, 5 = ` => {`
+        if let Some(cond_shape) =
+            Shape::indented(shape.indent.block_indent(context.config), context.config)
+                .offset_left(3)
+                .and_then(|s| s.sub_width(5))
         {
             if let Some(cond_str) = guard.rewrite(context, cond_shape) {
                 return Some(format!(
                     "\n{}if {}",
-                    shape
-                        .indent
-                        .block_indent(context.config)
-                        .to_string(context.config),
+                    cond_shape.indent.to_string(context.config),
                     cond_str
                 ));
             }
@@ -1920,7 +1915,6 @@ fn rewrite_pat_expr(
 
     // The expression won't fit on the current line, jump to next.
     let nested_shape = shape
-        .block()
         .block_indent(context.config.tab_spaces())
         .with_max_width(context.config);
     let nested_indent_str = nested_shape.indent.to_string(context.config);

From 76228e81dea390a618b36ee46379e23c321a6676 Mon Sep 17 00:00:00 2001
From: topecongiro <seuchida@gmail.com>
Date: Mon, 10 Jul 2017 16:52:50 +0900
Subject: [PATCH 2/3] Format source codes

---
 src/expr.rs  | 9 +++++----
 src/types.rs | 3 ++-
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/src/expr.rs b/src/expr.rs
index 9fc4df80cff..cb0f83732e9 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -1714,7 +1714,7 @@ impl Rewrite for ast::Arm {
         let (mut extend, body) = match body.node {
             ast::ExprKind::Block(ref block)
                 if !is_unsafe_block(block) && is_simple_block(block, context.codemap) &&
-                       context.config.wrap_match_arms() => {
+                    context.config.wrap_match_arms() => {
                 if let ast::StmtKind::Expr(ref expr) = block.stmts[0].node {
                     (false, &**expr)
                 } else {
@@ -1756,9 +1756,10 @@ impl Rewrite for ast::Arm {
             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 => {
+                        !context.config.wrap_match_arms() ||
+                        (extend && first_line_width(body_str) <= arm_shape.width) ||
+                        is_block =>
+                {
                     let block_sep = match context.config.control_brace_style() {
                         ControlBraceStyle::AlwaysNextLine if is_block => alt_block_sep.as_str(),
                         _ if guard.is_some() && pats_str.contains('\n') && is_block &&
diff --git a/src/types.rs b/src/types.rs
index b617c433f10..6fd070291af 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -210,7 +210,8 @@ fn rewrite_segment(
         match **params {
             ast::PathParameters::AngleBracketed(ref data)
                 if !data.lifetimes.is_empty() || !data.types.is_empty() ||
-                       !data.bindings.is_empty() => {
+                    !data.bindings.is_empty() =>
+            {
                 let param_list = data.lifetimes
                     .iter()
                     .map(SegmentParam::LifeTime)

From 7cbc48ae3269eb85639c56f82302c04929a111fa Mon Sep 17 00:00:00 2001
From: topecongiro <seuchida@gmail.com>
Date: Mon, 10 Jul 2017 16:52:55 +0900
Subject: [PATCH 3/3] Update tests

---
 tests/source/long-match-arms-brace-newline.rs | 1 +
 tests/target/configs-control_style-rfc.rs     | 7 ++++---
 tests/target/long-match-arms-brace-newline.rs | 1 +
 tests/target/match.rs                         | 4 ++--
 4 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/tests/source/long-match-arms-brace-newline.rs b/tests/source/long-match-arms-brace-newline.rs
index 927ada0ffb2..bc5c9deef68 100644
--- a/tests/source/long-match-arms-brace-newline.rs
+++ b/tests/source/long-match-arms-brace-newline.rs
@@ -1,4 +1,5 @@
 // rustfmt-format_strings: true
+// rustfmt-force_format_strings: true
 // rustfmt-max_width: 80
 // rustfmt-control_brace_style: AlwaysNextLine
 
diff --git a/tests/target/configs-control_style-rfc.rs b/tests/target/configs-control_style-rfc.rs
index 20742da2195..417c33c59f8 100644
--- a/tests/target/configs-control_style-rfc.rs
+++ b/tests/target/configs-control_style-rfc.rs
@@ -28,9 +28,10 @@ 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 => {
+                        !context.config.wrap_match_arms() ||
+                        (extend && first_line_width(body_str) <= arm_shape.width) ||
+                        is_block =>
+                {
                     return None;
                 }
                 _ => {}
diff --git a/tests/target/long-match-arms-brace-newline.rs b/tests/target/long-match-arms-brace-newline.rs
index 6f4e3a8de21..2a956e5b4d0 100644
--- a/tests/target/long-match-arms-brace-newline.rs
+++ b/tests/target/long-match-arms-brace-newline.rs
@@ -1,4 +1,5 @@
 // rustfmt-format_strings: true
+// rustfmt-force_format_strings: true
 // rustfmt-max_width: 80
 // rustfmt-control_brace_style: AlwaysNextLine
 
diff --git a/tests/target/match.rs b/tests/target/match.rs
index bd6376f5153..f08c5acc9be 100644
--- a/tests/target/match.rs
+++ b/tests/target/match.rs
@@ -324,8 +324,8 @@ fn guards() {
             if foooooooooooooo && barrrrrrrrrrrr => {}
         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
             if fooooooooooooooooooooo &&
-                   (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ||
-                        cccccccccccccccccccccccccccccccccccccccc) => {}
+                (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ||
+                    cccccccccccccccccccccccccccccccccccccccc) => {}
     }
 }