astral-sh/ruff · error

Expected Expression::ListComp | Expression::SetComp

Error message

Expected Expression::ListComp | Expression::SetComp

What it means

An unreachable!/bail guard in fix_unnecessary_comprehension_in_call: the first argument of the call matched by the rule is neither a list comprehension nor a set comprehension, so there is nothing to unwrap. The rule trigger and the fixer disagree on the expression shape.

Solutions

  1. Align the rule's detection with the fixer so only list/set comprehensions are fixed
  2. Bail without a fix when the argument shape is unexpected
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs:849 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs:849

    let (whitespace_after, whitespace_before, elt, for_in, lpar, rpar) = match &call.args[0].value {
        Expression::ListComp(list_comp) => (
            &list_comp.lbracket.whitespace_after,
            &list_comp.rbracket.whitespace_before,
            &list_comp.elt,
            &list_comp.for_in,
            &list_comp.lpar,
            &list_comp.rpar,
        ),
        Expression::SetComp(set_comp) => (
            &set_comp.lbrace.whitespace_after,
            &set_comp.rbrace.whitespace_before,
            &set_comp.elt,
            &set_comp.for_in,
            &set_comp.lpar,
            &set_comp.rpar,
        ),
        _ => {
            bail!("Expected Expression::ListComp | Expression::SetComp");
        }
    };

    let mut new_empty_lines = vec![];

    if let ParenthesizableWhitespace::ParenthesizedWhitespace(ParenthesizedWhitespace {
        first_line,
        empty_lines,
        ..
    }) = &whitespace_after
    {
        // If there's a comment on the line after the opening bracket, we need
        // to preserve it. The way we do this is by adding a new empty line
        // with the same comment.
        //
        // Example:
        // ```python
        // any(

View on GitHub (pinned to 26f38c119c)