Skip to content

Commit 7253547

Browse files
committed
Respect [lints.rust.unexpected_cfgs] lint level
1 parent b9958a3 commit 7253547

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

src/cargo/core/workspace.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,10 @@ impl<'gctx> Workspace<'gctx> {
11981198
.get("cargo")
11991199
.cloned()
12001200
.unwrap_or(manifest::TomlToolLints::default());
1201+
let rust_lints = toml_lints
1202+
.get("rust")
1203+
.cloned()
1204+
.unwrap_or(manifest::TomlToolLints::default());
12011205

12021206
let ws_contents = match self.root_maybe() {
12031207
MaybePackage::Package(pkg) => pkg.manifest().contents(),
@@ -1219,7 +1223,7 @@ impl<'gctx> Workspace<'gctx> {
12191223
self.gctx,
12201224
)?;
12211225
check_im_a_teapot(pkg, &path, &cargo_lints, &mut error_count, self.gctx)?;
1222-
unexpected_target_cfgs(self, pkg, &path, &mut error_count, self.gctx)?;
1226+
unexpected_target_cfgs(self, pkg, &path, &rust_lints, &mut error_count, self.gctx)?;
12231227
if error_count > 0 {
12241228
Err(crate::util::errors::AlreadyPrintedError::new(anyhow!(
12251229
"encountered {error_count} errors(s) while running lints"

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

tests/testsuite/cfg.rs

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

745745
p.cargo("check -Zcargo-lints -Zcheck-target-cfgs")
746746
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
747-
// FIXME: We shouldn't warn any target cfgs because of the level="allow"
748747
.with_stderr_data(str![[r#"
749-
[WARNING] unexpected `cfg` condition name: foo
750-
--> Cargo.toml:8:25
751-
|
752-
8 | [target."cfg(foo)".dependencies]
753-
| ----------
754-
|
755748
[LOCKING] 1 package to latest compatible version
756749
[CHECKING] a v0.0.1 ([ROOT]/foo)
757750
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
@@ -787,19 +780,15 @@ fn unexpected_cfgs_target_lint_level_deny() {
787780
p.cargo("check -Zcargo-lints -Zcheck-target-cfgs")
788781
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
789782
.with_stderr_data(str![[r#"
790-
[WARNING] unexpected `cfg` condition name: foo
783+
[ERROR] unexpected `cfg` condition name: foo
791784
--> Cargo.toml:8:25
792785
|
793786
8 | [target."cfg(foo)".dependencies]
794-
| ----------
787+
| ^^^^^^^^^^
795788
|
796-
[LOCKING] 1 package to latest compatible version
797-
[CHECKING] a v0.0.1 ([ROOT]/foo)
798-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
799789
800790
"#]])
801-
// FIXME: this test should fail
802-
// .with_status(101)
791+
.with_status(101)
803792
.run();
804793
}
805794

@@ -832,17 +821,16 @@ fn unexpected_cfgs_target_cfg_any() {
832821
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
833822
// FIXME: We shouldn't be linting `cfg(foo)` because of the `cfg(any())`
834823
.with_stderr_data(str![[r#"
835-
[WARNING] unexpected `cfg` condition name: foo
824+
[ERROR] unexpected `cfg` condition name: foo
836825
--> Cargo.toml:8:25
837826
|
838827
8 | [target."cfg(foo)".dependencies]
839-
| ----------
828+
| ^^^^^^^^^^
840829
|
841-
[LOCKING] 1 package to latest compatible version
842-
[CHECKING] a v0.0.1 ([ROOT]/foo)
843-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
844830
845831
"#]])
832+
// nor should we error out because of the level="deny"
833+
.with_status(101)
846834
.run();
847835
}
848836

0 commit comments

Comments
 (0)