Skip to content

Commit 85fc017

Browse files
committed
feat(cargo-lints): Add lint groups to analysis
1 parent 6abc8a2 commit 85fc017

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

Diff for: src/cargo/util/lints.rs

+43-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::ops::Range;
1212
use std::path::Path;
1313
use toml_edit::ImDocument;
1414

15+
const LINT_GROUPS: &[LintGroup] = &[TEST_DUMMY_UNSTABLE];
1516
const LINTS: &[Lint] = &[IM_A_TEAPOT, IMPLICIT_FEATURES, UNUSED_OPTIONAL_DEPENDENCY];
1617

1718
pub fn analyze_cargo_lints_table(
@@ -33,18 +34,27 @@ pub fn analyze_cargo_lints_table(
3334
.keys()
3435
.chain(ws_lints.map(|l| l.keys()).unwrap_or_default())
3536
{
36-
if let Some(lint) = LINTS.iter().find(|l| l.name == lint_name) {
37-
let (_, reason) = lint.level(pkg_lints, ws_lints, manifest.edition());
37+
if let Some((name, default_level, edition_lint_opts, feature_gate)) =
38+
find_lint_or_group(lint_name)
39+
{
40+
let (_, reason, _) = level_priority(
41+
name,
42+
*default_level,
43+
*edition_lint_opts,
44+
pkg_lints,
45+
ws_lints,
46+
manifest.edition(),
47+
);
3848

3949
// Only run analysis on user-specified lints
4050
if !reason.is_user_specified() {
4151
continue;
4252
}
4353

4454
// Only run this on lints that are gated by a feature
45-
if let Some(feature_gate) = lint.feature_gate {
55+
if let Some(feature_gate) = feature_gate {
4656
verify_feature_enabled(
47-
lint.name,
57+
name,
4858
feature_gate,
4959
reason,
5060
manifest,
@@ -67,6 +77,33 @@ pub fn analyze_cargo_lints_table(
6777
}
6878
}
6979

80+
fn find_lint_or_group<'a>(
81+
name: &str,
82+
) -> Option<(
83+
&'static str,
84+
&LintLevel,
85+
&Option<(Edition, LintLevel)>,
86+
&Option<&'static Feature>,
87+
)> {
88+
if let Some(lint) = LINTS.iter().find(|l| l.name == name) {
89+
Some((
90+
lint.name,
91+
&lint.default_level,
92+
&lint.edition_lint_opts,
93+
&lint.feature_gate,
94+
))
95+
} else if let Some(group) = LINT_GROUPS.iter().find(|g| g.name == name) {
96+
Some((
97+
group.name,
98+
&group.default_level,
99+
&group.edition_lint_opts,
100+
&group.feature_gate,
101+
))
102+
} else {
103+
None
104+
}
105+
}
106+
70107
fn verify_feature_enabled(
71108
lint_name: &str,
72109
feature_gate: &Feature,
@@ -217,13 +254,15 @@ pub struct LintGroup {
217254
pub default_level: LintLevel,
218255
pub desc: &'static str,
219256
pub edition_lint_opts: Option<(Edition, LintLevel)>,
257+
pub feature_gate: Option<&'static Feature>,
220258
}
221259

222260
const TEST_DUMMY_UNSTABLE: LintGroup = LintGroup {
223261
name: "test_dummy_unstable",
224262
desc: "test_dummy_unstable is meant to only be used in tests",
225263
default_level: LintLevel::Allow,
226264
edition_lint_opts: None,
265+
feature_gate: Some(Feature::test_dummy_unstable()),
227266
};
228267

229268
#[derive(Copy, Clone, Debug)]

Diff for: tests/testsuite/lints_table.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,20 @@ note: `cargo::im-a-teapot` was inherited
11641164
| ----------------
11651165
|
11661166
= help: consider adding `cargo-features = [\"test-dummy-unstable\"]` to the top of the manifest
1167-
error: encountered 1 errors(s) while verifying lints
1167+
error: use of unstable lint `test-dummy-unstable`
1168+
--> Cargo.toml:7:1
1169+
|
1170+
7 | test-dummy-unstable = { level = \"forbid\", priority = -1 }
1171+
| ^^^^^^^^^^^^^^^^^^^ this is behind `test-dummy-unstable`, which is not enabled
1172+
|
1173+
note: `cargo::test-dummy-unstable` was inherited
1174+
--> foo/Cargo.toml:9:1
1175+
|
1176+
9 | workspace = true
1177+
| ----------------
1178+
|
1179+
= help: consider adding `cargo-features = [\"test-dummy-unstable\"]` to the top of the manifest
1180+
error: encountered 2 errors(s) while verifying lints
11681181
",
11691182
)
11701183
.run();

0 commit comments

Comments
 (0)