Skip to content

Commit

Permalink
Respect [lints.rust.unexpected_cfgs] lint level
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Sep 27, 2024
1 parent cc5b088 commit efa7e19
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
22 changes: 20 additions & 2 deletions src/cargo/util/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LintLevel> {
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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
20 changes: 5 additions & 15 deletions tests/testsuite/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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
"#]])
Expand Down

0 comments on commit efa7e19

Please sign in to comment.