Unnamed repository; edit this file 'description' to name the repository.
Diagnose inactive code in macros
But only if the source is code passed to the macro, not code inside the macro.
Chayim Refael Friedman 3 months ago
parent 43e333a · commit 759b166
-rw-r--r--crates/ide-diagnostics/src/handlers/inactive_code.rs59
1 files changed, 55 insertions, 4 deletions
diff --git a/crates/ide-diagnostics/src/handlers/inactive_code.rs b/crates/ide-diagnostics/src/handlers/inactive_code.rs
index 09f3e8bfb3..71cac6af13 100644
--- a/crates/ide-diagnostics/src/handlers/inactive_code.rs
+++ b/crates/ide-diagnostics/src/handlers/inactive_code.rs
@@ -10,10 +10,8 @@ pub(crate) fn inactive_code(
ctx: &DiagnosticsContext<'_, '_>,
d: &hir::InactiveCode,
) -> Option<Diagnostic> {
- // If there's inactive code somewhere in a macro, don't propagate to the call-site.
- if d.node.file_id.is_macro() {
- return None;
- }
+ // If there's inactive code somewhere in a macro that doesn't map to something in the call, don't propagate to the call-site.
+ d.node.map(|it| it.text_range()).original_node_file_range_rooted_opt(ctx.db())?;
let inactive = DnfExpr::new(&d.cfg).why_inactive(&d.opts);
let mut message = "code is inactive due to #[cfg] directives".to_owned();
@@ -252,4 +250,57 @@ fn foo() {}
ide_db::FileRange { file_id: file_id.file_id(&db), range: full_file_range },
);
}
+
+ #[test]
+ fn cfg_in_macro_does_not_diagnose_the_whole_call() {
+ check(
+ r#"
+macro_rules! m {
+ ($e:item) => {
+ #[cfg(false)]
+ const _: () = ();
+
+ $e
+ };
+}
+
+m! {
+ fn foo() {}
+}
+ "#,
+ );
+ }
+
+ #[test]
+ fn in_macro() {
+ check(
+ r#"
+macro_rules! m {
+ ($e:item) => {
+ $e
+ };
+}
+
+m! {
+ #[cfg(false)] fn foo() {}
+ // ^^^^^^^^^^^^^^^^^^^^^^^^^ weak: code is inactive due to #[cfg] directives: false is disabled
+}
+ "#,
+ );
+ check(
+ r#"
+macro_rules! m {
+ ($e:item) => {
+ #[cfg(false)]
+ $e
+ };
+}
+
+m! {
+ fn foo() {}
+ // ^^^^^^^^^^^ weak: code is inactive due to #[cfg] directives: false is disabled
+}
+ "#,
+ );
+ }
}