{"record":{"id":"31c549d42afffa19","repo":"astral-sh/ruff","slug":"expected-a-valid-rule-name","errorCode":null,"errorMessage":"expected a valid rule name","messagePattern":"expected a valid rule name","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ruff_linter/src/message/sarif.rs","lineNumber":144,"sourceCode":"    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            },\n            help_uri: rule.url(),\n            properties: SarifProperties {\n                id: code,\n                kind,","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/message/sarif.rs#L126-L162","documentation":"The SARIF serializer accepts codes that are not prefixed linter codes by treating them as rule names via `Rule::from_name`. When the string is neither a valid prefixed code nor a known rule name, this expect panics, indicating the caller passed a code Ruff does not recognize as any rule.","triggerScenarios":"Calling the SARIF conversion (`SarifMessage::from`) with an arbitrary string (e.g. `\"MYRULE001\"`, a plugin rule name, or a typo like `\"F401 \"`) that `Linter::parse_code` fails on and `Rule::from_name` cannot resolve.","commonSituations":"Third-party/plugin rule codes passed through SARIF output; typos in tooling that constructs diagnostics programmatically; codes from other linters (flake8 plugins not registered in Ruff) being rendered.","solutions":["Use only real Ruff rule codes or rule names (`ruff rule` lists them)","Register/upgrade the plugin providing the rule if it is a third-party code","Sanitize upstream codes: drop or remap unknown codes before SARIF serialization","If developing: return a Result/fallback instead of expecting on from_name"],"exampleFix":"// before\nlet rule = Rule::from_name(code).expect(\"expected a valid rule name\");\n// after\nlet rule = Rule::from_name(code)\n    .with_context(|| format!(\"unknown rule name {code:?} in SARIF export\"))?;","handlingStrategy":"validation","validationCode":"# verify the string is a known rule name before SARIF conversion\nKNOWN = set(subprocess.run(['ruff','rule','--all'],capture_output=True,text=True).stdout.split())\nassert code in KNOWN or is_valid_code(code), f'unknown rule: {code}'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use canonical rule names from `ruff rule --all`","Never forward third-party flake8 plugin codes not registered in Ruff","Trim/normalize codes (whitespace, case) before passing them"],"tags":["rust","panic","sarif","rule-name","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"}