Skip to content

Commit

Permalink
Auto merge of #13913 - Urgau:check-cfg-lints-sub-config, r=<try>
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
bors committed May 15, 2024
2 parents fc13634 + 0b5be58 commit eff3734
Show file tree
Hide file tree
Showing 5 changed files with 378 additions and 5 deletions.
9 changes: 9 additions & 0 deletions crates/cargo-util-schemas/src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,13 @@ impl TomlLint {
Self::Config(config) => config.priority,
}
}

pub fn config(&self) -> Option<&toml::Table> {
match self {
Self::Level(_) => None,
Self::Config(config) => Some(&config.config),
}
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand All @@ -1511,6 +1518,8 @@ pub struct TomlLintConfig {
pub level: TomlLintLevel,
#[serde(default)]
pub priority: i8,
#[serde(flatten)]
pub config: toml::Table,
}

#[derive(Serialize, Deserialize, Debug, Copy, Clone, Eq, PartialEq)]
Expand Down
29 changes: 27 additions & 2 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1353,12 +1353,37 @@ fn check_cfg_args(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> Vec<OsStri
// Cargo and docs.rs than rustc and docs.rs. In particular, all users of docs.rs use
// Cargo, but not all users of rustc (like Rust-for-Linux) use docs.rs.

vec![
let mut args = vec![
OsString::from("--check-cfg"),
OsString::from("cfg(docsrs)"),
OsString::from("--check-cfg"),
arg_feature,
]
];

// Also include the custom arguments specified in `[lints.rust.unexpected_cfgs.check_cfg]`
if let Ok(Some(lints)) = unit.pkg.manifest().resolved_toml().resolved_lints() {
if let Some(rust_lints) = lints.get("rust") {
if let Some(unexpected_cfgs) = rust_lints.get("unexpected_cfgs") {
if let Some(config) = unexpected_cfgs.config() {
if let Some(check_cfg) = config.get("check-cfg") {
if let Ok(check_cfgs) =
toml::Value::try_into::<Vec<String>>(check_cfg.clone())
{
for check_cfg in check_cfgs {
args.push(OsString::from("--check-cfg"));
args.push(OsString::from(check_cfg));
}
// warn (if wise) about `check-cfg` not being a list-of-string
} else if unit.show_warnings(&build_runner.bcx.gctx) {
let _ = build_runner.bcx.gctx.shell().warn("`lints.rust.unexpected_cfgs.check-cfg` must be a list of string");
}
}
}
}
}
}

args
} else {
Vec::new()
}
Expand Down
15 changes: 14 additions & 1 deletion src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2265,7 +2265,7 @@ supported tools: {}",
if tool == "cargo" && !gctx.cli_unstable().cargo_lints {
warn_for_cargo_lint_feature(gctx, warnings);
}
for name in lints.keys() {
for (name, config) in lints {
if let Some((prefix, suffix)) = name.split_once("::") {
if tool == prefix {
anyhow::bail!(
Expand All @@ -2278,6 +2278,19 @@ supported tools: {}",
} else {
anyhow::bail!("`lints.{tool}.{name}` is not a valid lint name")
}
} else if let Some(config) = config.config() {
for config_name in config.keys() {
// manually report unused manifest key warning since we collect all the "extra"
// keys and values inside the config table
//
// except for `rust.unexpected_cfgs.check-cfg` which is used by rustc/rustdoc
if !(tool == "rust" && name == "unexpected_cfgs" && config_name == "check-cfg")
{
let message =
format!("unused manifest key: `lints.{tool}.{name}.{config_name}`");
warnings.push(message);
}
}
}
}
}
Expand Down

0 comments on commit eff3734

Please sign in to comment.