Skip to content

Commit 25e6ef3

Browse files
committed
Respect [lints.rust.unexpected_cfgs] lint level
1 parent a2dc7bf commit 25e6ef3

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

Diff for: src/cargo/core/workspace.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,10 @@ impl<'gctx> Workspace<'gctx> {
12211221
.get("cargo")
12221222
.cloned()
12231223
.unwrap_or(manifest::TomlToolLints::default());
1224+
let rust_lints = toml_lints
1225+
.get("rust")
1226+
.cloned()
1227+
.unwrap_or(manifest::TomlToolLints::default());
12241228

12251229
let ws_contents = match self.root_maybe() {
12261230
MaybePackage::Package(pkg) => pkg.manifest().contents(),
@@ -1242,7 +1246,7 @@ impl<'gctx> Workspace<'gctx> {
12421246
self.gctx,
12431247
)?;
12441248
check_im_a_teapot(pkg, &path, &cargo_lints, &mut error_count, self.gctx)?;
1245-
unexpected_target_cfgs(self, pkg, &path, &mut error_count, self.gctx)?;
1249+
unexpected_target_cfgs(self, pkg, &path, &rust_lints, &mut error_count, self.gctx)?;
12461250
if error_count > 0 {
12471251
Err(crate::util::errors::AlreadyPrintedError::new(anyhow!(
12481252
"encountered {error_count} errors(s) while running lints"

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

+21-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::path::Path;
1212
use toml_edit::ImDocument;
1313

1414
const LINT_GROUPS: &[LintGroup] = &[TEST_DUMMY_UNSTABLE];
15-
pub const LINTS: &[Lint] = &[IM_A_TEAPOT, UNKNOWN_LINTS];
15+
pub const LINTS: &[Lint] = &[IM_A_TEAPOT, UNEXPECTED_CFGS, UNKNOWN_LINTS];
1616

1717
pub fn analyze_cargo_lints_table(
1818
pkg: &Package,
@@ -608,17 +608,35 @@ fn output_unknown_lints(
608608
Ok(())
609609
}
610610

611+
// FIXME: This lint is only used for the Cargo infra, it's actually defined in rustc
612+
// it-self, which is broken is several ways, as it doesn't take into account
613+
// `rustc` flags ('via `RUSTFLAGS`), nor the possible `rustc` lints groups, ...
614+
const UNEXPECTED_CFGS: Lint = Lint {
615+
name: "unexpected_cfgs",
616+
desc: "lint on unexpected target cfgs",
617+
groups: &[],
618+
default_level: LintLevel::Warn,
619+
edition_lint_opts: None,
620+
feature_gate: None,
621+
docs: None,
622+
};
623+
611624
pub fn unexpected_target_cfgs(
612625
ws: &Workspace<'_>,
613626
pkg: &Package,
614627
path: &Path,
628+
rust_lints: &TomlToolLints,
615629
error_count: &mut usize,
616630
gctx: &GlobalContext,
617631
) -> CargoResult<()> {
618632
let manifest = pkg.manifest();
619633

620-
// FIXME: We should get the lint level from `[lints.rust.unexpected_cfgs]`
621-
let lint_level = LintLevel::Warn;
634+
let (lint_level, _lint_reason) =
635+
UNEXPECTED_CFGS.level(rust_lints, manifest.edition(), manifest.unstable_features());
636+
637+
if lint_level == LintLevel::Allow {
638+
return Ok(());
639+
}
622640

623641
let rustc = gctx.load_global_rustc(Some(ws))?;
624642
// FIXME: While it doesn't doesn't really matter for `--print=check-cfg`, we should

Diff for: tests/testsuite/cfg.rs

+7-19
Original file line numberDiff line numberDiff line change
@@ -882,14 +882,7 @@ fn unexpected_cfgs_target_lint_level_allow() {
882882

883883
p.cargo("check -Zcargo-lints -Zcheck-target-cfgs")
884884
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
885-
// FIXME: We shouldn't warn any target cfgs because of the level="allow"
886885
.with_stderr_data(str![[r#"
887-
[WARNING] unexpected `cfg` condition name: foo
888-
--> Cargo.toml:8:25
889-
|
890-
8 | [target."cfg(foo)".dependencies]
891-
| ----------
892-
|
893886
[LOCKING] 1 package to latest compatible version
894887
[CHECKING] a v0.0.1 ([ROOT]/foo)
895888
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
@@ -925,19 +918,15 @@ fn unexpected_cfgs_target_lint_level_deny() {
925918
p.cargo("check -Zcargo-lints -Zcheck-target-cfgs")
926919
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
927920
.with_stderr_data(str![[r#"
928-
[WARNING] unexpected `cfg` condition name: foo
921+
[ERROR] unexpected `cfg` condition name: foo
929922
--> Cargo.toml:8:25
930923
|
931924
8 | [target."cfg(foo)".dependencies]
932-
| ----------
925+
| ^^^^^^^^^^
933926
|
934-
[LOCKING] 1 package to latest compatible version
935-
[CHECKING] a v0.0.1 ([ROOT]/foo)
936-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
937927
938928
"#]])
939-
// FIXME: this test should fail
940-
// .with_status(101)
929+
.with_status(101)
941930
.run();
942931
}
943932

@@ -970,17 +959,16 @@ fn unexpected_cfgs_target_cfg_any() {
970959
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
971960
// FIXME: We shouldn't be linting `cfg(foo)` because of the `cfg(any())`
972961
.with_stderr_data(str![[r#"
973-
[WARNING] unexpected `cfg` condition name: foo
962+
[ERROR] unexpected `cfg` condition name: foo
974963
--> Cargo.toml:8:25
975964
|
976965
8 | [target."cfg(foo)".dependencies]
977-
| ----------
966+
| ^^^^^^^^^^
978967
|
979-
[LOCKING] 1 package to latest compatible version
980-
[CHECKING] a v0.0.1 ([ROOT]/foo)
981-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
982968
983969
"#]])
970+
// nor should we error out because of the level="deny"
971+
.with_status(101)
984972
.run();
985973
}
986974

0 commit comments

Comments
 (0)