Skip to content

Commit eff3734

Browse files
committed
Auto merge of #13913 - Urgau:check-cfg-lints-sub-config, r=<try>
Add special `check-cfg` lint config for the `unexpected_cfgs` lint This PR adds a special `check-cfg` lint config for the `unexpected_cfgs` lint, as it was decided by T-cargo (in today's meeting). The goal of this lint config is to provide a simple and cost-less alternative to the build-script `cargo::rustc-check-cfg` instruction. ```toml [lints.rust] unexpected_cfgs = { check-cfg = ["cfg(foo, values(\"bar\"))"] } ``` Regarding the implementation, everything is as straight forward as possible, nothing over-engineered; I added the possibility to omit the `level` field if a sub-config is specified instead, since it seems useful and was easy to implement. There are many small-ish commit, I recommend reviewing them independently. r? `@epage` (or `@weihanglo` maybe)
2 parents fc13634 + 0b5be58 commit eff3734

File tree

5 files changed

+378
-5
lines changed

5 files changed

+378
-5
lines changed

crates/cargo-util-schemas/src/manifest/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,13 @@ impl TomlLint {
15031503
Self::Config(config) => config.priority,
15041504
}
15051505
}
1506+
1507+
pub fn config(&self) -> Option<&toml::Table> {
1508+
match self {
1509+
Self::Level(_) => None,
1510+
Self::Config(config) => Some(&config.config),
1511+
}
1512+
}
15061513
}
15071514

15081515
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -1511,6 +1518,8 @@ pub struct TomlLintConfig {
15111518
pub level: TomlLintLevel,
15121519
#[serde(default)]
15131520
pub priority: i8,
1521+
#[serde(flatten)]
1522+
pub config: toml::Table,
15141523
}
15151524

15161525
#[derive(Serialize, Deserialize, Debug, Copy, Clone, Eq, PartialEq)]

src/cargo/core/compiler/mod.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,12 +1353,37 @@ fn check_cfg_args(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> Vec<OsStri
13531353
// Cargo and docs.rs than rustc and docs.rs. In particular, all users of docs.rs use
13541354
// Cargo, but not all users of rustc (like Rust-for-Linux) use docs.rs.
13551355

1356-
vec![
1356+
let mut args = vec![
13571357
OsString::from("--check-cfg"),
13581358
OsString::from("cfg(docsrs)"),
13591359
OsString::from("--check-cfg"),
13601360
arg_feature,
1361-
]
1361+
];
1362+
1363+
// Also include the custom arguments specified in `[lints.rust.unexpected_cfgs.check_cfg]`
1364+
if let Ok(Some(lints)) = unit.pkg.manifest().resolved_toml().resolved_lints() {
1365+
if let Some(rust_lints) = lints.get("rust") {
1366+
if let Some(unexpected_cfgs) = rust_lints.get("unexpected_cfgs") {
1367+
if let Some(config) = unexpected_cfgs.config() {
1368+
if let Some(check_cfg) = config.get("check-cfg") {
1369+
if let Ok(check_cfgs) =
1370+
toml::Value::try_into::<Vec<String>>(check_cfg.clone())
1371+
{
1372+
for check_cfg in check_cfgs {
1373+
args.push(OsString::from("--check-cfg"));
1374+
args.push(OsString::from(check_cfg));
1375+
}
1376+
// warn (if wise) about `check-cfg` not being a list-of-string
1377+
} else if unit.show_warnings(&build_runner.bcx.gctx) {
1378+
let _ = build_runner.bcx.gctx.shell().warn("`lints.rust.unexpected_cfgs.check-cfg` must be a list of string");
1379+
}
1380+
}
1381+
}
1382+
}
1383+
}
1384+
}
1385+
1386+
args
13621387
} else {
13631388
Vec::new()
13641389
}

src/cargo/util/toml/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2265,7 +2265,7 @@ supported tools: {}",
22652265
if tool == "cargo" && !gctx.cli_unstable().cargo_lints {
22662266
warn_for_cargo_lint_feature(gctx, warnings);
22672267
}
2268-
for name in lints.keys() {
2268+
for (name, config) in lints {
22692269
if let Some((prefix, suffix)) = name.split_once("::") {
22702270
if tool == prefix {
22712271
anyhow::bail!(
@@ -2278,6 +2278,19 @@ supported tools: {}",
22782278
} else {
22792279
anyhow::bail!("`lints.{tool}.{name}` is not a valid lint name")
22802280
}
2281+
} else if let Some(config) = config.config() {
2282+
for config_name in config.keys() {
2283+
// manually report unused manifest key warning since we collect all the "extra"
2284+
// keys and values inside the config table
2285+
//
2286+
// except for `rust.unexpected_cfgs.check-cfg` which is used by rustc/rustdoc
2287+
if !(tool == "rust" && name == "unexpected_cfgs" && config_name == "check-cfg")
2288+
{
2289+
let message =
2290+
format!("unused manifest key: `lints.{tool}.{name}.{config_name}`");
2291+
warnings.push(message);
2292+
}
2293+
}
22812294
}
22822295
}
22832296
}

0 commit comments

Comments
 (0)