diff --git a/crates/ruff/src/registry.rs b/crates/ruff/src/registry.rs index d53cd6ac7e8474..d1d9014bdfcd2b 100644 --- a/crates/ruff/src/registry.rs +++ b/crates/ruff/src/registry.rs @@ -19,7 +19,13 @@ impl Rule { pub fn from_code(code: &str) -> Result { let (linter, code) = Linter::parse_code(code).ok_or(FromCodeError::Unknown)?; let prefix: RuleCodePrefix = RuleCodePrefix::parse(&linter, code)?; - Ok(prefix.rules().next().unwrap()) + let rule = prefix.rules().next().unwrap(); + // TODO(charlie): Add a method to return an individual code, rather than matching on the + // prefix. + if rule.noqa_code().to_string() != format!("{}{}", linter.common_prefix(), code) { + return Err(FromCodeError::Prefix); + } + Ok(rule) } } @@ -27,6 +33,8 @@ impl Rule { pub enum FromCodeError { #[error("unknown rule code")] Unknown, + #[error("expected a rule code (like `SIM101`), not a prefix (like `SIM` or `SIM1`)")] + Prefix, } #[derive(EnumIter, Debug, PartialEq, Eq, Clone, Hash, RuleNamespace)] diff --git a/crates/ruff_cli/src/commands/rule.rs b/crates/ruff_cli/src/commands/rule.rs index ddb8e4ff21485a..9cd41e7bb2b2cb 100644 --- a/crates/ruff_cli/src/commands/rule.rs +++ b/crates/ruff_cli/src/commands/rule.rs @@ -31,7 +31,6 @@ pub(crate) fn rule(rule: Rule, format: HelpFormat) -> Result<()> { output.push('\n'); output.push('\n'); - let (linter, _) = Linter::parse_code(&rule.noqa_code().to_string()).unwrap(); output.push_str(&format!("Derived from the **{}** linter.", linter.name())); output.push('\n'); output.push('\n');