{"record":{"id":"19cdb2eb44fa5f5a","repo":"astral-sh/ruff","slug":"expected-at-least-one-reference","errorCode":null,"errorMessage":"Expected at least one reference","messagePattern":"Expected at least one reference","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ruff_linter/src/rules/flake8_type_checking/rules/runtime_import_in_type_checking_block.rs","lineNumber":310,"sourceCode":"                    let reference = checker.semantic().reference(*reference_id);\n                    if reference.in_runtime_context() {\n                        Some(quote_annotation(\n                            reference.expression_id()?,\n                            checker.semantic(),\n                            checker.stylist(),\n                            checker.locator(),\n                            checker.default_string_flags(),\n                        ))\n                    } else {\n                        None\n                    }\n                })\n            })\n            .collect::<Vec<_>>(),\n    );\n\n    let mut rest = quote_reference_edits.into_iter();\n    let head = rest.next().expect(\"Expected at least one reference\");\n    Fix::unsafe_edits(head, rest).isolate(Checker::isolation(\n        checker.semantic().parent_statement_id(node_id),\n    ))\n}\n\n/// Generate a [`Fix`] to remove runtime imports from a type-checking block.\nfn move_imports(checker: &Checker, node_id: NodeId, imports: &[ImportBinding]) -> Result<Fix> {\n    let statement = checker.semantic().statement(node_id);\n    let parent = checker.semantic().parent_statement(node_id);\n\n    let member_names: Vec<Cow<'_, str>> = imports\n        .iter()\n        .map(|ImportBinding { import, .. }| import)\n        .map(Imported::member_name)\n        .collect();\n\n    // Find the first reference across all imports.\n    let at = imports","sourceCodeStart":292,"sourceCodeEnd":328,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/rules/flake8_type_checking/rules/runtime_import_in_type_checking_block.rs#L292-L328","documentation":"When generating the fix that quotes runtime imports inside a `if TYPE_CHECKING:` block, the rule collects quote_reference_edits and expects at least one. `rest.next().expect(\"Expected at least one reference\")` panics on an empty list, because Fix::unsafe_edits needs a head edit. The invariant is that the function is only called when there is at least one reference to quote.","triggerScenarios":"quote_imports is invoked for an import whose bindings produced zero quote edits — e.g. all references were filtered out (unresolvable members, references outside the type-checking block, or deduplication removing everything) before the fix is built.","commonSituations":"Contributors hit this after changing the reference-collection or filtering logic upstream so that an import with no quotable references still reaches the fix-building path.","solutions":["Guard the call site: return early (or use Fix::empty()) when quote_reference_edits is empty","Ensure the function is only called after verifying at least one binding reference exists","Replace the iterator/expect with a Vec and match on `first`/`split_first`"],"exampleFix":"// before\nlet head = rest.next().expect(\"Expected at least one reference\");\nFix::unsafe_edits(head, rest)\n// after\nlet Some(head) = rest.next() else { return Fix::empty(); };\nFix::unsafe_edits(head, rest)","handlingStrategy":"validation","validationCode":"if quote_reference_edits.is_empty() { return Fix::empty(); }","typeGuard":"if let [head, tail @ ..] = quote_reference_edits.as_slice() { /* build fix */ }","tryCatchPattern":null,"preventionTips":["Check list emptiness before constructing multi-edit fixes","Keep the 'has references' check and fix builder adjacent","Use slice patterns instead of iterator expect"],"tags":["rust","panic","empty-collection","flake8-type-checking"],"backgroundTag":"empty-collection-panic","analyzedSha":"26f38c119cac42e4d320ba08f09224fdec74af2c","analyzedAt":"2026-09-05T10:32:37.492Z","contentChangedAt":"2026-09-05T10:32:37.492Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}