codeql icon indicating copy to clipboard operation
codeql copied to clipboard

[Rust] How do I query function and module attributes?

Open drank40 opened this issue 2 months ago • 2 comments

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()
}

drank40 avatar Nov 07 '25 05:11 drank40

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?

mbg avatar Nov 07 '25 18:11 mbg

: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.

redsun82 avatar Nov 24 '25 09:11 redsun82