{"record":{"id":"d0d3efd00f3e3bd7","repo":"astral-sh/ruff","slug":"expected-a-valid-noqa-code-corresponding-to-a-rule","errorCode":null,"errorMessage":"Expected a valid noqa code corresponding to a rule","messagePattern":"Expected a valid noqa code corresponding to a rule","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ruff_linter/src/message/sarif.rs","lineNumber":140,"sourceCode":"    help: SarifMessage<'a>,\n    #[serde(skip_serializing_if = \"Option::is_none\")]\n    help_uri: Option<String>,\n    id: &'a str,\n    properties: SarifProperties<'a>,\n    short_description: SarifMessage<'a>,\n}\n\nimpl<'a> From<(&'a str, SarifLevel)> for SarifRule<'a> {\n    fn from(code_and_level: (&'a str, SarifLevel)) -> Self {\n        let (code, level) = code_and_level;\n        // This is a manual re-implementation of Rule::from_code, but we also want the Linter. This\n        // avoids calling Linter::parse_code twice.\n        let (kind, rule) = match Linter::parse_code(code) {\n            Some((linter, suffix)) => {\n                let rule = linter\n                    .all_rules()\n                    .find(|rule| rule.noqa_code().is_some_and(|code| code.suffix() == suffix))\n                    .expect(\"Expected a valid noqa code corresponding to a rule\");\n                (Some(linter.name()), rule)\n            }\n            None => {\n                let rule = Rule::from_name(code).expect(\"expected a valid rule name\");\n                (None, rule)\n            }\n        };\n        Self {\n            id: code,\n            short_description: SarifMessage {\n                text: rule.message_formats()[0].into(),\n            },\n            full_description: rule\n                .explanation()\n                .map(|text| SarifMessage { text: text.into() }),\n            help: SarifMessage {\n                text: rule.message_formats()[0].into(),\n            },","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/message/sarif.rs#L122-L158","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nlet rule = linter.all_rules().find(...).expect(\"Expected a valid noqa code corresponding to a rule\");\n// after\nlet rule = linter.all_rules().find(...)\n    .with_context(|| format!(\"no rule with noqa code suffix {suffix:?} for linter {}\", linter.name()))?;","handlingStrategy":"validation","validationCode":"# validate a code before rendering SARIF\nimport subprocess\ndef is_valid_code(code: str) -> bool:\n    r = subprocess.run(['ruff', 'rule', code], capture_output=True)\n    return r.returncode == 0","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["rust","panic","sarif","rule-code","reporting"],"backgroundTag":"invalid-rule-code","analyzedSha":"26f38c119cac42e4d320ba08f09224fdec74af2c","analyzedAt":"2026-09-05T10:32:37.492Z","contentChangedAt":"2026-09-05T10:32:37.492Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}