Skip to content

Commit 11d6013

Browse files
committed
fix(cargo-lints): Respect Forbid lint level
1 parent 2d40a47 commit 11d6013

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

crates/cargo-util-schemas/src/manifest/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,7 @@ pub struct TomlLintConfig {
15061506
pub priority: i8,
15071507
}
15081508

1509-
#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
1509+
#[derive(Serialize, Deserialize, Debug, Copy, Clone, Eq, PartialEq)]
15101510
#[serde(rename_all = "kebab-case")]
15111511
pub enum TomlLintLevel {
15121512
Forbid,

src/cargo/util/lints.rs

+28-7
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ pub struct LintGroup {
6868
pub edition_lint_opts: Option<(Edition, LintLevel)>,
6969
}
7070

71+
const TEST_DUMMY_UNSTABLE: LintGroup = LintGroup {
72+
name: "test_dummy_unstable",
73+
desc: "test_dummy_unstable is meant to only be used in tests",
74+
default_level: LintLevel::Allow,
75+
edition_lint_opts: None,
76+
};
77+
7178
#[derive(Copy, Clone, Debug)]
7279
pub struct Lint {
7380
pub name: &'static str,
@@ -79,23 +86,37 @@ pub struct Lint {
7986

8087
impl Lint {
8188
pub fn level(&self, lints: &TomlToolLints, edition: Edition) -> LintLevel {
89+
let edition_level = self
90+
.edition_lint_opts
91+
.filter(|(e, _)| edition >= *e)
92+
.map(|(_, l)| l);
93+
94+
if self.default_level == LintLevel::Forbid || edition_level == Some(LintLevel::Forbid) {
95+
return LintLevel::Forbid;
96+
}
97+
8298
let level = self
8399
.groups
84100
.iter()
85101
.map(|g| g.name)
86102
.chain(std::iter::once(self.name))
87103
.filter_map(|n| lints.get(n).map(|l| (n, l)))
88-
.max_by_key(|(n, l)| (l.priority(), std::cmp::Reverse(*n)));
104+
.max_by_key(|(n, l)| {
105+
(
106+
l.level() == TomlLintLevel::Forbid,
107+
l.priority(),
108+
std::cmp::Reverse(*n),
109+
)
110+
});
89111

90112
match level {
91113
Some((_, toml_lint)) => toml_lint.level().into(),
92114
None => {
93-
if let Some((lint_edition, lint_level)) = self.edition_lint_opts {
94-
if edition >= lint_edition {
95-
return lint_level;
96-
}
115+
if let Some(level) = edition_level {
116+
level
117+
} else {
118+
self.default_level
97119
}
98-
self.default_level
99120
}
100121
}
101122
}
@@ -145,7 +166,7 @@ impl From<TomlLintLevel> for LintLevel {
145166
const IM_A_TEAPOT: Lint = Lint {
146167
name: "im_a_teapot",
147168
desc: "`im_a_teapot` is specified",
148-
groups: &[],
169+
groups: &[TEST_DUMMY_UNSTABLE],
149170
default_level: LintLevel::Allow,
150171
edition_lint_opts: None,
151172
};

tests/testsuite/lints_table.rs

+40
Original file line numberDiff line numberDiff line change
@@ -899,3 +899,43 @@ warning: `im_a_teapot` is specified
899899
)
900900
.run();
901901
}
902+
903+
#[cargo_test]
904+
fn forbid_not_overridden() {
905+
let p = project()
906+
.file(
907+
"Cargo.toml",
908+
r#"
909+
cargo-features = ["test-dummy-unstable"]
910+
911+
[package]
912+
name = "foo"
913+
version = "0.0.1"
914+
edition = "2015"
915+
authors = []
916+
im-a-teapot = true
917+
918+
[lints.cargo]
919+
im-a-teapot = { level = "warn", priority = 10 }
920+
test-dummy-unstable = { level = "forbid", priority = -1 }
921+
"#,
922+
)
923+
.file("src/lib.rs", "")
924+
.build();
925+
926+
p.cargo("check -Zcargo-lints")
927+
.masquerade_as_nightly_cargo(&["cargo-lints", "test-dummy-unstable"])
928+
.with_status(101)
929+
.with_stderr(
930+
"\
931+
error: `im_a_teapot` is specified
932+
--> Cargo.toml:9:1
933+
|
934+
9 | im-a-teapot = true
935+
| ^^^^^^^^^^^^^^^^^^
936+
|
937+
= note: `cargo::im_a_teapot` is set to `forbid`
938+
",
939+
)
940+
.run();
941+
}

0 commit comments

Comments
 (0)