Skip to content

Commit f77f2b7

Browse files
author
Paolo Tranquilli
committed
Rust: turn off the test cfg by default
1 parent a13c70b commit f77f2b7

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

rust/extractor/src/config.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,31 @@ fn to_cfg_override(spec: &str) -> CfgAtom {
137137
fn to_cfg_overrides(specs: &Vec<String>) -> CfgOverrides {
138138
let mut enabled_cfgs = Vec::new();
139139
let mut disabled_cfgs = Vec::new();
140+
let mut has_test_explicitly_enabled = false;
140141
for spec in specs {
141142
if spec.starts_with("-") {
142143
disabled_cfgs.push(to_cfg_override(&spec[1..]));
143144
} else {
144145
enabled_cfgs.push(to_cfg_override(spec));
146+
if spec == "test" {
147+
has_test_explicitly_enabled = true;
148+
}
145149
}
146150
}
151+
if !has_test_explicitly_enabled {
152+
disabled_cfgs.push(to_cfg_override("test"));
153+
}
147154
if let Some(global) = CfgDiff::new(enabled_cfgs, disabled_cfgs) {
148155
CfgOverrides {
149156
global,
150157
..Default::default()
151158
}
152159
} else {
153160
warn!("non-disjoint cfg overrides, ignoring: {}", specs.join(", "));
154-
CfgOverrides::default()
161+
CfgOverrides {
162+
global: CfgDiff::new(Vec::new(), vec![to_cfg_override("test")])
163+
.expect("disabling one cfg should always succeed"),
164+
..Default::default()
165+
}
155166
}
156167
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
| src/cfg.rs:1:1:2:16 | cfg_flag |
22
| src/cfg.rs:4:1:5:15 | cfg_key |
3-
| src/cfg.rs:13:1:14:15 | no_test |
3+
| src/cfg.rs:13:1:14:12 | test |
4+
| src/cfg.rs:16:1:17:24 | pointer_width_32 |

rust/ql/integration-tests/options/src/cfg.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ fn cfg_no_flag() {}
1010
#[cfg(not(cfg_key = "value"))]
1111
fn cfg_no_key() {}
1212

13-
#[cfg(not(test))]
14-
fn no_test() {}
13+
#[cfg(test)]
14+
fn test() {}
15+
16+
#[cfg(target_pointer_width = "32")]
17+
fn pointer_width_32() {}

rust/ql/integration-tests/options/test_options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_target_linux(codeql, rust):
2323
@pytest.mark.ql_test("arch_functions.ql", expected=f".{platform.system()}.expected")
2424
def test_cfg_override(codeql, rust):
2525
# currently codeql CLI has a limitation not allow to pass `=` in values via `--extractor-option`
26-
os.environ["CODEQL_EXTRACTOR_RUST_OPTION_CARGO_CFG_OVERRIDES"] = "cfg_flag,cfg_key=value,-test"
26+
os.environ["CODEQL_EXTRACTOR_RUST_OPTION_CARGO_CFG_OVERRIDES"] = "cfg_flag,cfg_key=value,-target_pointer_width=64,target_pointer_width=32,test"
2727
codeql.database.create()
2828

2929
@pytest.mark.ql_test("arch_functions.ql", expected=f".{platform.system()}.expected")

0 commit comments

Comments
 (0)