]> git.lizzy.rs Git - rust.git/commitdiff
Wrap closure with a single control flow expr with multi line condition
authortopecongiro <seuchida@gmail.com>
Thu, 15 Jun 2017 14:27:53 +0000 (23:27 +0900)
committertopecongiro <seuchida@gmail.com>
Thu, 15 Jun 2017 22:36:31 +0000 (07:36 +0900)
src/expr.rs

index de9ea598df76143b8f01fb202d15a7bfd15253da..61cd9ad0cfca530b2c978c96af5dfb782ad3806f 100644 (file)
@@ -2212,7 +2212,35 @@ fn rewrite_last_arg_with_overflow<'a, T>(
 where
     T: Rewrite + Spanned + ToExpr + 'a,
 {
-    let rewrite = last_arg.rewrite(context, shape);
+    let rewrite = if let Some(expr) = last_arg.to_expr() {
+        match expr.node {
+            // When overflowing the closure which consists of a single control flow expression,
+            // force to use block if its condition uses multi line.
+            ast::ExprKind::Closure(capture, ref fn_decl, ref body, _) => {
+                if rewrite_cond(context, body, shape).map_or(false, |cond| cond.contains('\n')) {
+                    rewrite_closure_fn_decl(capture, fn_decl, body, expr.span, context, shape)
+                        .and_then(|(prefix, extra_offset)| {
+                            // 1 = space between `|...|` and body.
+                            shape.offset_left(extra_offset).and_then(|body_shape| {
+                                let body = match body.node {
+                                    ast::ExprKind::Block(ref block) => {
+                                        stmt_expr(&block.stmts[0]).unwrap()
+                                    }
+                                    _ => body,
+                                };
+                                rewrite_closure_with_block(context, body_shape, &prefix, body)
+                            })
+                        })
+                        .or_else(|| expr.rewrite(context, shape))
+                } else {
+                    expr.rewrite(context, shape)
+                }
+            }
+            _ => expr.rewrite(context, shape),
+        }
+    } else {
+        last_arg.rewrite(context, shape)
+    };
     let orig_last = last_item.item.clone();
 
     if let Some(rewrite) = rewrite {