Skip to content

Commit 702fede

Browse files
author
Paolo Tranquilli
committed
Rust: use all features by default
1 parent 00c7bc1 commit 702fede

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

rust/codeql-extractor.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ options:
4444
cargo_features:
4545
title: Cargo features to turn on
4646
description: >
47-
Comma-separated list of features to turn on. If any value is `*` all features
48-
are turned on. By default only default cargo features are enabled. Can be
49-
repeated.
47+
Comma-separated list of features to turn on. By default all features are enabled.
48+
If any features are specified, then only those features are enabled. The `default`
49+
feature must be explicitly specified if only default features are desired.
50+
Can be repeated.
5051
type: array
5152
cargo_cfg_overrides:
5253
title: Cargo cfg overrides

rust/extractor/src/config.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,23 @@ impl Config {
126126
}
127127
}
128128

129+
fn cargo_features(&self) -> CargoFeatures {
130+
// '*' is to be considered deprecated but still kept in for backward compatibility
131+
if self.cargo_features.is_empty() || self.cargo_features.iter().any(|f| f == "*") {
132+
CargoFeatures::All
133+
} else {
134+
CargoFeatures::Selected {
135+
features: self
136+
.cargo_features
137+
.iter()
138+
.filter(|f| *f != "default")
139+
.cloned()
140+
.collect(),
141+
no_default_features: !self.cargo_features.iter().any(|f| f == "default"),
142+
}
143+
}
144+
}
145+
129146
pub fn to_cargo_config(&self, dir: &AbsPath) -> (CargoConfig, LoadCargoConfig) {
130147
let sysroot = self.sysroot(dir);
131148
(
@@ -156,16 +173,7 @@ impl Config {
156173
.unwrap_or_else(|| self.scratch_dir.join("target")),
157174
)
158175
.ok(),
159-
features: if self.cargo_features.is_empty() {
160-
Default::default()
161-
} else if self.cargo_features.contains(&"*".to_string()) {
162-
CargoFeatures::All
163-
} else {
164-
CargoFeatures::Selected {
165-
features: self.cargo_features.clone(),
166-
no_default_features: false,
167-
}
168-
},
176+
features: self.cargo_features(),
169177
target: self.cargo_target.clone(),
170178
cfg_overrides: to_cfg_overrides(&self.cargo_cfg_overrides),
171179
wrap_rustc_in_build_scripts: false,

rust/ql/integration-tests/options/features/functions.expected renamed to rust/ql/integration-tests/options/features/functions.none.expected

File renamed without changes.
Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22

3+
@pytest.mark.ql_test(expected=".all.expected")
34
def test_default(codeql, rust):
45
codeql.database.create()
56

@@ -8,10 +9,33 @@ def test_default(codeql, rust):
89
pytest.param(p,
910
marks=pytest.mark.ql_test(expected=f".{e}.expected"))
1011
for p, e in (
12+
("default", "none"),
1113
("foo", "foo"),
1214
("bar", "bar"),
1315
("*", "all"),
14-
("foo,bar", "all"))
16+
("foo,bar", "all"),
17+
("default,foo", "foo"),
18+
("default,bar", "bar"),
19+
)
1520
])
1621
def test_features(codeql, rust, features):
1722
codeql.database.create(extractor_option=f"cargo_features={features}")
23+
24+
@pytest.mark.parametrize("features",
25+
[
26+
pytest.param(p,
27+
marks=pytest.mark.ql_test(expected=f".{e}.expected"))
28+
for p, e in (
29+
("default", "foo"),
30+
("foo", "foo"),
31+
("bar", "bar"),
32+
("*", "all"),
33+
("foo,bar", "all"),
34+
("default,foo", "foo"),
35+
("default,bar", "all"),
36+
)
37+
])
38+
def test_features_with_deault(codeql, rust, features):
39+
with open("Cargo.toml", "a") as f:
40+
print('default = ["foo"]', file=f)
41+
codeql.database.create(extractor_option=f"cargo_features={features}")

0 commit comments

Comments
 (0)