Skip to content

Commit 08a227e

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 97f0590 commit 08a227e

File tree

8 files changed

+39
-23
lines changed

8 files changed

+39
-23
lines changed

crates/ruff/src/cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ impl LintCacheData {
442442
// Parse the kebab-case rule name into a `Rule`. This will fail for syntax errors, so
443443
// this also serves to filter them out, but we shouldn't be caching files with syntax
444444
// errors anyway.
445-
.filter_map(|msg| Some((msg.name().parse().ok()?, msg)))
445+
.filter_map(|msg| Some((msg.noqa_code().and_then(|code| code.rule())?, msg)))
446446
.map(|(rule, msg)| {
447447
// Make sure that all message use the same source file.
448448
assert_eq!(

crates/ruff_linter/src/checkers/noqa.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ pub(crate) fn check_noqa(
149149

150150
if seen_codes.insert(original_code) {
151151
let is_code_used = if is_file_level {
152-
context.iter().any(|diag| {
153-
diag.noqa_code().is_some_and(|noqa| noqa == code)
154-
})
152+
context
153+
.iter()
154+
.any(|diag| diag.noqa_code().is_some_and(|noqa| noqa == code))
155155
} else {
156156
matches.iter().any(|match_| *match_ == code)
157157
} || settings

crates/ruff_linter/src/linter.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,8 @@ pub fn check_path(
391391
if !per_file_ignores.is_empty() {
392392
diagnostics.as_mut_vec().retain(|diagnostic| {
393393
diagnostic
394-
.name()
395-
.parse()
396-
.ok()
394+
.noqa_code()
395+
.and_then(|code| code.rule())
397396
.is_none_or(|rule| !per_file_ignores.contains(rule))
398397
});
399398
}
@@ -428,9 +427,8 @@ pub fn check_path(
428427
// Remove fixes for any rules marked as unfixable.
429428
for diagnostic in &mut diagnostics {
430429
if diagnostic
431-
.name()
432-
.parse()
433-
.ok()
430+
.noqa_code()
431+
.and_then(|code| code.rule())
434432
.is_none_or(|rule| !settings.rules.should_fix(rule))
435433
{
436434
diagnostic.fix = None;

crates/ruff_linter/src/registry.rs

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

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