]> git.lizzy.rs Git - rust.git/commitdiff
Combine condition and body of control flow
authortopecongiro <seuchida@gmail.com>
Sat, 17 Jun 2017 12:11:55 +0000 (21:11 +0900)
committertopecongiro <seuchida@gmail.com>
Sun, 18 Jun 2017 05:27:58 +0000 (14:27 +0900)
If the condition of control flow expressions ends with closing parens and alike,
put the opening bracket of the body on the same line with closing parens.

src/chains.rs
src/expr.rs
src/utils.rs

index 9b27c92c270a6306d7e0bca1117ca9b7324bf32d..1729505d63b616b334b09b79fa01b634a2cd4946 100644 (file)
@@ -78,7 +78,7 @@
 
 use Shape;
 use rewrite::{Rewrite, RewriteContext};
-use utils::{wrap_str, first_line_width, last_line_width, mk_sp};
+use utils::{wrap_str, first_line_width, last_line_width, mk_sp, last_line_extendable};
 use expr::rewrite_call;
 use config::IndentStyle;
 use macros::convert_try_mac;
@@ -322,12 +322,7 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
 }
 
 fn is_extendable_parent(context: &RewriteContext, parent_str: &str) -> bool {
-    context.config.chain_indent() == IndentStyle::Block &&
-        parent_str.lines().last().map_or(false, |s| {
-            s.trim()
-                .chars()
-                .all(|c| c == ')' || c == ']' || c == '}' || c == '?')
-        })
+    context.config.chain_indent() == IndentStyle::Block && last_line_extendable(parent_str)
 }
 
 // True if the chain is only `?`s.
index b69e8bbd42eaf4b6b5472b7322c494098429139f..c299ffadad5b874c9e998c92cd20a3df4c3a579b 100644 (file)
@@ -21,7 +21,7 @@
 use string::{StringFormat, rewrite_string};
 use utils::{extra_offset, last_line_width, wrap_str, binary_search, first_line_width,
             semicolon_for_stmt, trimmed_last_line_width, left_most_sub_expr, stmt_expr,
-            colon_spaces, contains_skip, mk_sp};
+            colon_spaces, contains_skip, mk_sp, last_line_extendable};
 use visitor::FmtVisitor;
 use config::{Config, IndentStyle, MultilineStyle, ControlBraceStyle, Style};
 use comment::{FindUncommented, rewrite_comment, contains_comment, recover_comment_removed};
@@ -1145,7 +1145,8 @@ fn rewrite_cond(
         };
 
         let force_newline_brace = context.config.control_style() == Style::Rfc &&
-            pat_expr_string.contains('\n');
+            pat_expr_string.contains('\n') &&
+            !last_line_extendable(&pat_expr_string);
 
         // Try to format if-else on single line.
         if self.allow_single_line && context.config.single_line_if_else_max_width() > 0 {
index 9c3d023c2ed88184109186ffbb126e90aa1af332..1be83d01babacad7726227d67aad20095ae1afb5 100644 (file)
@@ -108,6 +108,15 @@ pub fn trimmed_last_line_width(s: &str) -> usize {
     }
 }
 
+#[inline]
+pub fn last_line_extendable(s: &str) -> bool {
+    s.lines().last().map_or(false, |s| {
+        s.trim()
+            .chars()
+            .all(|c| c == ')' || c == ']' || c == '}' || c == '?')
+    })
+}
+
 #[inline]
 fn is_skip(meta_item: &MetaItem) -> bool {
     match meta_item.node {