]> git.lizzy.rs Git - rust.git/commitdiff
Avoid early line breaks in match condition
authorSeiichi Uchida <seuchida@gmail.com>
Mon, 31 Jul 2017 07:24:07 +0000 (16:24 +0900)
committerSeiichi Uchida <seuchida@gmail.com>
Mon, 31 Jul 2017 07:24:07 +0000 (16:24 +0900)
src/expr.rs

index cf2bc33724790765fb8dd86de24311aa5c3f96ce..0a1a4a5831613ed6984878c30cac42140dbb6f2a 100644 (file)
@@ -1531,17 +1531,25 @@ fn rewrite_match(
         return None;
     }
 
-    // 6 = `match `, 2 = ` {`
+    // Do not take the rhs overhead from the upper expressions into account
+    // when rewriting match condition.
+    let new_width = try_opt!(context.config.max_width().checked_sub(shape.used_width()));
+    let cond_shape = Shape {
+        width: new_width,
+        ..shape
+    };
+    // 6 = `match `
     let cond_shape = match context.config.control_style() {
-        Style::Legacy => try_opt!(shape.shrink_left(6).and_then(|s| s.sub_width(2))),
-        Style::Rfc => try_opt!(shape.offset_left(6).and_then(|s| s.sub_width(2))),
+        Style::Legacy => try_opt!(cond_shape.shrink_left(6)),
+        Style::Rfc => try_opt!(cond_shape.offset_left(6)),
     };
     let cond_str = try_opt!(cond.rewrite(context, cond_shape));
     let alt_block_sep = String::from("\n") + &shape.indent.block_only().to_string(context.config);
     let block_sep = match context.config.control_brace_style() {
         ControlBraceStyle::AlwaysNextLine => &alt_block_sep,
         _ if last_line_extendable(&cond_str) => " ",
-        _ if cond_str.contains('\n') => &alt_block_sep,
+        // 2 = ` {`
+        _ if cond_str.contains('\n') || cond_str.len() + 2 > cond_shape.width => &alt_block_sep,
         _ => " ",
     };