From efa7e19da1106510bd6230ca1be59e33484a03d5 Mon Sep 17 00:00:00 2001 From: Urgau Date: Fri, 27 Sep 2024 15:57:25 +0200 Subject: [PATCH] Respect `[lints.rust.unexpected_cfgs]` lint level --- src/cargo/util/lints.rs | 22 ++++++++++++++++++++-- tests/testsuite/cfg.rs | 20 +++++--------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/cargo/util/lints.rs b/src/cargo/util/lints.rs index 662eadfafcb8..40b7c4322447 100644 --- a/src/cargo/util/lints.rs +++ b/src/cargo/util/lints.rs @@ -889,6 +889,19 @@ pub fn unexpected_target_cfgs( error_count: &mut usize, gctx: &GlobalContext, ) -> CargoResult<()> { + // FIXME: This method is broken is several ways, it doesn't take into account + // `rustc` flags 'via `RUSTFLAGS`), nor the possible lints groups, ... + fn get_unexpected_cfgs_lint_level(pkg: &Package) -> Option { + if let Ok(Some(lints)) = pkg.manifest().normalized_toml().normalized_lints() { + if let Some(rust_lints) = lints.get("rust") { + if let Some(unexpected_cfgs) = rust_lints.get("unexpected_cfgs") { + return Some(unexpected_cfgs.level().into()); + } + } + } + None + } + fn warn_on_unexpected_cfgs( gctx: &GlobalContext, check_cfg: &CheckCfg, @@ -946,6 +959,12 @@ pub fn unexpected_target_cfgs( }) } + let lint_level = get_unexpected_cfgs_lint_level(pkg).unwrap_or(LintLevel::Warn); + + 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`, wee should // still pass the actual requested targets instead of an empty array so that the @@ -976,8 +995,7 @@ pub fn unexpected_target_cfgs( gctx, &global_check_cfg, cfg_expr, - // FIXME: We should get the lint level from `[lints.rust.unexpected_cfgs]` - LintLevel::Warn, + lint_level, error_count, Some(path), ".dependencies", diff --git a/tests/testsuite/cfg.rs b/tests/testsuite/cfg.rs index 7a8ecf03f309..f057571de18d 100644 --- a/tests/testsuite/cfg.rs +++ b/tests/testsuite/cfg.rs @@ -659,9 +659,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] [ROOT]/foo/Cargo.toml: unexpected `cfg` condition name: `foo` in `[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 @@ -697,14 +695,10 @@ 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] [ROOT]/foo/Cargo.toml: unexpected `cfg` condition name: `foo` in `[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 +[ERROR] [ROOT]/foo/Cargo.toml: unexpected `cfg` condition name: `foo` in `[target.'cfg(foo)'.dependencies]` "#]]) - // FIXME: this test should fail - // .with_status(101) + .with_status(101) .run(); } @@ -737,12 +731,11 @@ 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] [ROOT]/foo/Cargo.toml: unexpected `cfg` condition name: `foo` in `[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 +[ERROR] [ROOT]/foo/Cargo.toml: unexpected `cfg` condition name: `foo` in `[target.'cfg(foo)'.dependencies]` "#]]) + // nor should we error out because of the level="deny" + .with_status(101) .run(); } @@ -790,9 +783,6 @@ fn no_unexpected_cfgs_target() { p.cargo("check -Zcargo-lints") .masquerade_as_nightly_cargo(&["requires -Zcargo-lints"]) .with_stderr_data(str![[r#" -[LOCKING] 1 package to latest compatible version -[CHECKING] b v0.0.1 ([ROOT]/foo/b) -[CHECKING] a v0.0.1 ([ROOT]/foo) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s "#]])