[Rust] How do I query function and module attributes?
I wanted to filter out tests from my results, based on if they either had a #[test] attribute or #[cfg(test)].
Currently tried something similar to this but cant figure out how to properly filter this from the docs
predicate doesntWork(Module mod, Attr a, Meta m) {
a = mod.getAnAttr() and
m = a.getMeta() and
m.hasPath() and
m.getPath().toString() = "cfg" and
m.hasTokenTree()
}
Hi @drank40 👋🏻
If you just want to filter out tests, then one option may be to just configure CodeQL to ignore test files. Would that work for you? If so, could you confirm whether you're using the CodeQL CLI directly or running analyses in Actions?
:wave: @drank40
Unfortunately, we currently do not extract the token tree of attributes, so it is not possible to do this on the QL side.
You can however exclude code under a cfg(test) module or block while extracting. You can do so by setting CODEQL_EXTRACTOR_RUST_OPTION_CARGO_CFG_OVERRIDES=-test in the environment, or passing -O cargo_cfg_overrides=-test to the CLI extraction command. This and other rust extractor options are listed in this file.