astral-sh/ruff · error

Failed to fix invalid comparison due to missing op

Error message

Failed to fix invalid comparison due to missing op

What it means

Companion bail for F632's autofix: the rule must have previously located and stored the comparison operator to replace, but when building the fix no located operator was found. Without it there is no range to edit, so the fixer aborts.

Source

Thrown at crates/ruff_linter/src/rules/pyflakes/rules/invalid_literal_comparisons.rs:113

                    lazy_located.as_ref().and_then(|located| located.get(index))
                {
                    assert_eq!(located_op.op, *op);
                    if let Ok(content) = match located_op.op {
                        CmpOp::Is => Ok::<String, Error>("==".to_string()),
                        CmpOp::IsNot => Ok("!=".to_string()),
                        node => {
                            bail!("Failed to fix invalid comparison: {node:?}")
                        }
                    } {
                        Ok(Some(Fix::safe_edit(Edit::range_replacement(
                            content,
                            located_op.range,
                        ))))
                    } else {
                        Ok(None)
                    }
                } else {
                    bail!("Failed to fix invalid comparison due to missing op")
                }
            });
        }
    }
}

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
enum IsCmpOp {
    Is,
    IsNot,
}

impl From<&CmpOp> for IsCmpOp {
    fn from(cmp_op: &CmpOp) -> Self {
        match cmp_op {
            CmpOp::Is => IsCmpOp::Is,
            CmpOp::IsNot => IsCmpOp::IsNot,
            _ => panic!("Expected CmpOp::Is | CmpOp::IsNot"),

View on GitHub (pinned to 15f3fe6b15)

Solutions

  1. Update Ruff to the latest version
  2. Fix the comparison manually (use `==`/`!=` for literal comparisons)
  3. Add `# noqa: F632` if the check is intentional

Example fix

# before
if sys.version_info[0] is 3:
    pass
# after
if sys.version_info[0] == 3:
    pass
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: `invalid_literal_comparison` iterated a comparison chain where the diagnostic condition held, but the operator lookup (`located_op`) returned nothing — e.g. the chain shape changed between collection and fix building — so the `assert_eq!`-guarded branch is not entered.

Common situations: Complex multi-operator comparisons, stale caches, or modified Ruff builds; practically rare in stock Ruff.

Related errors


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