From 09203d1e10a0d12e1dcd6af1b593b45ad9f97610 Mon Sep 17 00:00:00 2001 From: Urgau Date: Sun, 29 Sep 2024 18:27:36 +0200 Subject: [PATCH] Respect `[lints.rust.unexpected_cfgs.check-cfg]` lint config --- .../compiler/build_context/target_info.rs | 41 +++++++++++++++++++ src/cargo/util/lints.rs | 17 +++++--- src/doc/src/reference/unstable.md | 2 - tests/testsuite/cfg.rs | 25 ++--------- 4 files changed, 56 insertions(+), 29 deletions(-) diff --git a/src/cargo/core/compiler/build_context/target_info.rs b/src/cargo/core/compiler/build_context/target_info.rs index 78737107b8a..0ac1a2cf238 100644 --- a/src/cargo/core/compiler/build_context/target_info.rs +++ b/src/cargo/core/compiler/build_context/target_info.rs @@ -405,6 +405,47 @@ impl TargetInfo { true } + /// The [`CheckCfg`] settings with extra arguments passed. + pub fn check_cfg_with_extra_args( + &self, + gctx: &GlobalContext, + rustc: &Rustc, + extra_args: &[String], + ) -> CargoResult { + let mut process = rustc.workspace_process(); + + apply_env_config(gctx, &mut process)?; + process + .arg("-") + .arg("--print=check-cfg") + .arg("--check-cfg=cfg()") + .arg("-Zunstable-options") + .args(&self.rustflags) + .args(extra_args) + .env_remove("RUSTC_LOG"); + + // Removes `FD_CLOEXEC` set by `jobserver::Client` to pass jobserver + // as environment variables specify. + if let Some(client) = gctx.jobserver_from_env() { + process.inherit_jobserver(client); + } + + let (output, _error) = rustc + .cached_output(&process, 0) + .with_context(|| "failed to run `rustc` to learn about check-cfg information")?; + + let lines = output.lines(); + let mut check_cfg = CheckCfg::default(); + check_cfg.exhaustive = true; + + for line in lines { + check_cfg + .parse_print_check_cfg_line(line) + .with_context(|| format!("unable to parse a line from `--print=check-cfg`"))?; + } + Ok(check_cfg) + } + /// All the target [`Cfg`] settings. pub fn cfg(&self) -> &[Cfg] { &self.cfg diff --git a/src/cargo/util/lints.rs b/src/cargo/util/lints.rs index b67bae95eaa..a5e8d6ffebc 100644 --- a/src/cargo/util/lints.rs +++ b/src/cargo/util/lints.rs @@ -651,13 +651,20 @@ pub fn unexpected_target_cfgs( return Ok(()); }; - if !global_check_cfg.exhaustive { + // If we have extra `--check-cfg` args comming from the lints config, we need to + // refetch the `--print=check-cfg` with those extra args. + let lint_rustflags = pkg.manifest().lint_rustflags(); + let check_cfg = if lint_rustflags.iter().any(|a| a == "--check-cfg") { + Some(target_info.check_cfg_with_extra_args(gctx, &rustc, lint_rustflags)?) + } else { + None + }; + let check_cfg = check_cfg.as_ref().unwrap_or(&global_check_cfg); + + if !check_cfg.exhaustive { return Ok(()); } - // FIXME: If the `[lints.rust.unexpected_cfgs.check-cfg]` config is set we should - // re-fetch the check-cfg informations with those extra args - for dep in pkg.summary().dependencies() { let Some(platform) = dep.platform() else { continue; @@ -672,7 +679,7 @@ pub fn unexpected_target_cfgs( Cfg::KeyPair(name, value) => (name, Some(value.to_string())), }; - match global_check_cfg.expecteds.get(name.as_str()) { + match check_cfg.expecteds.get(name.as_str()) { Some(ExpectedValues::Some(values)) if !values.contains(&value) => { let level = lint_level.to_diagnostic_level(); if lint_level == LintLevel::Forbid || lint_level == LintLevel::Deny { diff --git a/src/doc/src/reference/unstable.md b/src/doc/src/reference/unstable.md index 93d006e6d49..7c35b099b18 100644 --- a/src/doc/src/reference/unstable.md +++ b/src/doc/src/reference/unstable.md @@ -1746,8 +1746,6 @@ Controls how Cargo handles warnings. Allowed values are: * Tracking Issue: [#00000](https://github.com/rust-lang/cargo/issues/00000) -**WARNING: Incomplete/WIP!** - This feature checks for unexpected cfgs in `[target.'cfg(...)']` entries, based on `rustc --print=check-cfg`. diff --git a/tests/testsuite/cfg.rs b/tests/testsuite/cfg.rs index 7271d2ca0d5..60e5d6cfdc9 100644 --- a/tests/testsuite/cfg.rs +++ b/tests/testsuite/cfg.rs @@ -783,7 +783,6 @@ fn unexpected_cfgs_target_with_lint() { p.cargo("check -Zcargo-lints -Zcheck-target-cfgs") .masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"]) - // FIXME: We should not warn on `cfg(foo = "foo")` but we currently do .with_stderr_data(str![[r#" [WARNING] unexpected `cfg` condition name: bar --> Cargo.toml:14:25 @@ -791,18 +790,6 @@ fn unexpected_cfgs_target_with_lint() { 14 | [target."cfg(bar)".dependencies] | ---------- | -[WARNING] unexpected `cfg` condition name: foo for `foo = "foo"` - --> Cargo.toml:11:25 - | -11 | [target.'cfg(foo = "foo")'.dependencies] # should not warn here - | ------------------ - | -[WARNING] unexpected `cfg` condition name: foo - --> Cargo.toml:8:25 - | -8 | [target."cfg(foo)".dependencies] # should not warn here - | ---------- - | [LOCKING] 1 package to latest compatible version [CHECKING] a v0.0.1 ([ROOT]/foo) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s @@ -957,18 +944,12 @@ fn unexpected_cfgs_target_cfg_any() { p.cargo("check -Zcargo-lints -Zcheck-target-cfgs") .masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"]) - // FIXME: We shouldn't be linting `cfg(foo)` because of the `cfg(any())` .with_stderr_data(str![[r#" -[ERROR] unexpected `cfg` condition name: foo - --> Cargo.toml:8:25 - | -8 | [target."cfg(foo)".dependencies] - | ^^^^^^^^^^ - | +[LOCKING] 1 package to latest compatible version +[CHECKING] a v0.0.1 ([ROOT]/foo) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s "#]]) - // nor should we error out because of the level="deny" - .with_status(101) .run(); }