Skip to content

Commit 0b21cd2

Browse files
committed
feat(cargo-lints): Add lint groups to verification
1 parent 889f5ab commit 0b21cd2

File tree

2 files changed

+55
-23
lines changed

2 files changed

+55
-23
lines changed

src/cargo/util/lints.rs

Lines changed: 42 additions & 22 deletions
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 verify_lints(
@@ -29,20 +30,41 @@ pub fn verify_lints(
2930
let manifest_path = rel_cwd_manifest_path(path, gctx);
3031
let ws_path = rel_cwd_manifest_path(ws_path, gctx);
3132

33+
let iter = LINTS
34+
.iter()
35+
.map(|l| (l.name, l.default_level, l.edition_lint_opts, l.feature_gate))
36+
.chain(
37+
LINT_GROUPS
38+
.iter()
39+
.map(|g| (g.name, g.default_level, g.edition_lint_opts, g.feature_gate)),
40+
)
41+
.collect::<Vec<_>>();
3242
for lint_name in pkg_lints.keys().chain(ws_lints.keys()) {
33-
if let Some(lint) = LINTS.iter().find(|l| l.name == lint_name) {
34-
let (_, reason) = lint.level(pkg_lints, ws_lints, manifest.edition());
35-
feature_gated_lint(
36-
lint,
37-
reason,
38-
manifest,
39-
&manifest_path,
40-
ws_contents,
41-
ws_document,
42-
&ws_path,
43-
&mut error_count,
44-
gctx,
45-
)?;
43+
if let Some((name, default_level, edition_lint_opts, feature_gate)) =
44+
iter.iter().find(|(n, _, _, _)| n == &lint_name)
45+
{
46+
let (_, reason, _) = level_priority(
47+
name,
48+
*default_level,
49+
*edition_lint_opts,
50+
pkg_lints,
51+
ws_lints,
52+
manifest.edition(),
53+
);
54+
if let Some(feature_gate) = feature_gate {
55+
feature_gated_lint(
56+
name,
57+
feature_gate,
58+
reason,
59+
manifest,
60+
&manifest_path,
61+
ws_contents,
62+
ws_document,
63+
&ws_path,
64+
&mut error_count,
65+
gctx,
66+
)?;
67+
}
4668
}
4769
}
4870
if error_count > 0 {
@@ -55,7 +77,8 @@ pub fn verify_lints(
5577
}
5678

5779
fn feature_gated_lint(
58-
lint: &Lint,
80+
lint_name: &str,
81+
feature_gate: &Feature,
5982
reason: LintLevelReason,
6083
manifest: &Manifest,
6184
manifest_path: &str,
@@ -65,14 +88,11 @@ fn feature_gated_lint(
6588
error_count: &mut usize,
6689
gctx: &GlobalContext,
6790
) -> CargoResult<()> {
68-
if lint
69-
.feature_gate
70-
.map_or(false, |f| !manifest.unstable_features().is_enabled(f))
71-
{
72-
let dash_name = lint.name.replace("_", "-");
91+
if !manifest.unstable_features().is_enabled(feature_gate) {
92+
let dash_name = lint_name.replace("_", "-");
7393
let title = "verification of lint failed";
7494
let label = "use of unstable lint that has not been enabled";
75-
let second_title = format!("`cargo::{}` was inherited", lint.name);
95+
let second_title = format!("`cargo::{}` was inherited", lint_name);
7696
let message = match reason {
7797
LintLevelReason::Package => {
7898
let span = get_span(
@@ -82,7 +102,7 @@ fn feature_gated_lint(
82102
)
83103
.or(get_span(
84104
manifest.document(),
85-
&["lints", "cargo", lint.name],
105+
&["lints", "cargo", lint_name],
86106
false,
87107
))
88108
.unwrap();
@@ -103,7 +123,7 @@ fn feature_gated_lint(
103123
)
104124
.or(get_span(
105125
ws_document,
106-
&["workspace", "lints", "cargo", lint.name],
126+
&["workspace", "lints", "cargo", lint_name],
107127
false,
108128
))
109129
.unwrap();

tests/testsuite/lints_table.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,19 @@ note: `cargo::im_a_teapot` was inherited
10661066
9 | workspace = true
10671067
| ---------
10681068
|
1069-
error: encountered 1 errors(s) while verifying lints
1069+
error: verification of lint failed
1070+
--> Cargo.toml:7:1
1071+
|
1072+
7 | test-dummy-unstable = { level = \"forbid\", priority = -1 }
1073+
| ^^^^^^^^^^^^^^^^^^^ use of unstable lint that has not been enabled
1074+
|
1075+
note: `cargo::test_dummy_unstable` was inherited
1076+
--> foo/Cargo.toml:9:1
1077+
|
1078+
9 | workspace = true
1079+
| ---------
1080+
|
1081+
error: encountered 2 errors(s) while verifying lints
10701082
",
10711083
)
10721084
.run();

0 commit comments

Comments
 (0)