Skip to content

Commit f8a9009

Browse files
committed
convert to Rule "directly" from NoqaCode
it's not exactly direct because we're still matching on a bunch of &'static strs, but it's better than having to call Rule::from_code, which has to format a string and then reparse the NoqaCode into pieces
1 parent 63a8433 commit f8a9009

File tree

6 files changed

+32
-14
lines changed

6 files changed

+32
-14
lines changed

crates/ruff_linter/src/message/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ impl OldDiagnostic {
285285
/// Returns the [`Rule`] corresponding to the diagnostic message.
286286
pub fn to_rule(&self) -> Option<Rule> {
287287
self.noqa_code
288-
.map(|code| Rule::from_code(&code.to_string()).expect("Expected a valid noqa code"))
288+
.map(|code| code.rule().expect("Expected a valid noqa code"))
289289
}
290290

291291
/// Returns the [`NoqaCode`] corresponding to the diagnostic message.

crates/ruff_linter/src/registry.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,6 @@ pub enum Linter {
214214
}
215215

216216
pub trait RuleNamespace: Sized {
217-
/// Returns the prefix that every single code that ruff uses to identify
218-
/// rules from this linter starts with. In the case that multiple
219-
/// `#[prefix]`es are configured for the variant in the `Linter` enum
220-
/// definition this is the empty string.
221-
fn common_prefix(&self) -> &'static str;
222-
223217
/// Attempts to parse the given rule code. If the prefix is recognized
224218
/// returns the respective variant along with the code with the common
225219
/// prefix stripped.

crates/ruff_linter/src/rule_selector.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,6 @@ mod schema {
265265
use strum::IntoEnumIterator;
266266

267267
use crate::RuleSelector;
268-
use crate::registry::RuleNamespace;
269268
use crate::rule_selector::{Linter, RuleCodePrefix};
270269

271270
impl JsonSchema for RuleSelector {

crates/ruff_macros/src/map_codes.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,11 @@ fn generate_rule_to_code(linter_to_rules: &BTreeMap<Ident, BTreeMap<String, Rule
254254
}
255255

256256
let mut rule_noqa_code_match_arms = quote!();
257+
let mut noqa_code_rule_match_arms = quote!();
257258
let mut rule_group_match_arms = quote!();
259+
let mut noqa_code_consts = quote!();
258260

259-
for (rule, codes) in rule_to_codes {
261+
for (i, (rule, codes)) in rule_to_codes.into_iter().enumerate() {
260262
let rule_name = rule.segments.last().unwrap();
261263
assert_eq!(
262264
codes.len(),
@@ -292,6 +294,14 @@ See also https://github.com/astral-sh/ruff/issues/2186.
292294
#(#attrs)* Rule::#rule_name => NoqaCode(crate::registry::Linter::#linter.common_prefix(), #code),
293295
});
294296

297+
let const_ident = quote::format_ident!("NOQA_PREFIX_{}", i);
298+
noqa_code_consts.extend(quote! {
299+
const #const_ident: &str = crate::registry::Linter::#linter.common_prefix();
300+
});
301+
noqa_code_rule_match_arms.extend(quote! {
302+
#(#attrs)* NoqaCode(#const_ident, #code) => Some(Rule::#rule_name),
303+
});
304+
295305
rule_group_match_arms.extend(quote! {
296306
#(#attrs)* Rule::#rule_name => #group,
297307
});
@@ -340,6 +350,16 @@ See also https://github.com/astral-sh/ruff/issues/2186.
340350
}
341351
}
342352
}
353+
354+
impl NoqaCode {
355+
pub fn rule(&self) -> Option<Rule> {
356+
#noqa_code_consts
357+
match self {
358+
#noqa_code_rule_match_arms
359+
_ => None
360+
}
361+
}
362+
}
343363
};
344364
rule_to_code
345365
}

crates/ruff_macros/src/rule_namespace.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ pub(crate) fn derive_impl(input: DeriveInput) -> syn::Result<proc_macro2::TokenS
118118
None
119119
}
120120

121-
fn common_prefix(&self) -> &'static str {
122-
match self { #common_prefix_match_arms }
123-
}
124-
125121
fn name(&self) -> &'static str {
126122
match self { #name_match_arms }
127123
}
@@ -130,6 +126,16 @@ pub(crate) fn derive_impl(input: DeriveInput) -> syn::Result<proc_macro2::TokenS
130126
match self { #url_match_arms }
131127
}
132128
}
129+
130+
impl #ident {
131+
/// Returns the prefix that every single code that ruff uses to identify
132+
/// rules from this linter starts with. In the case that multiple
133+
/// `#[prefix]`es are configured for the variant in the `Linter` enum
134+
/// definition this is the empty string.
135+
pub const fn common_prefix(&self) -> &'static str {
136+
match self { #common_prefix_match_arms }
137+
}
138+
}
133139
})
134140
}
135141

crates/ruff_workspace/src/configuration.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use ruff_cache::cache_dir;
2222
use ruff_formatter::IndentStyle;
2323
use ruff_graph::{AnalyzeSettings, Direction};
2424
use ruff_linter::line_width::{IndentWidth, LineLength};
25-
use ruff_linter::registry::RuleNamespace;
2625
use ruff_linter::registry::{INCOMPATIBLE_CODES, Rule, RuleSet};
2726
use ruff_linter::rule_selector::{PreviewOptions, Specificity};
2827
use ruff_linter::rules::{flake8_import_conventions, isort, pycodestyle};

0 commit comments

Comments
 (0)