Skip to content

Commit

Permalink
feat(cargo-lints): Add lint groups to verification
Browse files Browse the repository at this point in the history
  • Loading branch information
Muscraft committed Apr 29, 2024
1 parent fbb23ff commit f3a1a6a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
45 changes: 41 additions & 4 deletions src/cargo/util/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::ops::Range;
use std::path::Path;
use toml_edit::ImDocument;

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

pub fn verify_lints(
Expand All @@ -33,11 +34,20 @@ pub fn verify_lints(
.keys()
.chain(ws_lints.map(|l| l.keys()).unwrap_or_default())
{
if let Some(lint) = LINTS.iter().find(|l| l.name == lint_name) {
let (_, reason) = lint.level(pkg_lints, ws_lints, manifest.edition());
if let Some(feature_gate) = lint.feature_gate {
if let Some((name, default_level, edition_lint_opts, feature_gate)) =
find_lint_or_group(lint_name)
{
let (_, reason, _) = level_priority(
name,
*default_level,
*edition_lint_opts,
pkg_lints,
ws_lints,
manifest.edition(),
);
if let Some(feature_gate) = feature_gate {
feature_gated_lint(
lint.name,
name,
feature_gate,
reason,
manifest,
Expand All @@ -60,6 +70,33 @@ pub fn verify_lints(
}
}

fn find_lint_or_group<'a>(
name: &str,
) -> Option<(
&'static str,
&LintLevel,
&Option<(Edition, LintLevel)>,
&Option<&'static Feature>,
)> {
if let Some(lint) = LINTS.iter().find(|l| l.name == name) {
Some((
lint.name,
&lint.default_level,
&lint.edition_lint_opts,
&lint.feature_gate,
))
} else if let Some(group) = LINT_GROUPS.iter().find(|g| g.name == name) {
Some((
group.name,
&group.default_level,
&group.edition_lint_opts,
&group.feature_gate,
))
} else {
None
}
}

fn feature_gated_lint(
lint_name: &str,
feature_gate: &Feature,
Expand Down
15 changes: 14 additions & 1 deletion tests/testsuite/lints_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,20 @@ note: `cargo::im-a-teapot` was inherited
| ----------------
|
= help: consider adding `cargo-features = [\"test-dummy-unstable\"]` to the top of the manifest
error: encountered 1 errors(s) while verifying lints
error: use of unstable lint `test-dummy-unstable`
--> Cargo.toml:7:1
|
7 | test-dummy-unstable = { level = \"forbid\", priority = -1 }
| ^^^^^^^^^^^^^^^^^^^ this is behind `test-dummy-unstable`, which is not enabled
|
note: `cargo::test-dummy-unstable` was inherited
--> foo/Cargo.toml:9:1
|
9 | workspace = true
| ----------------
|
= help: consider adding `cargo-features = [\"test-dummy-unstable\"]` to the top of the manifest
error: encountered 2 errors(s) while verifying lints
",
)
.run();
Expand Down

0 comments on commit f3a1a6a

Please sign in to comment.