Skip to content

Commit 18f2caf

Browse files
committed
Respect [lints.rust.unexpected_cfgs.check-cfg] lint config
1 parent efa7e19 commit 18f2caf

File tree

4 files changed

+57
-17
lines changed

4 files changed

+57
-17
lines changed

Diff for: src/cargo/core/compiler/build_context/target_info.rs

+39
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,45 @@ impl TargetInfo {
364364
true
365365
}
366366

367+
/// The [`CheckCfg`] settings with extra arguments passed.
368+
pub fn check_cfg_with_extra_args(
369+
&self,
370+
gctx: &GlobalContext,
371+
rustc: &Rustc,
372+
extra_args: &[String],
373+
) -> CargoResult<CheckCfg> {
374+
let mut process = rustc.workspace_process();
375+
376+
apply_env_config(gctx, &mut process)?;
377+
process
378+
.arg("-")
379+
.arg("--print=check-cfg")
380+
.arg("--check-cfg=cfg()")
381+
.arg("-Zunstable-options")
382+
.args(&self.rustflags)
383+
.args(extra_args)
384+
.env_remove("RUSTC_LOG");
385+
386+
// Removes `FD_CLOEXEC` set by `jobserver::Client` to pass jobserver
387+
// as environment variables specify.
388+
if let Some(client) = gctx.jobserver_from_env() {
389+
process.inherit_jobserver(client);
390+
}
391+
392+
let (output, _error) = rustc
393+
.cached_output(&process, 0)
394+
.with_context(|| "failed to run `rustc` to learn about check-cfg information")?;
395+
396+
let lines = output.lines();
397+
let mut check_cfg = CheckCfg::default();
398+
check_cfg.exhaustive = true;
399+
400+
Ok(lines.fold(check_cfg, |mut check_cfg, line| {
401+
check_cfg.process_line(line);
402+
check_cfg
403+
}))
404+
}
405+
367406
/// All the target [`Cfg`] settings.
368407
pub fn cfg(&self) -> &[Cfg] {
369408
&self.cfg

Diff for: src/cargo/util/lints.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,20 @@ pub fn unexpected_target_cfgs(
976976
return Ok(());
977977
};
978978

979+
// If we have extra `--check-cfg` args comming from the lints config, we need to
980+
// refetch the `--print=check-cfg` with those extra args.
981+
let lint_rustflags = pkg.manifest().lint_rustflags();
982+
let check_cfg = if lint_rustflags.iter().any(|a| a == "--check-cfg") {
983+
Some(target_info.check_cfg_with_extra_args(gctx, &rustc, lint_rustflags)?)
984+
} else {
985+
None
986+
};
987+
let check_cfg = check_cfg.as_ref().unwrap_or(&global_check_cfg);
988+
989+
if !check_cfg.exhaustive {
990+
continue;
991+
}
992+
979993
for dep in pkg.summary().dependencies() {
980994
let Some(platform) = dep.platform() else {
981995
continue;
@@ -984,16 +998,9 @@ pub fn unexpected_target_cfgs(
984998
continue;
985999
};
9861000

987-
// FIXME: If the `[lints.rust.unexpected_cfgs.check-cfg]` config is set we should
988-
// re-fetch the check-cfg informations with those extra args
989-
990-
if !global_check_cfg.exhaustive {
991-
continue;
992-
}
993-
9941001
warn_on_unexpected_cfgs(
9951002
gctx,
996-
&global_check_cfg,
1003+
&check_cfg,
9971004
cfg_expr,
9981005
lint_level,
9991006
error_count,

Diff for: src/doc/src/reference/unstable.md

-2
Original file line numberDiff line numberDiff line change
@@ -1735,8 +1735,6 @@ When in doubt, you can discuss this in [#14520](https://github.com/rust-lang/car
17351735

17361736
* Tracking Issue: [#00000](https://github.com/rust-lang/cargo/issues/00000)
17371737

1738-
**WARNING: Incomplete/WIP!**
1739-
17401738
This feature checks for unexpected cfgs in `[target.'cfg(...)']` entries, based
17411739
on `rustc --print=check-cfg`.
17421740

Diff for: tests/testsuite/cfg.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -620,11 +620,8 @@ fn unexpected_cfgs_target_with_lint() {
620620

621621
p.cargo("check -Zcargo-lints -Zcheck-target-cfgs")
622622
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
623-
// FIXME: We should not warn on `cfg(foo = "foo")` but we currently do
624623
.with_stderr_data(str![[r#"
625624
[WARNING] [ROOT]/foo/Cargo.toml: unexpected `cfg` condition name: `bar` in `[target.'cfg(bar)'.dependencies]`
626-
[WARNING] [ROOT]/foo/Cargo.toml: unexpected `cfg` condition name: `foo` for `foo = "foo"` in `[target.'cfg(foo = "foo")'.dependencies]`
627-
[WARNING] [ROOT]/foo/Cargo.toml: unexpected `cfg` condition name: `foo` in `[target.'cfg(foo)'.dependencies]`
628625
[LOCKING] 1 package to latest compatible version
629626
[CHECKING] a v0.0.1 ([ROOT]/foo)
630627
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
@@ -729,13 +726,12 @@ fn unexpected_cfgs_target_cfg_any() {
729726

730727
p.cargo("check -Zcargo-lints -Zcheck-target-cfgs")
731728
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
732-
// FIXME: We shouldn't be linting `cfg(foo)` because of the `cfg(any())`
733729
.with_stderr_data(str![[r#"
734-
[ERROR] [ROOT]/foo/Cargo.toml: unexpected `cfg` condition name: `foo` in `[target.'cfg(foo)'.dependencies]`
730+
[LOCKING] 1 package to latest compatible version
731+
[CHECKING] a v0.0.1 ([ROOT]/foo)
732+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
735733
736734
"#]])
737-
// nor should we error out because of the level="deny"
738-
.with_status(101)
739735
.run();
740736
}
741737

0 commit comments

Comments
 (0)