{"record":{"id":"0a2d1453aef61ad7","repo":"astral-sh/ruff","slug":"expected-container-to-use-a-value-as-the-second-el","errorCode":null,"errorMessage":"Expected container to use a value as the second element","messagePattern":"Expected container to use a value as the second element","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs","lineNumber":788,"sourceCode":"                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            }));\n        }\n    }\n","sourceCodeStart":770,"sourceCodeEnd":806,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs#L770-L806","documentation":"An anyhow bail! in fix_unnecessary_map (C40-style comprehension fixes): while rewriting a dict comprehension into a dict literal, the lambda's body container did not contain exactly a key and a value element. The fixer cannot build a valid dict literal from that shape and aborts the fix.","triggerScenarios":"Thrown at crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs:788 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Skip the autofix when the container does not hold exactly two elements","Extend the fixer to normalize containers with unexpected shapes before rewriting"],"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"}