]> git.lizzy.rs Git - rust.git/blobdiff - src/matches.rs
Update rustc-ap-* crates to 581.0.0 (#3783)
[rust.git] / src / matches.rs
index 2e2c547761685029f08709a0b080a658992fedf4..b608faded9e6ff7dcfdffefdc9cd78f0148a0f87 100644 (file)
@@ -19,7 +19,7 @@
 use crate::spanned::Spanned;
 use crate::utils::{
     contains_skip, extra_offset, first_line_width, inner_attributes, last_line_extendable, mk_sp,
-    ptr_vec_to_ref_vec, semicolon_for_expr, trimmed_last_line_width,
+    ptr_vec_to_ref_vec, semicolon_for_expr, trimmed_last_line_width, unicode_str_width,
 };
 
 /// A simple wrapper type against `ast::Arm`. Used inside `write_list()`.
@@ -111,7 +111,7 @@ pub(crate) fn rewrite_match(
             .snippet_provider
             .span_after(mk_sp(cond.span.hi(), hi), "{")
     } else {
-        inner_attrs[inner_attrs.len() - 1].span().hi()
+        inner_attrs[inner_attrs.len() - 1].span.hi()
     };
 
     if arms.is_empty() {
@@ -278,6 +278,7 @@ fn block_can_be_flattened<'a>(
     match expr.node {
         ast::ExprKind::Block(ref block, _)
             if !is_unsafe_block(block)
+                && !context.inside_macro()
                 && is_simple_block(block, Some(&expr.attrs), context.source_map) =>
         {
             Some(&*block)
@@ -394,25 +395,26 @@ fn rewrite_match_body(
         }
 
         let indent_str = shape.indent.to_string_with_newline(context.config);
-        let (body_prefix, body_suffix) = if context.config.match_arm_blocks() {
-            let comma = if context.config.match_block_trailing_comma() {
-                ","
-            } else {
-                ""
-            };
-            let semicolon = if context.config.version() == Version::One {
-                ""
-            } else {
-                if semicolon_for_expr(context, body) {
-                    ";"
+        let (body_prefix, body_suffix) =
+            if context.config.match_arm_blocks() && !context.inside_macro() {
+                let comma = if context.config.match_block_trailing_comma() {
+                    ","
                 } else {
                     ""
-                }
+                };
+                let semicolon = if context.config.version() == Version::One {
+                    ""
+                } else {
+                    if semicolon_for_expr(context, body) {
+                        ";"
+                    } else {
+                        ""
+                    }
+                };
+                ("{", format!("{}{}}}{}", semicolon, indent_str, comma))
+            } else {
+                ("", String::from(","))
             };
-            ("{", format!("{}{}}}{}", semicolon, indent_str, comma))
-        } else {
-            ("", String::from(","))
-        };
 
         let block_sep = match context.config.control_brace_style() {
             ControlBraceStyle::AlwaysNextLine => format!("{}{}", alt_block_sep, body_prefix),
@@ -450,7 +452,9 @@ fn rewrite_match_body(
 
         match rewrite {
             Some(ref body_str)
-                if is_block || (!body_str.contains('\n') && body_str.len() <= body_shape.width) =>
+                if is_block
+                    || (!body_str.contains('\n')
+                        && unicode_str_width(body_str) <= body_shape.width) =>
             {
                 return combine_orig_body(body_str);
             }
@@ -485,18 +489,10 @@ fn rewrite_match_body(
     }
 }
 
-impl Rewrite for ast::Guard {
-    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
-        match self {
-            ast::Guard::If(ref expr) => expr.rewrite(context, shape),
-        }
-    }
-}
-
 // The `if ...` guard on a match arm.
 fn rewrite_guard(
     context: &RewriteContext<'_>,
-    guard: &Option<ast::Guard>,
+    guard: &Option<ptr::P<ast::Expr>>,
     shape: Shape,
     // The amount of space used up on this line for the pattern in
     // the arm (excludes offset).
@@ -558,12 +554,10 @@ fn can_flatten_block_around_this(body: &ast::Expr) -> bool {
     match body.node {
         // We do not allow `if` to stay on the same line, since we could easily mistake
         // `pat => if cond { ... }` and `pat if cond => { ... }`.
-        ast::ExprKind::If(..) | ast::ExprKind::IfLet(..) => false,
+        ast::ExprKind::If(..) => false,
         // We do not allow collapsing a block around expression with condition
         // to avoid it being cluttered with match arm.
-        ast::ExprKind::ForLoop(..) | ast::ExprKind::While(..) | ast::ExprKind::WhileLet(..) => {
-            false
-        }
+        ast::ExprKind::ForLoop(..) | ast::ExprKind::While(..) => false,
         ast::ExprKind::Loop(..)
         | ast::ExprKind::Match(..)
         | ast::ExprKind::Block(..)
@@ -578,6 +572,7 @@ fn can_flatten_block_around_this(body: &ast::Expr) -> bool {
         | ast::ExprKind::Box(ref expr)
         | ast::ExprKind::Try(ref expr)
         | ast::ExprKind::Unary(_, ref expr)
+        | ast::ExprKind::Index(ref expr, _)
         | ast::ExprKind::Cast(ref expr, _) => can_flatten_block_around_this(expr),
         _ => false,
     }