refactor: Add Copy implementation to Rule (#3556)

This commit is contained in:
Micha Reiser 2023-03-16 17:50:18 +01:00 committed by GitHub
parent aa51ecedc5
commit eff84442bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
59 changed files with 835 additions and 965 deletions

View file

@ -167,7 +167,7 @@ pub fn map_codes(func: &ItemFn) -> syn::Result<TokenStream> {
});
#[allow(clippy::type_complexity)]
let mut rule_to_codes: HashMap<&Path, Vec<(&Ident, &String, &Vec<Attribute>)>> = HashMap::new();
let mut rule_to_codes: HashMap<&Path, Vec<(&Ident, &str, &Vec<Attribute>)>> = HashMap::new();
let mut linter_code_for_rule_match_arms = quote!();
for (linter, map) in &linters {
@ -227,7 +227,7 @@ pub fn map_codes(func: &ItemFn) -> syn::Result<TokenStream> {
}
impl crate::registry::Linter {
pub fn code_for_rule(&self, rule: &Rule) -> Option<&'static str> {
pub fn code_for_rule(&self, rule: Rule) -> Option<&'static str> {
match (self, rule) {
#linter_code_for_rule_match_arms
_ => None,

View file

@ -24,8 +24,7 @@ pub fn register_rules(input: &Input) -> proc_macro2::TokenStream {
rule_explanation_match_arms.extend(quote! {#(#attr)* Self::#name => #path::explanation(),});
// Enable conversion from `DiagnosticKind` to `Rule`.
from_impls_for_diagnostic_kind
.extend(quote! {#(#attr)* stringify!(#name) => &Rule::#name,});
from_impls_for_diagnostic_kind.extend(quote! {#(#attr)* stringify!(#name) => Rule::#name,});
}
quote! {
@ -34,6 +33,7 @@ pub fn register_rules(input: &Input) -> proc_macro2::TokenStream {
Debug,
PartialEq,
Eq,
Copy,
Clone,
Hash,
PartialOrd,
@ -57,17 +57,13 @@ pub fn register_rules(input: &Input) -> proc_macro2::TokenStream {
}
/// Returns the autofix status of this rule.
pub fn autofixable(&self) -> Option<ruff_diagnostics::AutofixKind> {
pub const fn autofixable(&self) -> Option<ruff_diagnostics::AutofixKind> {
match self { #rule_autofixable_match_arms }
}
}
pub trait AsRule {
fn rule(&self) -> &'static Rule;
}
impl AsRule for ruff_diagnostics::DiagnosticKind {
fn rule(&self) -> &'static Rule {
fn rule(&self) -> Rule {
match self.name.as_str() {
#from_impls_for_diagnostic_kind
_ => unreachable!("invalid rule name: {}", self.name),