astral-sh/ruff · error

redefined_loop_name called on Statement that is not a `With`

Error message

redefined_loop_name called on Statement that is not a `With` or `For`

What it means

An unreachable! in the redefined-loop-name rule: the function was invoked with a statement that is neither a For nor a With, but the rule only registers the visitor for those statement kinds. Firing it means the statement dispatch in the rule's visitor is out of sync with this handler.

Source

Thrown at crates/ruff_linter/src/rules/pylint/rules/redefined_loop_name.rs:408

        Stmt::For(ast::StmtFor { target, body, .. }) => {
            let outer_assignment_targets: Vec<ExprWithOuterBindingKind> =
                assignment_targets_from_expr(target, &checker.settings().dummy_variable_rgx)
                    .map(|expr| ExprWithOuterBindingKind {
                        expr,
                        binding_kind: OuterBindingKind::For,
                    })
                    .collect();
            let mut visitor = InnerForWithAssignTargetsVisitor {
                context: checker.semantic(),
                dummy_variable_rgx: &checker.settings().dummy_variable_rgx,
                assignment_targets: vec![],
            };
            for stmt in body {
                visitor.visit_stmt(stmt);
            }
            (outer_assignment_targets, visitor.assignment_targets)
        }
        _ => panic!("redefined_loop_name called on Statement that is not a `With` or `For`"),
    };

    for outer_assignment_target in &outer_assignment_targets {
        for inner_assignment_target in &inner_assignment_targets {
            // Compare the targets structurally.
            if ComparableExpr::from(outer_assignment_target.expr)
                .eq(&(ComparableExpr::from(inner_assignment_target.expr)))
            {
                checker.report_diagnostic(
                    RedefinedLoopName {
                        name: checker.generator().expr(outer_assignment_target.expr),
                        outer_kind: outer_assignment_target.binding_kind,
                        inner_kind: inner_assignment_target.binding_kind,
                    },
                    inner_assignment_target.expr.range(),
                );
            }
        }

View on GitHub (pinned to 26f38c119c)

Solutions

  1. Ensure the visitor only calls the handler for Stmt::For and Stmt::With
  2. Replace unreachable! with an early return for other statement kinds
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at crates/ruff_linter/src/rules/pylint/rules/redefined_loop_name.rs:408 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/d44c134a4dea6975. Report an issue: GitHub.