Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cargo/diagnostics/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub struct LintLevelProduct {
pub source: LintLevelSource,
}

#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum LintLevel {
Allow,
Warn,
Expand Down
1 change: 1 addition & 0 deletions src/cargo/diagnostics/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::diagnostics::LintLevel;
use crate::diagnostics::LintLevelProduct;
use crate::diagnostics::ManifestFor;

#[derive(Clone)]
pub enum ParsePassRule<'r> {
DiagnosticManifest {
rule: FnDiagnosticManifest,
Expand Down
143 changes: 96 additions & 47 deletions src/cargo/diagnostics/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,77 +30,79 @@ pub const PARSE_PASS_RULES: &[ParsePassRule<'static>] = &[
ParsePassRule::DiagnosticManifest {
rule: missing_lints_features::diagnose_manifest,
},
ParsePassRule::LintManifest {
rule: text_direction_codepoint_in_comment::lint_manifest,
lint: text_direction_codepoint_in_comment::LINT,
},
ParsePassRule::LintManifest {
rule: text_direction_codepoint_in_literal::lint_manifest,
lint: text_direction_codepoint_in_literal::LINT,
},
ParsePassRule::LintManifest {
rule: unknown_lints::lint_manifest,
lint: unknown_lints::LINT,
},
ParsePassRule::LintWorkspace {
rule: unused_workspace_package_fields::lint_workspace,
lint: unused_workspace_package_fields::LINT,
rule: blanket_hint_mostly_unused::lint_workspace,
lint: blanket_hint_mostly_unused::LINT,
},
ParsePassRule::LintWorkspace {
rule: unused_workspace_dependencies::lint_workspace,
lint: unused_workspace_dependencies::LINT,
},
ParsePassRule::LintWorkspace {
rule: implicit_minimum_version_req::lint_workspace,
lint: implicit_minimum_version_req::LINT,
},
ParsePassRule::LintManifest {
rule: text_direction_codepoint_in_comment::lint_manifest,
lint: text_direction_codepoint_in_comment::LINT,
},
ParsePassRule::LintManifest {
rule: text_direction_codepoint_in_literal::lint_manifest,
lint: text_direction_codepoint_in_literal::LINT,
rule: unused_workspace_package_fields::lint_workspace,
lint: unused_workspace_package_fields::LINT,
},
ParsePassRule::LintWorkspace {
rule: blanket_hint_mostly_unused::lint_workspace,
lint: blanket_hint_mostly_unused::LINT,
rule: implicit_minimum_version_req::lint_workspace,
lint: implicit_minimum_version_req::LINT,
},
// `warn`
ParsePassRule::LintPackage {
rule: im_a_teapot::lint_package,
lint: im_a_teapot::LINT,
rule: missing_lints_inheritance::lint_package,
lint: missing_lints_inheritance::LINT,
},
ParsePassRule::LintPackage {
rule: implicit_minimum_version_req::lint_package,
lint: implicit_minimum_version_req::LINT,
rule: non_kebab_case_bins::lint_package,
lint: non_kebab_case_bins::LINT,
},
ParsePassRule::LintPackage {
rule: non_kebab_case_packages::lint_package,
lint: non_kebab_case_packages::LINT,
rule: redundant_homepage::lint_package,
lint: redundant_homepage::LINT,
},
ParsePassRule::LintPackage {
rule: non_snake_case_packages::lint_package,
lint: non_snake_case_packages::LINT,
rule: redundant_readme::lint_package,
lint: redundant_readme::LINT,
},
ParsePassRule::LintPackage {
rule: non_kebab_case_bins::lint_package,
lint: non_kebab_case_bins::LINT,
rule: unused_dependencies::lint_package,
lint: unused_dependencies::LINT,
},
ParsePassRule::LintPackage {
rule: non_kebab_case_features::lint_package,
lint: non_kebab_case_features::LINT,
rule: im_a_teapot::lint_package,
lint: im_a_teapot::LINT,
},
// `allow`
ParsePassRule::LintPackage {
rule: non_snake_case_features::lint_package,
lint: non_snake_case_features::LINT,
rule: implicit_minimum_version_req::lint_package,
lint: implicit_minimum_version_req::LINT,
},
ParsePassRule::LintPackage {
rule: unused_dependencies::lint_package,
lint: unused_dependencies::LINT,
rule: non_kebab_case_features::lint_package,
lint: non_kebab_case_features::LINT,
},
ParsePassRule::LintPackage {
rule: redundant_readme::lint_package,
lint: redundant_readme::LINT,
rule: non_kebab_case_packages::lint_package,
lint: non_kebab_case_packages::LINT,
},
ParsePassRule::LintPackage {
rule: redundant_homepage::lint_package,
lint: redundant_homepage::LINT,
rule: non_snake_case_features::lint_package,
lint: non_snake_case_features::LINT,
},
ParsePassRule::LintPackage {
rule: missing_lints_inheritance::lint_package,
lint: missing_lints_inheritance::LINT,
rule: non_snake_case_packages::lint_package,
lint: non_snake_case_packages::LINT,
},
];

Expand Down Expand Up @@ -289,19 +291,52 @@ mod tests {
snapbox::assert_data_eq!(actual.to_debug(), expected.to_debug());
}

#[test]
fn ensure_sorted_parse_pass_rules() {
let actual = parse_pass_rule_names(PARSE_PASS_RULES);
let mut ordered_parse_pass = PARSE_PASS_RULES.to_vec();
ordered_parse_pass.sort_by_key(|rule| {
let (lint, scope) = match rule {
ParsePassRule::DiagnosticManifest { .. } => {
let scope = 0;
(None, scope)
}
ParsePassRule::LintManifest { lint, .. } => {
let scope = 0;
(Some(lint), scope)
}
ParsePassRule::DiagnosticWorkspace { .. } => {
let scope = 1;
(None, scope)
}
ParsePassRule::LintWorkspace { lint, .. } => {
let scope = 1;
(Some(lint), scope)
}
ParsePassRule::DiagnosticPackage { .. } => {
let scope = 2;
(None, scope)
}
ParsePassRule::LintPackage { lint, .. } => {
let scope = 2;
(Some(lint), scope)
}
};
let is_lint = lint.is_some();
let level = lint.map(|l| std::cmp::Reverse(l.primary_group.default_level));
let name = lint.map(|l| l.name);
(is_lint, scope, level, name)
});
let expected = parse_pass_rule_names(&ordered_parse_pass);

println!("`PARSE_PASS_RULES` sort order:");
snapbox::assert_data_eq!(actual.join("\n"), expected.join("\n"));
}

#[test]
fn ensure_parse_passed_in_lints() {
let parse_pass_lint_names = PARSE_PASS_RULES
.iter()
.filter_map(|rule| match rule {
ParsePassRule::DiagnosticManifest { .. }
| ParsePassRule::DiagnosticWorkspace { .. }
| ParsePassRule::DiagnosticPackage { .. } => None,
ParsePassRule::LintManifest { lint, .. }
| ParsePassRule::LintWorkspace { lint, .. }
| ParsePassRule::LintPackage { lint, .. } => Some(lint.name),
})
.collect::<std::collections::HashSet<_>>();
let parse_pass_lint_names =
HashSet::from_iter(parse_pass_rule_names(PARSE_PASS_RULES).into_iter());
let lint_names = LINTS
.iter()
.map(|l| l.name)
Expand All @@ -322,6 +357,20 @@ mod tests {
);
}

fn parse_pass_rule_names(rules: &[ParsePassRule<'_>]) -> Vec<&'static str> {
rules
.iter()
.filter_map(|rule| match rule {
ParsePassRule::DiagnosticManifest { .. }
| ParsePassRule::DiagnosticWorkspace { .. }
| ParsePassRule::DiagnosticPackage { .. } => None,
ParsePassRule::LintManifest { lint, .. }
| ParsePassRule::LintWorkspace { lint, .. }
| ParsePassRule::LintPackage { lint, .. } => Some(lint.name),
})
.collect()
}

#[test]
fn ensure_updated_lints() {
let dir = snapbox::utils::current_dir!();
Expand Down
16 changes: 8 additions & 8 deletions tests/testsuite/lints/non_kebab_case_packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,6 @@ fn main() {}"#,
.with_stderr_data(str![[r#"
[WARNING] `package.edition` is unspecified, defaulting to the latest edition (currently `[..]`)
[HELP] to pin the edition, run `cargo fix --manifest-path [ROOT]/foo/foo_bar`
[WARNING] packages should have a kebab-case name
--> foo_bar
= [NOTE] `cargo::non_kebab_case_packages` is set to `warn` in `[lints]`
[HELP] to change the package name to kebab case, convert the file stem
|
1 - [ROOT]/foo/foo_bar
1 + [ROOT]/foo/foo-bar
|
[WARNING] binaries should have a kebab-case name
|
1 | [ROOT]/home/.cargo/build/[HASH]/target/.../foo_bar[EXE]
Expand All @@ -80,6 +72,14 @@ fn main() {}"#,
1 - foo_bar
1 + foo-bar
|
[WARNING] packages should have a kebab-case name
--> foo_bar
= [NOTE] `cargo::non_kebab_case_packages` is set to `warn` in `[lints]`
[HELP] to change the package name to kebab case, convert the file stem
|
1 - [ROOT]/foo/foo_bar
1 + [ROOT]/foo/foo-bar
|
[WARNING] `foo_bar` (manifest) generated 2 warnings

"#]])
Expand Down
Loading