Skip to content

Conversation

@krobelus
Copy link
Contributor

@krobelus krobelus commented Oct 24, 2025

The references.excludeTests setting fails to exclude references from test code in various scenarios.
For example when using textDocument/references on "foo" in

pub fn foo() {}
#[cfg(test)]
mod tests {
	use crate::foo; // Should be excluded
}

same when the test module lives in a different file:

#[cfg(test)]
mod tests;

Try to fix that for most real-world cases.

TODO: the unit test does not actually test this because whereas
full rust-analyzer finds references in code that's disabled due to
#[cfg(test)] directives, the test logic doesn't; "ModuleData::children"
is always empty. Would appreciate help with that.

changelog fix

Closes #18573

It's even inlined though that probably only makes a difference in
cross-crate calls.
For the next commit.

While at it, handle #[cfg(all(test,foo))].  Also take the attribute
by reference since we don't need to consume it.  Not sure if that's
the convention here.
The references.excludeTests setting fails to exclude references
from test code in various scenarios. For example when using
textDocument/references on "foo" in

	pub fn foo() {}
	#[cfg(test)]
	mod tests {
		use crate::foo; // Should be excluded
	}

same when the test module lives in a different file:

	#[cfg(test)]
	mod tests;

Try to fix that for most real-world cases.

TODO: the unit test does not actually test this because whereas
full rust-analyzer finds references in code that's disabled due to
#[cfg(test)] directives, the test logic doesn't; "ModuleData::children"
is always empty. Would appreciate help with that.

changelog fix

Closes rust-lang#18573
Comment on lines +1430 to +1431
// N.B. possible false negatives here.
Invalid | Atom(CfgAtom::KeyValue { .. }) | Any(_) | Not(_) => false,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something similar to CfgExpr::fold can be used for more complex cases, e.g.:

diff --git a/crates/ide-db/src/search.rs b/crates/ide-db/src/search.rs
index 41a2ea8fe0..ad3af99fb9 100644
--- a/crates/ide-db/src/search.rs
+++ b/crates/ide-db/src/search.rs
@@ -1421,17 +1421,26 @@
 
 /// Returns true if the given attributes enable code only in test configurations.
 pub fn has_cfg_test(attrs: &AttrsWithOwner) -> bool {
-    fn is_cfg_test(cfg_expr: &CfgExpr) -> bool {
+    fn is_cfg_test(cfg_expr: &CfgExpr) -> Option<bool> {
         use CfgExpr::*;
         use cfg::CfgAtom;
         match cfg_expr {
-            Atom(CfgAtom::Flag(flag)) => *flag == sym::test,
-            All(exprs) => exprs.iter().any(is_cfg_test),
-            // N.B. possible false negatives here.
-            Invalid | Atom(CfgAtom::KeyValue { .. }) | Any(_) | Not(_) => false,
+            Invalid => None,
+            Atom(CfgAtom::Flag(flag)) => Some(*flag == sym::test),
+            Atom(CfgAtom::KeyValue { .. }) => Some(false),
+            All(exprs) => {
+                exprs.iter().try_fold(false, |acc, expr| is_cfg_test(expr).map(|res| acc || res))
+            }
+            Any(exprs) => {
+                exprs.iter().try_fold(true, |acc, expr| is_cfg_test(expr).map(|res| acc && res))
+            }
+            Not(expr) => is_cfg_test(expr).map(|is_test| !is_test),
         }
     }
-    attrs.cfgs().any(|cfg_expr| is_cfg_test(&cfg_expr))
+    attrs
+        .cfgs()
+        .try_fold(false, |acc, cfg_expr| is_cfg_test(&cfg_expr).map(|is_test| acc || is_test))
+        .unwrap_or(false)
 }
 
 /// Returns true if this reference's enclosing module is declared conditional on `cfg(test)`.

I noticed that expressions annotated with #[cfg(test)] are not handled by this PR, and it seems that the approach used for other ast types does not work for ast::Expr. Does the ast::HasAttrs trait correspond to the types which can be annotated with #[cfg(test)]? If so, maybe the implementation can be based around that. It might be unnecessary to handle some of these types, since I don't know why anyone would put #[cfg(test)] on something like an expression.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rust-analyzer.references.excludeTests should exclude #[cfg(test)]

2 participants