astral-sh/ruff · error
Expected a valid noqa code corresponding to a rule
Error message
Expected a valid noqa code corresponding to a rule
What it means
When Ruff serializes diagnostics to SARIF, a diagnostic rule code (e.g. `F401`) is parsed with `Linter::parse_code`; the linter prefix is then searched for a rule whose noqa-code suffix matches. If parse_code succeeds but no rule in that linter has the matching suffix, the code is structurally valid but not a real rule, and this expect panics.
Source
Thrown at crates/ruff_linter/src/message/sarif.rs:140
help: SarifMessage<'a>,
#[serde(skip_serializing_if = "Option::is_none")]
help_uri: Option<String>,
id: &'a str,
properties: SarifProperties<'a>,
short_description: SarifMessage<'a>,
}
impl<'a> From<(&'a str, SarifLevel)> for SarifRule<'a> {
fn from(code_and_level: (&'a str, SarifLevel)) -> Self {
let (code, level) = code_and_level;
// This is a manual re-implementation of Rule::from_code, but we also want the Linter. This
// avoids calling Linter::parse_code twice.
let (kind, rule) = match Linter::parse_code(code) {
Some((linter, suffix)) => {
let rule = linter
.all_rules()
.find(|rule| rule.noqa_code().is_some_and(|code| code.suffix() == suffix))
.expect("Expected a valid noqa code corresponding to a rule");
(Some(linter.name()), rule)
}
None => {
let rule = Rule::from_name(code).expect("expected a valid rule name");
(None, rule)
}
};
Self {
id: code,
short_description: SarifMessage {
text: rule.message_formats()[0].into(),
},
full_description: rule
.explanation()
.map(|text| SarifMessage { text: text.into() }),
help: SarifMessage {
text: rule.message_formats()[0].into(),
},View on GitHub (pinned to 26f38c119c)
Solutions
- Verify the rule code is a current, real Ruff code (`ruff rule <CODE>`); replace stale codes with the current ones
- Update Ruff — rule-to-noqa-code mappings change between versions
- If a downstream tool injects codes, map removed/renamed codes to their current replacements before rendering SARIF
- If developing: handle the None case from the find() instead of expecting
Example fix
// before
let rule = linter.all_rules().find(...).expect("Expected a valid noqa code corresponding to a rule");
// after
let rule = linter.all_rules().find(...)
.with_context(|| format!("no rule with noqa code suffix {suffix:?} for linter {}", linter.name()))?; Defensive patterns
Strategy: validation
Validate before calling
# validate a code before rendering SARIF
import subprocess
def is_valid_code(code: str) -> bool:
r = subprocess.run(['ruff', 'rule', code], capture_output=True)
return r.returncode == 0 Prevention
- Only feed real Ruff codes/names into SARIF export
- Re-map codes removed or renamed between Ruff versions
- Run `ruff rule <CODE>` to verify before use
When it happens
Trigger: Rendering a diagnostic to SARIF (`SarifMessage::from`) with a code string that has a valid linter prefix but an unmatched suffix — e.g. an alias or reserved code like `E999`-style codes or a preview/promoted rule whose mapping changed.
Common situations: External tools feeding synthetic/legacy rule codes through Ruff's SARIF output; version skew where a rule's noqa code changed between releases; preview-mode rules renamed upstream.
Related errors
- expected a valid rule name
- extra use-def data should have been retained
- should be set because `extract_if` only yields elements with
- Should only ever pass a positive integer to `from_nonnegativ
- argument index should be valid
AI-assisted analysis of astral-sh/ruff@26f38c119c (2026-09-05).
Data as JSON: /api/errors/d0d3efd00f3e3bd7.
Report an issue: GitHub.