diff --git a/crates/ruff_dev/src/generate_rules_table.rs b/crates/ruff_dev/src/generate_rules_table.rs index 3b775efcaa..8f1fec7e05 100644 --- a/crates/ruff_dev/src/generate_rules_table.rs +++ b/crates/ruff_dev/src/generate_rules_table.rs @@ -23,7 +23,7 @@ pub struct Args { pub(crate) dry_run: bool, } -fn generate_table(table_out: &mut String, rules: impl IntoIterator) { +fn generate_table(table_out: &mut String, rules: impl IntoIterator, linter: &Linter) { table_out.push_str("| Code | Name | Message | Fix |"); table_out.push('\n'); table_out.push_str("| ---- | ---- | ------- | --- |"); @@ -38,8 +38,9 @@ fn generate_table(table_out: &mut String, rules: impl IntoIterator) #[allow(clippy::or_fun_call)] table_out.push_str(&format!( - "| {} | {} | {} | {} |", - rule.noqa_code(), + "| {}{} | {} | {} | {} |", + linter.common_prefix(), + linter.code_for_rule(&rule).unwrap(), rule.explanation() .is_some() .then_some(format_args!("[{rule_name}]({URL_PREFIX}/{rule_name}/)",)) @@ -111,10 +112,10 @@ pub fn main(args: &Args) -> Result<()> { )); table_out.push('\n'); table_out.push('\n'); - generate_table(&mut table_out, prefix); + generate_table(&mut table_out, prefix, &linter); } } else { - generate_table(&mut table_out, &linter); + generate_table(&mut table_out, &linter, &linter); } } diff --git a/crates/ruff_macros/src/map_codes.rs b/crates/ruff_macros/src/map_codes.rs index 5d1350611f..f8deef8e15 100644 --- a/crates/ruff_macros/src/map_codes.rs +++ b/crates/ruff_macros/src/map_codes.rs @@ -179,6 +179,7 @@ pub fn map_codes(func: &ItemFn) -> syn::Result { #[allow(clippy::type_complexity)] let mut rule_to_codes: HashMap<&Path, Vec<(&Ident, &String, &Vec)>> = HashMap::new(); + let mut linter_code_for_rule_match_arms = quote!(); for (linter, map) in &linters { for (code, (rule, attrs)) in map { @@ -186,6 +187,9 @@ pub fn map_codes(func: &ItemFn) -> syn::Result { .entry(rule) .or_default() .push((linter, code, attrs)); + linter_code_for_rule_match_arms.extend(quote! { + #(#attrs)* (Self::#linter, #rule) => Some(#code), + }); } } @@ -216,6 +220,15 @@ pub fn map_codes(func: &ItemFn) -> syn::Result { } } + impl crate::registry::Linter { + pub fn code_for_rule(&self, rule: &Rule) -> Option<&'static str> { + match (self, rule) { + #linter_code_for_rule_match_arms + _ => None, + } + } + } + #[derive(PartialEq, Eq, PartialOrd, Ord)] pub struct NoqaCode(&'static str, &'static str);