fix: Skip cfg attributes when stripping macro input attributes
diff --git a/crates/hir-def/src/macro_expansion_tests/proc_macros.rs b/crates/hir-def/src/macro_expansion_tests/proc_macros.rs
index 5216246..6f30ca0 100644
--- a/crates/hir-def/src/macro_expansion_tests/proc_macros.rs
+++ b/crates/hir-def/src/macro_expansion_tests/proc_macros.rs
@@ -341,3 +341,22 @@
#[helper_should_be_ignored] struct Foo;"#]],
);
}
+
+#[test]
+fn attribute_macro_stripping_with_cfg() {
+ check(
+ r#"
+//- proc_macros: generate_suffixed_type
+#[cfg(all())]
+#[proc_macros::generate_suffixed_type]
+struct S;
+"#,
+ expect![[r#"
+#[cfg(all())]
+#[proc_macros::generate_suffixed_type]
+struct S;
+
+struct S;
+struct SSuffix;"#]],
+ );
+}
diff --git a/crates/hir-expand/src/cfg_process.rs b/crates/hir-expand/src/cfg_process.rs
index 227a62f..a0de365 100644
--- a/crates/hir-expand/src/cfg_process.rs
+++ b/crates/hir-expand/src/cfg_process.rs
@@ -162,25 +162,19 @@
}
}
Meta::TokenTree { path, tt } => {
- if path.segments.len() != 1
+ if path.is1("cfg") {
+ let cfg_expr = CfgExpr::parse_from_ast(
+ &mut TokenTreeChildren::new(&tt).peekable(),
+ );
+ if cfg_options().check(&cfg_expr) == Some(false) {
+ return ControlFlow::Break(ItemIsCfgedOut);
+ }
+ strip_current_attr = true;
+ } else if path.segments.len() != 1
|| !is_item_tree_filtered_attr(path.segments[0].text())
{
strip_current_attr = should_strip_attr();
}
-
- if path.segments.len() == 1 {
- let name = path.segments[0].text();
-
- if name == "cfg" {
- let cfg_expr = CfgExpr::parse_from_ast(
- &mut TokenTreeChildren::new(&tt).peekable(),
- );
- if cfg_options().check(&cfg_expr) == Some(false) {
- return ControlFlow::Break(ItemIsCfgedOut);
- }
- strip_current_attr = true;
- }
- }
}
Meta::Path { path } => {
if path.segments.len() != 1