diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index 5abab39afd44..f3b7716c6216 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -1217,6 +1217,10 @@ impl<'gctx> Workspace<'gctx> { .get("cargo") .cloned() .unwrap_or(manifest::TomlToolLints::default()); + let rust_lints = toml_lints + .get("rust") + .cloned() + .unwrap_or(manifest::TomlToolLints::default()); let ws_contents = match self.root_maybe() { MaybePackage::Package(pkg) => pkg.manifest().contents(), @@ -1238,7 +1242,7 @@ impl<'gctx> Workspace<'gctx> { self.gctx, )?; check_im_a_teapot(pkg, &path, &cargo_lints, &mut error_count, self.gctx)?; - unexpected_target_cfgs(self, pkg, &path, &mut error_count, self.gctx)?; + unexpected_target_cfgs(self, pkg, &path, &rust_lints, &mut error_count, self.gctx)?; if error_count > 0 { Err(crate::util::errors::AlreadyPrintedError::new(anyhow!( "encountered {error_count} errors(s) while running lints" diff --git a/src/cargo/util/lints.rs b/src/cargo/util/lints.rs index a44b9b404608..19ebfddfd21d 100644 --- a/src/cargo/util/lints.rs +++ b/src/cargo/util/lints.rs @@ -12,7 +12,7 @@ use std::path::Path; use toml_edit::ImDocument; const LINT_GROUPS: &[LintGroup] = &[TEST_DUMMY_UNSTABLE]; -pub const LINTS: &[Lint] = &[IM_A_TEAPOT, UNKNOWN_LINTS]; +pub const LINTS: &[Lint] = &[IM_A_TEAPOT, UNEXPECTED_CFGS, UNKNOWN_LINTS]; pub fn analyze_cargo_lints_table( pkg: &Package, @@ -608,10 +608,24 @@ fn output_unknown_lints( Ok(()) } +// FIXME: This lint is only used for the Cargo infra, it's actually defined in rustc +// it-self, which is broken is several ways, as it doesn't take into account +// `rustc` flags ('via `RUSTFLAGS`), nor the possible `rustc` lints groups, ... +const UNEXPECTED_CFGS: Lint = Lint { + name: "unexpected_cfgs", + desc: "lint on unexpected target cfgs", + groups: &[], + default_level: LintLevel::Warn, + edition_lint_opts: None, + feature_gate: None, + docs: None, +}; + pub fn unexpected_target_cfgs( ws: &Workspace<'_>, pkg: &Package, path: &Path, + rust_lints: &TomlToolLints, error_count: &mut usize, gctx: &GlobalContext, ) -> CargoResult<()> { @@ -621,8 +635,12 @@ pub fn unexpected_target_cfgs( return Ok(()); }; - // FIXME: We should get the lint level from `[lints.rust.unexpected_cfgs]` - let lint_level = LintLevel::Warn; + let (lint_level, _lint_reason) = + UNEXPECTED_CFGS.level(rust_lints, manifest.edition(), manifest.unstable_features()); + + if lint_level == LintLevel::Allow { + return Ok(()); + } let rustc = gctx.load_global_rustc(Some(ws))?; // FIXME: While it doesn't doesn't really matter for `--print=check-cfg`, we should diff --git a/tests/testsuite/cfg.rs b/tests/testsuite/cfg.rs index e09143b3d812..e382adc7990c 100644 --- a/tests/testsuite/cfg.rs +++ b/tests/testsuite/cfg.rs @@ -746,14 +746,7 @@ fn unexpected_cfgs_target_lint_level_allow() { p.cargo("check -Zcargo-lints -Zcheck-target-cfgs") .masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"]) - // FIXME: We shouldn't warn any target cfgs because of the level="allow" .with_stderr_data(str![[r#" -[WARNING] unexpected `cfg` condition name: `foo` for `foo` - --> Cargo.toml:8:25 - | -8 | [target."cfg(foo)".dependencies] - | ---------- - | [LOCKING] 1 package to latest compatible version [CHECKING] a v0.0.1 ([ROOT]/foo) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s @@ -789,19 +782,15 @@ fn unexpected_cfgs_target_lint_level_deny() { p.cargo("check -Zcargo-lints -Zcheck-target-cfgs") .masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"]) .with_stderr_data(str![[r#" -[WARNING] unexpected `cfg` condition name: `foo` for `foo` +[ERROR] unexpected `cfg` condition name: `foo` for `foo` --> Cargo.toml:8:25 | 8 | [target."cfg(foo)".dependencies] - | ---------- + | ^^^^^^^^^^ | -[LOCKING] 1 package to latest compatible version -[CHECKING] a v0.0.1 ([ROOT]/foo) -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s "#]]) - // FIXME: this test should fail - // .with_status(101) + .with_status(101) .run(); } @@ -834,17 +823,16 @@ fn unexpected_cfgs_target_cfg_any() { .masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"]) // FIXME: We shouldn't be linting `cfg(foo)` because of the `cfg(any())` .with_stderr_data(str![[r#" -[WARNING] unexpected `cfg` condition name: `foo` for `foo` +[ERROR] unexpected `cfg` condition name: `foo` for `foo` --> Cargo.toml:8:25 | 8 | [target."cfg(foo)".dependencies] - | ---------- + | ^^^^^^^^^^ | -[LOCKING] 1 package to latest compatible version -[CHECKING] a v0.0.1 ([ROOT]/foo) -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s "#]]) + // nor should we error out because of the level="deny" + .with_status(101) .run(); }