Change NoqaCode to a single string with an offset

This commit is contained in:
Brent Westbrook 2025-06-17 16:29:04 -04:00
parent a2cd6df429
commit 1215b84d32
2 changed files with 12 additions and 8 deletions

View file

@ -11,17 +11,17 @@ use crate::rule_selector::is_single_rule_selector;
use crate::rules;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct NoqaCode(&'static str, &'static str);
pub struct NoqaCode(&'static str, usize);
impl NoqaCode {
/// Return the prefix for the [`NoqaCode`], e.g., `SIM` for `SIM101`.
pub fn prefix(&self) -> &str {
self.0
&self.0[..self.1]
}
/// Return the suffix for the [`NoqaCode`], e.g., `101` for `SIM101`.
pub fn suffix(&self) -> &str {
self.1
&self.0[self.1..]
}
}
@ -33,14 +33,14 @@ impl std::fmt::Debug for NoqaCode {
impl std::fmt::Display for NoqaCode {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{}{}", self.0, self.1)
f.write_str(self.0)
}
}
impl PartialEq<&str> for NoqaCode {
fn eq(&self, other: &&str) -> bool {
match other.strip_prefix(self.0) {
Some(suffix) => suffix == self.1,
match other.strip_prefix(self.prefix()) {
Some(suffix) => suffix == self.suffix(),
None => false,
}
}
@ -51,7 +51,7 @@ impl serde::Serialize for NoqaCode {
where
S: serde::Serializer,
{
serializer.serialize_str(&self.to_string())
serializer.serialize_str(self.0)
}
}

View file

@ -289,7 +289,11 @@ See also https://github.com/astral-sh/ruff/issues/2186.
.unwrap();
rule_noqa_code_match_arms.extend(quote! {
#(#attrs)* Rule::#rule_name => NoqaCode(crate::registry::Linter::#linter.common_prefix(), #code),
#(#attrs)* Rule::#rule_name => {
static CODE: std::sync::LazyLock<String> = std::sync::LazyLock::new(
|| format!("{}{}", crate::registry::Linter::#linter.common_prefix(), #code));
NoqaCode(CODE.as_str(), crate::registry::Linter::#linter.common_prefix().len())
}
});
rule_group_match_arms.extend(quote! {