Skip to content

Commit

Permalink
Respect [lints.rust.unexpected_cfgs.check-cfg] lint config
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Sep 29, 2024
1 parent b97c2fd commit dbc30b8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 29 deletions.
41 changes: 41 additions & 0 deletions src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,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<CheckCfg> {
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
Expand Down
17 changes: 12 additions & 5 deletions src/cargo/util/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,13 +927,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;
Expand All @@ -948,7 +955,7 @@ pub fn unexpected_target_cfgs(
Cfg::KeyPair(name, value) => (name, Some(value.to_string())),
};

match global_check_cfg.expecteds.get(name) {
match check_cfg.expecteds.get(name) {
Some(ExpectedValues::Some(values)) if !values.contains(&value) => {
let level = lint_level.to_diagnostic_level();
if lint_level == LintLevel::Forbid || lint_level == LintLevel::Deny {
Expand Down
2 changes: 0 additions & 2 deletions src/doc/src/reference/unstable.md
Original file line number Diff line number Diff line change
Expand Up @@ -1735,8 +1735,6 @@ When in doubt, you can discuss this in [#14520](https://github.com/rust-lang/car

* 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`.

Expand Down
25 changes: 3 additions & 22 deletions tests/testsuite/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,26 +645,13 @@ 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
|
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
Expand Down Expand Up @@ -819,18 +806,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();
}

Expand Down

0 comments on commit dbc30b8

Please sign in to comment.