Skip to content

Commit 9797aec

Browse files
committed
Respect [lints.rust.unexpected_cfgs.check-cfg] lint config
1 parent bace0ea commit 9797aec

File tree

3 files changed

+53
-24
lines changed

3 files changed

+53
-24
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 & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -951,26 +951,13 @@ fn unexpected_cfgs_target_with_lint() {
951951

952952
p.cargo("check -Zcargo-lints -Zcheck-target-cfgs")
953953
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
954-
// FIXME: We should not warn on `cfg(foo = "foo")` but we currently do
955954
.with_stderr_data(str![[r#"
956955
[WARNING] unexpected `cfg` condition name: bar
957956
--> Cargo.toml:19:25
958957
|
959958
19 | [target."cfg(bar)".dependencies]
960959
| ----------
961960
|
962-
[WARNING] unexpected `cfg` condition name: foo for `foo = "foo"`
963-
--> Cargo.toml:16:25
964-
|
965-
16 | [target.'cfg(foo = "foo")'.dependencies] # should not warn here
966-
| ------------------
967-
|
968-
[WARNING] unexpected `cfg` condition name: foo
969-
--> Cargo.toml:13:25
970-
|
971-
13 | [target."cfg(foo)".dependencies] # should not warn here
972-
| ----------
973-
|
974961
[LOCKING] 1 package to latest compatible version
975962
[CHECKING] a v0.0.1 ([ROOT]/foo)
976963
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
@@ -1146,12 +1133,6 @@ fn unexpected_cfgs_target_cfg_any() {
11461133
p.cargo("check -Zcargo-lints -Zcheck-target-cfgs")
11471134
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
11481135
.with_stderr_data(str![[r#"
1149-
[WARNING] unexpected `cfg` condition name: foo
1150-
--> Cargo.toml:13:25
1151-
|
1152-
13 | [target."cfg(foo)".dependencies]
1153-
| ----------
1154-
|
11551136
[LOCKING] 1 package to latest compatible version
11561137
[CHECKING] a v0.0.1 ([ROOT]/foo)
11571138
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

0 commit comments

Comments
 (0)