{"record":{"id":"a00ceed1e39c615d","repo":"astral-sh/ruff","slug":"cannot-autofix-reference-in-the-source-code","errorCode":null,"errorMessage":"Cannot autofix: reference in the source code (`{}`) is not equal to the typevar name (`{}`)","messagePattern":"Cannot autofix: reference in the source code \\(`(.+?)`\\) is not equal to the typevar name \\(`(.+?)`\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ruff_linter/src/rules/flake8_pyi/rules/custom_type_var_for_self.rs","lineNumber":418,"sourceCode":"\n/// Returns a series of [`Edit`]s that modify all references to the given `typevar`.\n///\n/// Only references within `editable_range` will be modified.\n/// This ensures that no edit in this series will overlap with other edits.\nfn replace_typevar_usages_with_self<'a>(\n    typevar: TypeVar<'a>,\n    source: &'a str,\n    self_or_cls_annotation_range: TextRange,\n    self_symbol_binding: &'a str,\n    editable_range: TextRange,\n    semantic: &'a SemanticModel<'a>,\n    edits: &mut Vec<Edit>,\n) -> anyhow::Result<()> {\n    let tvar_name = typevar.name(source);\n    for reference in typevar.references(semantic) {\n        let reference_range = reference.range();\n        if &source[reference_range] != tvar_name {\n            bail!(\n                \"Cannot autofix: reference in the source code (`{}`) is not equal to the typevar name (`{}`)\",\n                &source[reference_range],\n                tvar_name\n            );\n        }\n        if !editable_range.contains_range(reference_range) {\n            continue;\n        }\n        if self_or_cls_annotation_range.contains_range(reference_range) {\n            continue;\n        }\n        edits.push(Edit::range_replacement(\n            self_symbol_binding.to_string(),\n            reference_range,\n        ));\n    }\n    Ok(())\n}","sourceCodeStart":400,"sourceCodeEnd":436,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/rules/flake8_pyi/rules/custom_type_var_for_self.rs#L400-L436","documentation":"An anyhow bail! in the flake8-pyi custom-typevar-generic-for-self autofix: while replacing typevar references with Self, a reference in the source text does not equal the typevar's name, so applying a textual edit would be wrong. The fix is abandoned and reported as failed.","triggerScenarios":"Thrown at crates/ruff_linter/src/rules/flake8_pyi/rules/custom_type_var_for_self.rs:418 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Skip the autofix when a reference's source text does not match the typevar name","Reuse the exact reference span from the semantic model instead of matching by name"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"26f38c119cac42e4d320ba08f09224fdec74af2c","analyzedAt":"2026-09-05T10:32:37.492Z","contentChangedAt":"2026-09-05T10:32:37.492Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}