From a83b1586cb91c763182c86a0095a50aba841cc15 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 28 Dec 2024 20:20:04 -0800 Subject: [PATCH] Normalize Stmt::Expr(Expr::Macro) to Stmt::Macro --- tests/common/visit.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/common/visit.rs b/tests/common/visit.rs index df2e150187..7330a5727e 100644 --- a/tests/common/visit.rs +++ b/tests/common/visit.rs @@ -1,6 +1,6 @@ use std::mem; use syn::visit_mut::{self, VisitMut}; -use syn::{Expr, File, Generics, LifetimeParam, TypeParam}; +use syn::{Expr, File, Generics, LifetimeParam, MacroDelimiter, Stmt, StmtMacro, TypeParam}; pub struct FlattenParens; @@ -41,6 +41,27 @@ impl VisitMut for AsIfPrinted { visit_mut::visit_lifetime_param_mut(self, param); } + fn visit_stmt_mut(&mut self, stmt: &mut Stmt) { + if let Stmt::Expr(expr, semi) = stmt { + if let Expr::Macro(e) = expr { + if match e.mac.delimiter { + MacroDelimiter::Brace(_) => true, + MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => semi.is_some(), + } { + let Expr::Macro(expr) = mem::replace(expr, Expr::PLACEHOLDER) else { + unreachable!(); + }; + *stmt = Stmt::Macro(StmtMacro { + attrs: expr.attrs, + mac: expr.mac, + semi_token: *semi, + }); + } + } + } + visit_mut::visit_stmt_mut(self, stmt); + } + fn visit_type_param_mut(&mut self, param: &mut TypeParam) { if param.bounds.is_empty() { param.colon_token = None;