]> git.lizzy.rs Git - rust.git/blobdiff - src/matches.rs
Placate tidy in submodule.
[rust.git] / src / matches.rs
index 7b691bf61006d5a0f67d04d9fdaf1636fd0df9ec..85d9c5d2b9bbf4f3e80ec3371cc733d9dea157ea 100644 (file)
@@ -163,17 +163,15 @@ fn arm_comma(config: &Config, body: &ast::Expr, is_last: bool) -> &'static str {
 fn collect_beginning_verts(
     context: &RewriteContext<'_>,
     arms: &[ast::Arm],
-    span: Span,
 ) -> Vec<Option<BytePos>> {
-    let mut beginning_verts = Vec::with_capacity(arms.len());
-    let mut lo = context.snippet_provider.span_after(span, "{");
-    for arm in arms {
-        let hi = arm.pat.span.lo();
-        let missing_span = mk_sp(lo, hi);
-        beginning_verts.push(context.snippet_provider.opt_span_before(missing_span, "|"));
-        lo = arm.span().hi();
-    }
-    beginning_verts
+    arms.iter()
+        .map(|a| {
+            context
+                .snippet(a.pat.span)
+                .starts_with('|')
+                .then(|| a.pat.span().lo())
+        })
+        .collect()
 }
 
 fn rewrite_match_arms(
@@ -191,7 +189,7 @@ fn rewrite_match_arms(
     let is_last_iter = repeat(false)
         .take(arm_len.saturating_sub(1))
         .chain(repeat(true));
-    let beginning_verts = collect_beginning_verts(context, arms, span);
+    let beginning_verts = collect_beginning_verts(context, arms);
     let items = itemize_list(
         context.snippet_provider,
         arms.iter()
@@ -284,6 +282,15 @@ fn rewrite_match_arm(
     )
 }
 
+fn stmt_is_expr_mac(stmt: &ast::Stmt) -> bool {
+    if let ast::StmtKind::Expr(expr) = &stmt.kind {
+        if let ast::ExprKind::MacCall(_) = &expr.kind {
+            return true;
+        }
+    }
+    false
+}
+
 fn block_can_be_flattened<'a>(
     context: &RewriteContext<'_>,
     expr: &'a ast::Expr,
@@ -292,7 +299,8 @@ fn block_can_be_flattened<'a>(
         ast::ExprKind::Block(ref block, _)
             if !is_unsafe_block(block)
                 && !context.inside_macro()
-                && is_simple_block(context, block, Some(&expr.attrs)) =>
+                && is_simple_block(context, block, Some(&expr.attrs))
+                && !stmt_is_expr_mac(&block.stmts[0]) =>
         {
             Some(&*block)
         }
@@ -311,10 +319,14 @@ fn flatten_arm_body<'a>(
     let can_extend =
         |expr| !context.config.force_multiline_blocks() && can_flatten_block_around_this(expr);
 
-    if let Some(ref block) = block_can_be_flattened(context, body) {
+    if let Some(block) = block_can_be_flattened(context, body) {
         if let ast::StmtKind::Expr(ref expr) = block.stmts[0].kind {
             if let ast::ExprKind::Block(..) = expr.kind {
-                flatten_arm_body(context, expr, None)
+                if expr.attrs.is_empty() {
+                    flatten_arm_body(context, expr, None)
+                } else {
+                    (true, body)
+                }
             } else {
                 let cond_becomes_muti_line = opt_shape
                     .and_then(|shape| rewrite_cond(context, expr, shape))
@@ -385,7 +397,7 @@ fn rewrite_match_body(
         if comment_str.is_empty() {
             String::new()
         } else {
-            rewrite_comment(comment_str, false, shape, &context.config)?
+            rewrite_comment(comment_str, false, shape, context.config)?
         }
     };
 
@@ -400,7 +412,8 @@ fn rewrite_match_body(
                 result.push_str(&arrow_comment);
             }
             result.push_str(&nested_indent_str);
-            result.push_str(&body_str);
+            result.push_str(body_str);
+            result.push_str(comma);
             return Some(result);
         }
 
@@ -442,7 +455,7 @@ fn rewrite_match_body(
             result.push_str(&arrow_comment);
         }
         result.push_str(&block_sep);
-        result.push_str(&body_str);
+        result.push_str(body_str);
         result.push_str(&body_suffix);
         Some(result)
     };