Skip to content

Commit 70598e9

Browse files
redsun82Copilot
andcommitted
Rust: Add extractor test for cfg exclusion in macro expansions
Regression test for `should_be_excluded`: items produced by a macro expansion carrying a disabled `#[cfg(...)]` must be excluded from extraction, just like disabled items written directly in a source file. Uses `cfg(any())`/`cfg(all())` so the test does not depend on crate features. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 4ee033fb-30ad-4617-9922-bc0f1088ba81
1 parent dc90b3a commit 70598e9

4 files changed

Lines changed: 37 additions & 0 deletions

File tree

rust/ql/test/extractor-tests/macro-expansion-cfg/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| cfg_macro.rs:15:1:15:15 | fn from_macro_enabled | from_macro_enabled |
2+
| cfg_macro.rs:21:1:22:26 | fn direct_enabled | direct_enabled |
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import rust
2+
import TestUtils
3+
4+
query predicate functions(Function f, string name) {
5+
toBeTested(f) and name = f.getName().getText()
6+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Regression test: `#[cfg(...)]`-disabled items produced by a macro expansion must be
2+
// excluded from extraction, just like disabled items written directly in a source file.
3+
// `any()` is always false and `all()` is always true, so this does not depend on crate features.
4+
5+
macro_rules! make_cfg_items {
6+
() => {
7+
#[cfg(any())] // always false: must be excluded
8+
pub fn from_macro_disabled() {}
9+
10+
#[cfg(all())] // always true: must be kept
11+
pub fn from_macro_enabled() {}
12+
};
13+
}
14+
15+
make_cfg_items!();
16+
17+
// Baseline: the same predicates on items written directly in the file (not via a macro).
18+
#[cfg(any())] // always false: must be excluded
19+
pub fn direct_disabled() {}
20+
21+
#[cfg(all())] // always true: must be kept
22+
pub fn direct_enabled() {}

0 commit comments

Comments
 (0)