Skip to content

Commit 603d16f

Browse files
committed
Respect [lints.rust.unexpected_cfgs.check-cfg] lint config
1 parent f239934 commit 603d16f

File tree

4 files changed

+56
-29
lines changed

4 files changed

+56
-29
lines changed

src/cargo/core/compiler/build_context/target_info.rs

+41
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,47 @@ 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+
for line in lines {
401+
check_cfg
402+
.parse_print_check_cfg_line(line)
403+
.with_context(|| format!("unable to parse a line from `--print=check-cfg`"))?;
404+
}
405+
Ok(check_cfg)
406+
}
407+
367408
/// All the target [`Cfg`] settings.
368409
pub fn cfg(&self) -> &[Cfg] {
369410
&self.cfg

src/cargo/util/lints.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -651,13 +651,20 @@ pub fn unexpected_target_cfgs(
651651
return Ok(());
652652
};
653653

654-
if !global_check_cfg.exhaustive {
654+
// If we have extra `--check-cfg` args comming from the lints config, we need to
655+
// refetch the `--print=check-cfg` with those extra args.
656+
let lint_rustflags = pkg.manifest().lint_rustflags();
657+
let check_cfg = if lint_rustflags.iter().any(|a| a == "--check-cfg") {
658+
Some(target_info.check_cfg_with_extra_args(gctx, &rustc, lint_rustflags)?)
659+
} else {
660+
None
661+
};
662+
let check_cfg = check_cfg.as_ref().unwrap_or(&global_check_cfg);
663+
664+
if !check_cfg.exhaustive {
655665
return Ok(());
656666
}
657667

658-
// FIXME: If the `[lints.rust.unexpected_cfgs.check-cfg]` config is set we should
659-
// re-fetch the check-cfg informations with those extra args
660-
661668
for dep in pkg.summary().dependencies() {
662669
let Some(platform) = dep.platform() else {
663670
continue;
@@ -672,7 +679,7 @@ pub fn unexpected_target_cfgs(
672679
Cfg::KeyPair(name, value) => (name, Some(value.to_string())),
673680
};
674681

675-
match global_check_cfg.expecteds.get(name) {
682+
match check_cfg.expecteds.get(name) {
676683
Some(ExpectedValues::Some(values)) if !values.contains(&value) => {
677684
let level = lint_level.to_diagnostic_level();
678685
if lint_level == LintLevel::Forbid || lint_level == LintLevel::Deny {

src/doc/src/reference/unstable.md

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

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

1754-
**WARNING: Incomplete/WIP!**
1755-
17561754
This feature checks for unexpected cfgs in `[target.'cfg(...)']` entries, based
17571755
on `rustc --print=check-cfg`.
17581756

tests/testsuite/cfg.rs

+3-22
Original file line numberDiff line numberDiff line change
@@ -645,26 +645,13 @@ fn unexpected_cfgs_target_with_lint() {
645645

646646
p.cargo("check -Zcargo-lints -Zcheck-target-cfgs")
647647
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
648-
// FIXME: We should not warn on `cfg(foo = "foo")` but we currently do
649648
.with_stderr_data(str![[r#"
650649
[WARNING] unexpected `cfg` condition name: bar
651650
--> Cargo.toml:14:25
652651
|
653652
14 | [target."cfg(bar)".dependencies]
654653
| ----------
655654
|
656-
[WARNING] unexpected `cfg` condition name: foo for `foo = "foo"`
657-
--> Cargo.toml:11:25
658-
|
659-
11 | [target.'cfg(foo = "foo")'.dependencies] # should not warn here
660-
| ------------------
661-
|
662-
[WARNING] unexpected `cfg` condition name: foo
663-
--> Cargo.toml:8:25
664-
|
665-
8 | [target."cfg(foo)".dependencies] # should not warn here
666-
| ----------
667-
|
668655
[LOCKING] 1 package to latest compatible version
669656
[CHECKING] a v0.0.1 ([ROOT]/foo)
670657
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
@@ -819,18 +806,12 @@ fn unexpected_cfgs_target_cfg_any() {
819806

820807
p.cargo("check -Zcargo-lints -Zcheck-target-cfgs")
821808
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
822-
// FIXME: We shouldn't be linting `cfg(foo)` because of the `cfg(any())`
823809
.with_stderr_data(str![[r#"
824-
[ERROR] unexpected `cfg` condition name: foo
825-
--> Cargo.toml:8:25
826-
|
827-
8 | [target."cfg(foo)".dependencies]
828-
| ^^^^^^^^^^
829-
|
810+
[LOCKING] 1 package to latest compatible version
811+
[CHECKING] a v0.0.1 ([ROOT]/foo)
812+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
830813
831814
"#]])
832-
// nor should we error out because of the level="deny"
833-
.with_status(101)
834815
.run();
835816
}
836817

0 commit comments

Comments
 (0)