{"record":{"id":"e94a35a455902351","repo":"astral-sh/ruff","slug":"expected-container-to-use-a-key-as-the-first-eleme","errorCode":null,"errorMessage":"Expected container to use a key as the first element","messagePattern":"Expected container to use a key as the first element","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs","lineNumber":785,"sourceCode":"                lpar: vec![],\n                rpar: vec![],\n                lbrace: LeftCurlyBrace::default(),\n                rbrace: RightCurlyBrace::default(),\n            }));\n        }\n        ObjectType::Dict => {\n            let elements = match lambda.body.as_ref() {\n                Expression::Tuple(tuple) => &tuple.elements,\n                Expression::List(list) => &list.elements,\n                _ => {\n                    bail!(\"Expected tuple or list for dictionary comprehension\")\n                }\n            };\n            let [key, value] = elements.as_slice() else {\n                bail!(\"Expected container to include two elements\");\n            };\n            let Element::Simple { value: key, .. } = key else {\n                bail!(\"Expected container to use a key as the first element\");\n            };\n            let Element::Simple { value, .. } = value else {\n                bail!(\"Expected container to use a value as the second element\");\n            };\n\n            tree = Expression::DictComp(Box::new(DictComp {\n                for_in: compfor,\n                lpar: vec![],\n                rpar: vec![],\n                key: Box::new(key.clone()),\n                value: Box::new(value.clone()),\n                lbrace: LeftCurlyBrace::default(),\n                rbrace: RightCurlyBrace::default(),\n                whitespace_before_colon: ParenthesizableWhitespace::default(),\n                whitespace_after_colon: ParenthesizableWhitespace::SimpleWhitespace(\n                    SimpleWhitespace(\" \"),\n                ),\n            }));","sourceCodeStart":767,"sourceCodeEnd":803,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs#L767-L803","documentation":"Third check in the same C417 dict-comprehension fix: the first element of the lambda body's pair container must be a simple (starless, unpackless) expression to serve as the key. Starred elements, slices with unusual nodes, etc. cannot become dict keys in the generated comprehension, so the fixer bails.","triggerScenarios":"`ruff check --fix` on `dict(map(lambda x: (*x, y), items))` or bodies whose first element is a starred expression like `lambda k: (*a, b)`.","commonSituations":"Unpacking-heavy lambda bodies from data-processing code; refactors introducing starred args into tuple bodies; stale diagnostics after edits.","solutions":["Replace the starred element with an explicit expression (e.g. `x[0]`) so the fix can apply.","Rewrite as a manual dict comprehension: `{k: v for ...}` including whatever unpacking you need.","Add `# noqa: C417` if starred/unpacked keys are intentional."],"exampleFix":"// before\nd = dict(map(lambda x: (*x[:2],), rows))\n// after\nd = {x[0]: x[1] for x in rows}","handlingStrategy":"type-guard","validationCode":"ok = not isinstance(lam.body.elts[0], ast.Starred)","typeGuard":"def simple_key(body):\n    return (isinstance(body, (ast.Tuple, ast.List)) and len(body.elts) == 2\n            and not isinstance(body.elts[0], ast.Starred)\n            and not isinstance(body.elts[1], ast.Starred))","tryCatchPattern":"try:\n    apply_autofix(diagnostic)\nexcept Exception:\n    rewrite_with_explicit_indexing()","preventionTips":["Avoid starred expressions in key/value tuple bodies","Use explicit indexing x[0], x[1] for dict-conversion lambdas","Suppress C417 when unpacking is intentional"],"tags":["rust","ruff","lint-autofix","flake8-comprehensions"],"backgroundTag":"autofix-unsupported-ast-shape","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"}