From cf9bcc1b9964d80ee930a97150779e82f2a429c7 Mon Sep 17 00:00:00 2001 From: Urgau Date: Sun, 29 Sep 2024 18:24:06 +0200 Subject: [PATCH] Respect `[lints.rust.unexpected_cfgs]` lint level --- src/cargo/core/workspace.rs | 6 +++++- src/cargo/util/lints.rs | 23 +++++++++++++++++++++-- tests/testsuite/cfg.rs | 26 +++++++------------------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index 8df6db2e045b..a489b579f08a 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -1216,6 +1216,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(), @@ -1239,7 +1243,7 @@ impl<'gctx> Workspace<'gctx> { check_im_a_teapot(pkg, &path, &cargo_lints, &mut error_count, self.gctx)?; check_implicit_features(pkg, &path, &cargo_lints, &mut error_count, self.gctx)?; unused_dependencies(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 4db8b71405e7..d22bf3aa83bb 100644 --- a/src/cargo/util/lints.rs +++ b/src/cargo/util/lints.rs @@ -21,6 +21,7 @@ pub const LINTS: &[Lint] = &[ IM_A_TEAPOT, UNKNOWN_LINTS, UNUSED_OPTIONAL_DEPENDENCY, + UNEXPECTED_CFGS, ]; pub fn analyze_cargo_lints_table( @@ -883,17 +884,35 @@ pub fn unused_dependencies( 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<()> { let manifest = pkg.manifest(); - // 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 f0d40aaadce1..176c2c74de66 100644 --- a/tests/testsuite/cfg.rs +++ b/tests/testsuite/cfg.rs @@ -744,14 +744,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 - --> 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 @@ -787,19 +780,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 +[ERROR] unexpected `cfg` condition name: 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(); } @@ -832,17 +821,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 +[ERROR] unexpected `cfg` condition name: 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(); }