Skip to content

Commit e6dca67

Browse files
committed
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)"] } ```
1 parent 1e4857a commit e6dca67

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

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: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,9 +2282,14 @@ supported tools: {}",
22822282
for config_name in config.keys() {
22832283
// manually report unused manifest key warning since we collect all the "extra"
22842284
// keys and values inside the config table
2285-
let message =
2286-
format!("unused manifest key: `lints.{tool}.{name}.{config_name}`");
2287-
warnings.push(message);
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+
}
22882293
}
22892294
}
22902295
}

tests/testsuite/check_cfg.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,28 @@ fn build_script_test() {
496496
.with_stdout_contains_n("test [..] ... ok", 3)
497497
.run();
498498
}
499+
500+
#[cargo_test(>=1.79, reason = "--check-cfg was stabilized in Rust 1.79")]
501+
fn config_simple() {
502+
let p = project()
503+
.file(
504+
"Cargo.toml",
505+
r#"
506+
[package]
507+
name = "foo"
508+
version = "0.1.0"
509+
edition = "2015"
510+
511+
[lints.rust]
512+
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(has_foo)", "cfg(has_bar, values(\"yes\", \"no\"))"] }
513+
"#,
514+
)
515+
.file("src/main.rs", "fn main() {}")
516+
.build();
517+
518+
p.cargo("check -v")
519+
.with_stderr_contains(x!("rustc" => "cfg" of "has_foo"))
520+
.with_stderr_contains(x!("rustc" => "cfg" of "has_bar" with "yes" "no"))
521+
.with_stderr_does_not_contain("[..]unused manifest key[..]")
522+
.run();
523+
}

0 commit comments

Comments
 (0)