Skip to content

Commit 465bec1

Browse files
committed
Respect [lints.rust.unexpected_cfgs.check-cfg] lint config
1 parent 48cb321 commit 465bec1

File tree

3 files changed

+53
-18
lines changed

3 files changed

+53
-18
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,47 @@ impl TargetInfo {
411411
true
412412
}
413413

414+
/// The [`CheckCfg`] settings with extra arguments passed.
415+
pub fn check_cfg_with_extra_args(
416+
&self,
417+
gctx: &GlobalContext,
418+
rustc: &Rustc,
419+
extra_args: &[String],
420+
) -> CargoResult<CheckCfg> {
421+
let mut process = rustc.workspace_process();
422+
423+
apply_env_config(gctx, &mut process)?;
424+
process
425+
.arg("-")
426+
.arg("--print=check-cfg")
427+
.arg("--check-cfg=cfg()")
428+
.arg("-Zunstable-options")
429+
.args(&self.rustflags)
430+
.args(extra_args)
431+
.env_remove("RUSTC_LOG");
432+
433+
// Removes `FD_CLOEXEC` set by `jobserver::Client` to pass jobserver
434+
// as environment variables specify.
435+
if let Some(client) = gctx.jobserver_from_env() {
436+
process.inherit_jobserver(client);
437+
}
438+
439+
let (output, _error) = rustc
440+
.cached_output(&process, 0)
441+
.with_context(|| "failed to run `rustc` to learn about check-cfg information")?;
442+
443+
let lines = output.lines();
444+
let mut check_cfg = CheckCfg::default();
445+
check_cfg.exhaustive = true;
446+
447+
for line in lines {
448+
check_cfg
449+
.parse_print_check_cfg_line(line)
450+
.with_context(|| format!("unable to parse a line from `--print=check-cfg`"))?;
451+
}
452+
Ok(check_cfg)
453+
}
454+
414455
/// All the target [`Cfg`] settings.
415456
pub fn cfg(&self) -> &[Cfg] {
416457
&self.cfg

src/cargo/util/lints.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -665,13 +665,20 @@ pub fn unexpected_target_cfgs(
665665
return Ok(());
666666
};
667667

668-
if !global_check_cfg.exhaustive {
668+
// If we have extra `--check-cfg` args comming from the lints config, we need to
669+
// refetch the `--print=check-cfg` with those extra args.
670+
let lint_rustflags = pkg.manifest().lint_rustflags();
671+
let check_cfg = if lint_rustflags.iter().any(|a| a == "--check-cfg") {
672+
Some(target_info.check_cfg_with_extra_args(gctx, &rustc, lint_rustflags)?)
673+
} else {
674+
None
675+
};
676+
let check_cfg = check_cfg.as_ref().unwrap_or(&global_check_cfg);
677+
678+
if !check_cfg.exhaustive {
669679
return Ok(());
670680
}
671681

672-
// FIXME: If the `[lints.rust.unexpected_cfgs.check-cfg]` config is set we should
673-
// re-fetch the check-cfg informations with those extra args
674-
675682
for dep in pkg.summary().dependencies() {
676683
let Some(platform) = dep.platform() else {
677684
continue;
@@ -686,7 +693,7 @@ pub fn unexpected_target_cfgs(
686693
Cfg::KeyPair(name, value) => (name, Some(value.to_string())),
687694
};
688695

689-
match global_check_cfg.expecteds.get(name.as_str()) {
696+
match check_cfg.expecteds.get(name.as_str()) {
690697
Some(ExpectedValues::Some(values)) if !values.contains(&value) => {
691698
let level = lint_level.to_diagnostic_level();
692699
if lint_level == LintLevel::Forbid || lint_level == LintLevel::Deny {

tests/testsuite/cfg.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -941,26 +941,13 @@ fn unexpected_cfgs_target_with_lint() {
941941

942942
p.cargo("check -Zcargo-lints -Zcheck-target-cfgs")
943943
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
944-
// FIXME: We should not warn on `cfg(foo = "foo")` but we currently do
945944
.with_stderr_data(str![[r#"
946945
[WARNING] unexpected `cfg` condition name: bar
947946
--> Cargo.toml:14:25
948947
|
949948
14 | [target."cfg(bar)".dependencies]
950949
| ----------
951950
|
952-
[WARNING] unexpected `cfg` condition name: foo for `foo = "foo"`
953-
--> Cargo.toml:11:25
954-
|
955-
11 | [target.'cfg(foo = "foo")'.dependencies] # should not warn here
956-
| ------------------
957-
|
958-
[WARNING] unexpected `cfg` condition name: foo
959-
--> Cargo.toml:8:25
960-
|
961-
8 | [target."cfg(foo)".dependencies] # should not warn here
962-
| ----------
963-
|
964951
[LOCKING] 1 package to latest compatible version
965952
[CHECKING] a v0.0.1 ([ROOT]/foo)
966953
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

0 commit comments

Comments
 (0)