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 26, 2024
1 parent 889f5ab commit 0b21cd2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 23 deletions.
64 changes: 42 additions & 22 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 @@ -29,20 +30,41 @@ pub fn verify_lints(
let manifest_path = rel_cwd_manifest_path(path, gctx);
let ws_path = rel_cwd_manifest_path(ws_path, gctx);

let iter = LINTS
.iter()
.map(|l| (l.name, l.default_level, l.edition_lint_opts, l.feature_gate))
.chain(
LINT_GROUPS
.iter()
.map(|g| (g.name, g.default_level, g.edition_lint_opts, g.feature_gate)),
)
.collect::<Vec<_>>();
for lint_name in pkg_lints.keys().chain(ws_lints.keys()) {
if let Some(lint) = LINTS.iter().find(|l| l.name == lint_name) {
let (_, reason) = lint.level(pkg_lints, ws_lints, manifest.edition());
feature_gated_lint(
lint,
reason,
manifest,
&manifest_path,
ws_contents,
ws_document,
&ws_path,
&mut error_count,
gctx,
)?;
if let Some((name, default_level, edition_lint_opts, feature_gate)) =
iter.iter().find(|(n, _, _, _)| n == &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(
name,
feature_gate,
reason,
manifest,
&manifest_path,
ws_contents,
ws_document,
&ws_path,
&mut error_count,
gctx,
)?;
}
}
}
if error_count > 0 {
Expand All @@ -55,7 +77,8 @@ pub fn verify_lints(
}

fn feature_gated_lint(
lint: &Lint,
lint_name: &str,
feature_gate: &Feature,
reason: LintLevelReason,
manifest: &Manifest,
manifest_path: &str,
Expand All @@ -65,14 +88,11 @@ fn feature_gated_lint(
error_count: &mut usize,
gctx: &GlobalContext,
) -> CargoResult<()> {
if lint
.feature_gate
.map_or(false, |f| !manifest.unstable_features().is_enabled(f))
{
let dash_name = lint.name.replace("_", "-");
if !manifest.unstable_features().is_enabled(feature_gate) {
let dash_name = lint_name.replace("_", "-");
let title = "verification of lint failed";
let label = "use of unstable lint that has not been enabled";
let second_title = format!("`cargo::{}` was inherited", lint.name);
let second_title = format!("`cargo::{}` was inherited", lint_name);
let message = match reason {
LintLevelReason::Package => {
let span = get_span(
Expand All @@ -82,7 +102,7 @@ fn feature_gated_lint(
)
.or(get_span(
manifest.document(),
&["lints", "cargo", lint.name],
&["lints", "cargo", lint_name],
false,
))
.unwrap();
Expand All @@ -103,7 +123,7 @@ fn feature_gated_lint(
)
.or(get_span(
ws_document,
&["workspace", "lints", "cargo", lint.name],
&["workspace", "lints", "cargo", lint_name],
false,
))
.unwrap();
Expand Down
14 changes: 13 additions & 1 deletion tests/testsuite/lints_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,19 @@ note: `cargo::im_a_teapot` was inherited
9 | workspace = true
| ---------
|
error: encountered 1 errors(s) while verifying lints
error: verification of lint failed
--> Cargo.toml:7:1
|
7 | test-dummy-unstable = { level = \"forbid\", priority = -1 }
| ^^^^^^^^^^^^^^^^^^^ use of unstable lint that has not been enabled
|
note: `cargo::test_dummy_unstable` was inherited
--> foo/Cargo.toml:9:1
|
9 | workspace = true
| ---------
|
error: encountered 2 errors(s) while verifying lints
",
)
.run();
Expand Down

0 comments on commit 0b21cd2

Please sign in to comment.