Add special check-cfg config for the unexpected_cfgs lint

This permits things like this to be recognized and passed to
rustc/rustdoc.

```rust
[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(foo)"] }
```
This commit is contained in:
Urgau 2024-05-15 21:31:20 +02:00
parent 1e4857a4d9
commit e6dca67a84
3 changed files with 60 additions and 5 deletions

View File

@ -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()
}

View File

@ -2282,9 +2282,14 @@ supported tools: {}",
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
let message =
format!("unused manifest key: `lints.{tool}.{name}.{config_name}`");
warnings.push(message);
//
// 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);
}
}
}
}

View File

@ -496,3 +496,28 @@ fn build_script_test() {
.with_stdout_contains_n("test [..] ... ok", 3)
.run();
}
#[cargo_test(>=1.79, reason = "--check-cfg was stabilized in Rust 1.79")]
fn config_simple() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2015"
[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(has_foo)", "cfg(has_bar, values(\"yes\", \"no\"))"] }
"#,
)
.file("src/main.rs", "fn main() {}")
.build();
p.cargo("check -v")
.with_stderr_contains(x!("rustc" => "cfg" of "has_foo"))
.with_stderr_contains(x!("rustc" => "cfg" of "has_bar" with "yes" "no"))
.with_stderr_does_not_contain("[..]unused manifest key[..]")
.run();
}