perf(linter): use usize for RuleEnum hash (#3336)

This commit is contained in:
Boshen 2024-05-18 14:12:32 +08:00 committed by GitHub
parent 8383b6e046
commit 8388c7bd21
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 4 deletions

View file

@ -250,7 +250,7 @@ impl LintOptions {
let mut rules = rules.into_iter().collect::<Vec<_>>();
// for stable diagnostics output ordering
rules.sort_unstable_by_key(RuleEnum::name);
rules.sort_unstable_by_key(RuleEnum::id);
Ok((rules, config.unwrap_or_default()))
}

View file

@ -60,6 +60,7 @@ pub fn declare_all_lint_rules(metadata: AllLintRulesMeta) -> TokenStream {
.collect::<Vec<_>>()
.join("/")
});
let ids = rules.iter().enumerate().map(|(i, _)| i).collect::<Vec<_>>();
let expanded = quote! {
#(#use_stmts)*
@ -74,6 +75,12 @@ pub fn declare_all_lint_rules(metadata: AllLintRulesMeta) -> TokenStream {
}
impl RuleEnum {
pub fn id(&self) -> usize {
match self {
#(Self::#struct_names(_) => #ids),*
}
}
pub fn name(&self) -> &'static str {
match self {
#(Self::#struct_names(_) => #struct_names::NAME),*
@ -127,13 +134,13 @@ pub fn declare_all_lint_rules(metadata: AllLintRulesMeta) -> TokenStream {
impl std::hash::Hash for RuleEnum {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.name().hash(state);
self.id().hash(state);
}
}
impl PartialEq for RuleEnum {
fn eq(&self, other: &Self) -> bool {
self.name() == other.name()
self.id() == other.id()
}
}
@ -141,7 +148,7 @@ pub fn declare_all_lint_rules(metadata: AllLintRulesMeta) -> TokenStream {
impl Ord for RuleEnum {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.name().cmp(&other.name())
self.id().cmp(&other.id())
}
}