]> git.lizzy.rs Git - rust.git/blobdiff - src/closures.rs
discard trailing blank comments
[rust.git] / src / closures.rs
index 5f1102a36fca875ed6d2685cffafc376e88dcc5d..1a4a6e44fdfba07db0177d3fffced668c5c977db 100644 (file)
@@ -31,9 +31,9 @@
 //     statement without needing a semi-colon), then adding or removing braces
 //     can change whether it is treated as an expression or statement.
 
-// FIXME(topecongiro) Format async closures (#2813).
 pub fn rewrite_closure(
     capture: ast::CaptureBy,
+    asyncness: ast::IsAsync,
     movability: ast::Movability,
     fn_decl: &ast::FnDecl,
     body: &ast::Expr,
@@ -43,8 +43,9 @@ pub fn rewrite_closure(
 ) -> Option<String> {
     debug!("rewrite_closure {:?}", body);
 
-    let (prefix, extra_offset) =
-        rewrite_closure_fn_decl(capture, movability, fn_decl, body, span, context, shape)?;
+    let (prefix, extra_offset) = rewrite_closure_fn_decl(
+        capture, asyncness, movability, fn_decl, body, span, context, shape,
+    )?;
     // 1 = space between `|...|` and body.
     let body_shape = shape.offset_left(extra_offset)?;
 
@@ -117,6 +118,22 @@ fn needs_block(block: &ast::Block, prefix: &str, context: &RewriteContext) -> bo
         || prefix.contains('\n')
 }
 
+fn veto_block(e: &ast::Expr) -> bool {
+    match e.node {
+        ast::ExprKind::Call(..)
+        | ast::ExprKind::Binary(..)
+        | ast::ExprKind::Cast(..)
+        | ast::ExprKind::Type(..)
+        | ast::ExprKind::Assign(..)
+        | ast::ExprKind::AssignOp(..)
+        | ast::ExprKind::Field(..)
+        | ast::ExprKind::Index(..)
+        | ast::ExprKind::Range(..)
+        | ast::ExprKind::Try(..) => true,
+        _ => false,
+    }
+}
+
 // Rewrite closure with a single expression wrapping its body with block.
 fn rewrite_closure_with_block(
     body: &ast::Expr,
@@ -125,7 +142,7 @@ fn rewrite_closure_with_block(
     shape: Shape,
 ) -> Option<String> {
     let left_most = left_most_sub_expr(body);
-    let veto_block = left_most != body && !classify::expr_requires_semi_to_be_stmt(left_most);
+    let veto_block = veto_block(body) && !classify::expr_requires_semi_to_be_stmt(left_most);
     if veto_block {
         return None;
     }
@@ -181,8 +198,7 @@ fn allow_multi_line(expr: &ast::Expr) -> bool {
             } else {
                 Some(rw)
             }
-        })
-        .map(|rw| format!("{} {}", prefix, rw))
+        }).map(|rw| format!("{} {}", prefix, rw))
 }
 
 // Rewrite closure whose body is block.
@@ -198,6 +214,7 @@ fn rewrite_closure_block(
 // Return type is (prefix, extra_offset)
 fn rewrite_closure_fn_decl(
     capture: ast::CaptureBy,
+    asyncness: ast::IsAsync,
     movability: ast::Movability,
     fn_decl: &ast::FnDecl,
     body: &ast::Expr,
@@ -205,12 +222,12 @@ fn rewrite_closure_fn_decl(
     context: &RewriteContext,
     shape: Shape,
 ) -> Option<(String, usize)> {
+    let is_async = if asyncness.is_async() { "async " } else { "" };
     let mover = if capture == ast::CaptureBy::Value {
         "move "
     } else {
         ""
     };
-
     let immovable = if movability == ast::Movability::Static {
         "static "
     } else {
@@ -219,7 +236,7 @@ fn rewrite_closure_fn_decl(
     // 4 = "|| {".len(), which is overconservative when the closure consists of
     // a single expression.
     let nested_shape = shape
-        .shrink_left(mover.len() + immovable.len())?
+        .shrink_left(is_async.len() + mover.len() + immovable.len())?
         .sub_width(4)?;
 
     // 1 = |
@@ -253,19 +270,11 @@ fn rewrite_closure_fn_decl(
         _ => arg_shape,
     };
 
-    let fmt = ListFormatting {
-        tactic,
-        separator: ",",
-        trailing_separator: SeparatorTactic::Never,
-        separator_place: SeparatorPlace::Back,
-        shape: arg_shape,
-        ends_with_newline: false,
-        preserve_newline: true,
-        nested: false,
-        config: context.config,
-    };
+    let fmt = ListFormatting::new(arg_shape, context.config)
+        .tactic(tactic)
+        .preserve_newline(true);
     let list_str = write_list(&item_vec, &fmt)?;
-    let mut prefix = format!("{}{}|{}|", immovable, mover, list_str);
+    let mut prefix = format!("{}{}{}|{}|", is_async, immovable, mover, list_str);
 
     if !ret_str.is_empty() {
         if prefix.contains('\n') {
@@ -289,7 +298,9 @@ pub fn rewrite_last_closure(
     expr: &ast::Expr,
     shape: Shape,
 ) -> Option<String> {
-    if let ast::ExprKind::Closure(capture, _, movability, ref fn_decl, ref body, _) = expr.node {
+    if let ast::ExprKind::Closure(capture, asyncness, movability, ref fn_decl, ref body, _) =
+        expr.node
+    {
         let body = match body.node {
             ast::ExprKind::Block(ref block, _)
                 if !is_unsafe_block(block)
@@ -300,7 +311,7 @@ pub fn rewrite_last_closure(
             _ => body,
         };
         let (prefix, extra_offset) = rewrite_closure_fn_decl(
-            capture, movability, fn_decl, body, expr.span, context, shape,
+            capture, asyncness, movability, fn_decl, body, expr.span, context, shape,
         )?;
         // If the closure goes multi line before its body, do not overflow the closure.
         if prefix.contains('\n') {
@@ -356,10 +367,9 @@ pub fn args_have_many_closure<T>(args: &[&T]) -> bool
                 .map(|e| match e.node {
                     ast::ExprKind::Closure(..) => true,
                     _ => false,
-                })
-                .unwrap_or(false)
-        })
-        .count() > 1
+                }).unwrap_or(false)
+        }).count()
+        > 1
 }
 
 fn is_block_closure_forced(context: &RewriteContext, expr: &ast::Expr) -> bool {