-
Notifications
You must be signed in to change notification settings - Fork 1.9k
references.excludeTests to exclude #[cfg(test)]-gated refs #20901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
references.excludeTests to exclude #[cfg(test)]-gated refs #20901
Conversation
It's even inlined though that probably only makes a difference in cross-crate calls.
Helps a following commit.
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
| // N.B. possible false negatives here. | ||
| Invalid | Atom(CfgAtom::KeyValue { .. }) | Any(_) | Not(_) => false, |
There was a problem hiding this comment.
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.
The references.excludeTests setting fails to exclude references from test code in various scenarios.
For example when using textDocument/references on "foo" in
same when the test module lives in a different file:
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