]> git.lizzy.rs Git - rust.git/blobdiff - src/expr.rs
Do not combine short parent and comment
[rust.git] / src / expr.rs
index c4675cbcdf7ebd0a1659ed470a0322d2dd78cd96..72cf618dca546fc7c0213013d2340ec4dd989161 100644 (file)
@@ -39,9 +39,9 @@
 use string::{rewrite_string, StringFormat};
 use types::{can_be_overflowed_type, rewrite_path, PathContext};
 use utils::{
-    colon_spaces, contains_skip, count_newlines, first_line_width, inner_attributes,
-    last_line_extendable, last_line_width, mk_sp, outer_attributes, ptr_vec_to_ref_vec,
-    semicolon_for_stmt, wrap_str,
+    colon_spaces, contains_skip, count_newlines, first_line_ends_with, first_line_width,
+    inner_attributes, last_line_extendable, last_line_width, mk_sp, outer_attributes,
+    ptr_vec_to_ref_vec, semicolon_for_stmt, wrap_str,
 };
 use vertical::rewrite_with_alignment;
 use visitor::FmtVisitor;
@@ -345,8 +345,38 @@ fn needs_space_after_range(rhs: &ast::Expr) -> bool {
         }
         // FIXME(#2743)
         ast::ExprKind::ObsoleteInPlace(..) => unimplemented!(),
-        // FIXME(topecongiro) Format async block.
-        ast::ExprKind::Async(..) => None,
+        ast::ExprKind::Async(capture_by, _node_id, ref block) => {
+            let mover = if capture_by == ast::CaptureBy::Value {
+                "move "
+            } else {
+                ""
+            };
+            if let rw @ Some(_) = rewrite_single_line_block(
+                context,
+                format!("{}{}", "async ", mover).as_str(),
+                block,
+                Some(&expr.attrs),
+                None,
+                shape,
+            ) {
+                rw
+            } else {
+                // 6 = `async `
+                let budget = shape.width.saturating_sub(6);
+                Some(format!(
+                    "{}{}{}",
+                    "async ",
+                    mover,
+                    rewrite_block(
+                        block,
+                        Some(&expr.attrs),
+                        None,
+                        context,
+                        Shape::legacy(budget, shape.indent)
+                    )?
+                ))
+            }
+        }
     };
 
     expr_rw
@@ -1212,8 +1242,7 @@ fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Opt
                             new_indent.to_string(context.config),
                             line.trim_left()
                         )
-                    })
-                    .collect::<Vec<_>>()
+                    }).collect::<Vec<_>>()
                     .join("\n")
                     .trim_left(),
             );
@@ -1229,7 +1258,6 @@ fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Opt
     rewrite_string(
         str_lit,
         &StringFormat::new(shape.visual_indent(0), context.config),
-        None,
     )
 }
 
@@ -1653,7 +1681,7 @@ pub fn rewrite_field(
     if !attrs_str.is_empty() {
         attrs_str.push_str(&shape.indent.to_string_with_newline(context.config));
     };
-    let name = &field.ident.name.to_string();
+    let name = context.snippet(field.ident.span);
     if field.is_shorthand {
         Some(attrs_str + &name)
     } else {
@@ -1954,6 +1982,9 @@ pub fn prefer_next_line(orig_rhs: &str, next_line_rhs: &str, rhs_tactics: RhsTac
     rhs_tactics == RhsTactics::ForceNextLineWithoutIndent
         || !next_line_rhs.contains('\n')
         || count_newlines(orig_rhs) > count_newlines(next_line_rhs) + 1
+        || first_line_ends_with(orig_rhs, '(') && !first_line_ends_with(next_line_rhs, '(')
+        || first_line_ends_with(orig_rhs, '{') && !first_line_ends_with(next_line_rhs, '{')
+        || first_line_ends_with(orig_rhs, '[') && !first_line_ends_with(next_line_rhs, '[')
 }
 
 fn rewrite_expr_addrof(