mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:35 +00:00
Tweak prefix match to use .all_rules() (#5512)
## Summary No behavior change, but I think this is a little cleaner.
This commit is contained in:
parent
d7214e77e6
commit
485d997d35
2 changed files with 16 additions and 10 deletions
|
@ -14,6 +14,18 @@ use crate::rules;
|
||||||
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub struct NoqaCode(&'static str, &'static str);
|
pub struct NoqaCode(&'static str, &'static str);
|
||||||
|
|
||||||
|
impl NoqaCode {
|
||||||
|
/// Return the prefix for the [`NoqaCode`], e.g., `SIM` for `SIM101`.
|
||||||
|
pub fn prefix(&self) -> &str {
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the suffix for the [`NoqaCode`], e.g., `101` for `SIM101`.
|
||||||
|
pub fn suffix(&self) -> &str {
|
||||||
|
self.1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl std::fmt::Debug for NoqaCode {
|
impl std::fmt::Debug for NoqaCode {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
std::fmt::Display::fmt(self, f)
|
std::fmt::Display::fmt(self, f)
|
||||||
|
|
|
@ -18,14 +18,10 @@ pub trait AsRule {
|
||||||
impl Rule {
|
impl Rule {
|
||||||
pub fn from_code(code: &str) -> Result<Self, FromCodeError> {
|
pub fn from_code(code: &str) -> Result<Self, FromCodeError> {
|
||||||
let (linter, code) = Linter::parse_code(code).ok_or(FromCodeError::Unknown)?;
|
let (linter, code) = Linter::parse_code(code).ok_or(FromCodeError::Unknown)?;
|
||||||
let prefix: RuleCodePrefix = RuleCodePrefix::parse(&linter, code)?;
|
linter
|
||||||
let rule = prefix.rules().next().unwrap();
|
.all_rules()
|
||||||
// TODO(charlie): Add a method to return an individual code, rather than matching on the
|
.find(|rule| rule.noqa_code().suffix() == code)
|
||||||
// prefix.
|
.ok_or(FromCodeError::Unknown)
|
||||||
if rule.noqa_code().to_string() != format!("{}{}", linter.common_prefix(), code) {
|
|
||||||
return Err(FromCodeError::Prefix);
|
|
||||||
}
|
|
||||||
Ok(rule)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,8 +29,6 @@ impl Rule {
|
||||||
pub enum FromCodeError {
|
pub enum FromCodeError {
|
||||||
#[error("unknown rule code")]
|
#[error("unknown rule code")]
|
||||||
Unknown,
|
Unknown,
|
||||||
#[error("expected a rule code (like `SIM101`), not a prefix (like `SIM` or `SIM1`)")]
|
|
||||||
Prefix,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(EnumIter, Debug, PartialEq, Eq, Clone, Hash, RuleNamespace)]
|
#[derive(EnumIter, Debug, PartialEq, Eq, Clone, Hash, RuleNamespace)]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue