Skip to content

Commit 8125d3c

Browse files
committed
Respect [lints.rust.unexpected_cfgs] lint level
1 parent aa6f189 commit 8125d3c

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
@@ -1240,6 +1240,10 @@ impl<'gctx> Workspace<'gctx> {
12401240
.get("cargo")
12411241
.cloned()
12421242
.unwrap_or(manifest::TomlToolLints::default());
1243+
let rust_lints = toml_lints
1244+
.get("rust")
1245+
.cloned()
1246+
.unwrap_or(manifest::TomlToolLints::default());
12431247

12441248
let ws_contents = match self.root_maybe() {
12451249
MaybePackage::Package(pkg) => pkg.manifest().contents(),
@@ -1261,7 +1265,7 @@ impl<'gctx> Workspace<'gctx> {
12611265
self.gctx,
12621266
)?;
12631267
check_im_a_teapot(pkg, &path, &cargo_lints, &mut error_count, self.gctx)?;
1264-
unexpected_target_cfgs(self, pkg, &path, &mut error_count, self.gctx)?;
1268+
unexpected_target_cfgs(self, pkg, &path, &rust_lints, &mut error_count, self.gctx)?;
12651269
if error_count > 0 {
12661270
Err(crate::util::errors::AlreadyPrintedError::new(anyhow!(
12671271
"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
@@ -1040,14 +1040,7 @@ fn unexpected_cfgs_target_lint_level_allow() {
10401040

10411041
p.cargo("check -Zcargo-lints -Zcheck-target-cfgs")
10421042
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
1043-
// FIXME: We shouldn't warn any target cfgs because of the level="allow"
10441043
.with_stderr_data(str![[r#"
1045-
[WARNING] unexpected `cfg` condition name: foo
1046-
--> Cargo.toml:8:25
1047-
|
1048-
8 | [target."cfg(foo)".dependencies]
1049-
| ----------
1050-
|
10511044
[LOCKING] 1 package to latest compatible version
10521045
[CHECKING] a v0.0.1 ([ROOT]/foo)
10531046
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
@@ -1083,19 +1076,15 @@ fn unexpected_cfgs_target_lint_level_deny() {
10831076
p.cargo("check -Zcargo-lints -Zcheck-target-cfgs")
10841077
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
10851078
.with_stderr_data(str![[r#"
1086-
[WARNING] unexpected `cfg` condition name: foo
1079+
[ERROR] unexpected `cfg` condition name: foo
10871080
--> Cargo.toml:8:25
10881081
|
10891082
8 | [target."cfg(foo)".dependencies]
1090-
| ----------
1083+
| ^^^^^^^^^^
10911084
|
1092-
[LOCKING] 1 package to latest compatible version
1093-
[CHECKING] a v0.0.1 ([ROOT]/foo)
1094-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
10951085
10961086
"#]])
1097-
// FIXME: this test should fail
1098-
// .with_status(101)
1087+
.with_status(101)
10991088
.run();
11001089
}
11011090

@@ -1128,17 +1117,16 @@ fn unexpected_cfgs_target_cfg_any() {
11281117
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
11291118
// FIXME: We shouldn't be linting `cfg(foo)` because of the `cfg(any())`
11301119
.with_stderr_data(str![[r#"
1131-
[WARNING] unexpected `cfg` condition name: foo
1120+
[ERROR] unexpected `cfg` condition name: foo
11321121
--> Cargo.toml:8:25
11331122
|
11341123
8 | [target."cfg(foo)".dependencies]
1135-
| ----------
1124+
| ^^^^^^^^^^
11361125
|
1137-
[LOCKING] 1 package to latest compatible version
1138-
[CHECKING] a v0.0.1 ([ROOT]/foo)
1139-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
11401126
11411127
"#]])
1128+
// nor should we error out because of the level="deny"
1129+
.with_status(101)
11421130
.run();
11431131
}
11441132

0 commit comments

Comments
 (0)