Skip to content

Commit efa7e19

Browse files
committed
Respect [lints.rust.unexpected_cfgs] lint level
1 parent cc5b088 commit efa7e19

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

src/cargo/util/lints.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,19 @@ pub fn unexpected_target_cfgs(
889889
error_count: &mut usize,
890890
gctx: &GlobalContext,
891891
) -> CargoResult<()> {
892+
// FIXME: This method is broken is several ways, it doesn't take into account
893+
// `rustc` flags 'via `RUSTFLAGS`), nor the possible lints groups, ...
894+
fn get_unexpected_cfgs_lint_level(pkg: &Package) -> Option<LintLevel> {
895+
if let Ok(Some(lints)) = pkg.manifest().normalized_toml().normalized_lints() {
896+
if let Some(rust_lints) = lints.get("rust") {
897+
if let Some(unexpected_cfgs) = rust_lints.get("unexpected_cfgs") {
898+
return Some(unexpected_cfgs.level().into());
899+
}
900+
}
901+
}
902+
None
903+
}
904+
892905
fn warn_on_unexpected_cfgs(
893906
gctx: &GlobalContext,
894907
check_cfg: &CheckCfg,
@@ -946,6 +959,12 @@ pub fn unexpected_target_cfgs(
946959
})
947960
}
948961

962+
let lint_level = get_unexpected_cfgs_lint_level(pkg).unwrap_or(LintLevel::Warn);
963+
964+
if lint_level == LintLevel::Allow {
965+
return Ok(());
966+
}
967+
949968
let rustc = gctx.load_global_rustc(Some(ws))?;
950969
// FIXME: While it doesn't doesn't really matter for `--print=check-cfg`, wee should
951970
// still pass the actual requested targets instead of an empty array so that the
@@ -976,8 +995,7 @@ pub fn unexpected_target_cfgs(
976995
gctx,
977996
&global_check_cfg,
978997
cfg_expr,
979-
// FIXME: We should get the lint level from `[lints.rust.unexpected_cfgs]`
980-
LintLevel::Warn,
998+
lint_level,
981999
error_count,
9821000
Some(path),
9831001
".dependencies",

tests/testsuite/cfg.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -659,9 +659,7 @@ fn unexpected_cfgs_target_lint_level_allow() {
659659

660660
p.cargo("check -Zcargo-lints -Zcheck-target-cfgs")
661661
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
662-
// FIXME: We shouldn't warn any target cfgs because of the level="allow"
663662
.with_stderr_data(str![[r#"
664-
[WARNING] [ROOT]/foo/Cargo.toml: unexpected `cfg` condition name: `foo` in `[target.'cfg(foo)'.dependencies]`
665663
[LOCKING] 1 package to latest compatible version
666664
[CHECKING] a v0.0.1 ([ROOT]/foo)
667665
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
@@ -697,14 +695,10 @@ fn unexpected_cfgs_target_lint_level_deny() {
697695
p.cargo("check -Zcargo-lints -Zcheck-target-cfgs")
698696
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
699697
.with_stderr_data(str![[r#"
700-
[WARNING] [ROOT]/foo/Cargo.toml: unexpected `cfg` condition name: `foo` in `[target.'cfg(foo)'.dependencies]`
701-
[LOCKING] 1 package to latest compatible version
702-
[CHECKING] a v0.0.1 ([ROOT]/foo)
703-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
698+
[ERROR] [ROOT]/foo/Cargo.toml: unexpected `cfg` condition name: `foo` in `[target.'cfg(foo)'.dependencies]`
704699
705700
"#]])
706-
// FIXME: this test should fail
707-
// .with_status(101)
701+
.with_status(101)
708702
.run();
709703
}
710704

@@ -737,12 +731,11 @@ fn unexpected_cfgs_target_cfg_any() {
737731
.masquerade_as_nightly_cargo(&["requires -Zcheck-target-cfgs"])
738732
// FIXME: We shouldn't be linting `cfg(foo)` because of the `cfg(any())`
739733
.with_stderr_data(str![[r#"
740-
[WARNING] [ROOT]/foo/Cargo.toml: unexpected `cfg` condition name: `foo` in `[target.'cfg(foo)'.dependencies]`
741-
[LOCKING] 1 package to latest compatible version
742-
[CHECKING] a v0.0.1 ([ROOT]/foo)
743-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
734+
[ERROR] [ROOT]/foo/Cargo.toml: unexpected `cfg` condition name: `foo` in `[target.'cfg(foo)'.dependencies]`
744735
745736
"#]])
737+
// nor should we error out because of the level="deny"
738+
.with_status(101)
746739
.run();
747740
}
748741

@@ -790,9 +783,6 @@ fn no_unexpected_cfgs_target() {
790783
p.cargo("check -Zcargo-lints")
791784
.masquerade_as_nightly_cargo(&["requires -Zcargo-lints"])
792785
.with_stderr_data(str![[r#"
793-
[LOCKING] 1 package to latest compatible version
794-
[CHECKING] b v0.0.1 ([ROOT]/foo/b)
795-
[CHECKING] a v0.0.1 ([ROOT]/foo)
796786
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
797787
798788
"#]])

0 commit comments

Comments
 (0)