astral-sh/ruff · error

expected a valid rule name

Error message

expected a valid rule name

What it means

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.

Source

Thrown at crates/ruff_linter/src/message/sarif.rs:144

    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(),
            },
            help_uri: rule.url(),
            properties: SarifProperties {
                id: code,
                kind,

View on GitHub (pinned to 26f38c119c)

Solutions

  1. Use only real Ruff rule codes or rule names (`ruff rule` lists them)
  2. Register/upgrade the plugin providing the rule if it is a third-party code
  3. Sanitize upstream codes: drop or remap unknown codes before SARIF serialization
  4. If developing: return a Result/fallback instead of expecting on from_name

Example fix

// before
let rule = Rule::from_name(code).expect("expected a valid rule name");
// after
let rule = Rule::from_name(code)
    .with_context(|| format!("unknown rule name {code:?} in SARIF export"))?;
Defensive patterns

Strategy: validation

Validate before calling

# verify the string is a known rule name before SARIF conversion
KNOWN = set(subprocess.run(['ruff','rule','--all'],capture_output=True,text=True).stdout.split())
assert code in KNOWN or is_valid_code(code), f'unknown rule: {code}'

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of astral-sh/ruff@26f38c119c (2026-09-05). Data as JSON: /api/errors/31c549d42afffa19. Report an issue: GitHub.