From 657a72142599379447f8477d71fb759e181a33a4 Mon Sep 17 00:00:00 2001 From: Chayim Refael Friedman Date: Wed, 17 Jun 2026 13:02:12 +0300 Subject: [PATCH] Check for #[cfg]s in tail expression macros --- crates/hir-def/src/expr_store/lower.rs | 8 +++--- crates/hir-def/src/expr_store/tests/body.rs | 28 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/crates/hir-def/src/expr_store/lower.rs b/crates/hir-def/src/expr_store/lower.rs index bbc3f5333a14..a69755baf632 100644 --- a/crates/hir-def/src/expr_store/lower.rs +++ b/crates/hir-def/src/expr_store/lower.rs @@ -2404,6 +2404,10 @@ impl<'db> ExprCollector<'db> { statements: &mut Vec, mac: ast::MacroExpr, ) -> Option { + if !self.check_cfg(&ast::Expr::MacroExpr(mac.clone())) { + return None; + } + let mac_call = mac.macro_call()?; let syntax_ptr = AstPtr::new(&ast::Expr::from(mac)); let macro_ptr = AstPtr::new(&mac_call); @@ -2447,10 +2451,6 @@ impl<'db> ExprCollector<'db> { } ast::Stmt::ExprStmt(stmt) => { let expr = stmt.expr(); - match &expr { - Some(expr) if !self.check_cfg(expr) => return, - _ => (), - } let has_semi = stmt.semicolon_token().is_some(); // Note that macro could be expanded to multiple statements if let Some(ast::Expr::MacroExpr(mac)) = expr { diff --git a/crates/hir-def/src/expr_store/tests/body.rs b/crates/hir-def/src/expr_store/tests/body.rs index e97718ca22c3..da412a620d0a 100644 --- a/crates/hir-def/src/expr_store/tests/body.rs +++ b/crates/hir-def/src/expr_store/tests/body.rs @@ -688,3 +688,31 @@ fn foo() { }"#]], ); } + +#[test] +fn foo() { + pretty_print( + r#" +macro_rules! foo { + () => { + 1 + }; +} + +fn foo() -> i64 { + #[cfg(true)] + { + 5 + } + #[cfg(false)] + foo!() +} + "#, + expect![[r#" + fn foo() { + { + 5 + } + }"#]], + ); +}