{"record":{"id":"7c0763225d3a454b","repo":"astral-sh/ruff","slug":"expected-two-arguments","errorCode":null,"errorMessage":"Expected two arguments","messagePattern":"Expected two arguments","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs","lineNumber":680,"sourceCode":"}\n\n/// (C417) Convert `map(lambda x: x * 2, bar)` to `(x * 2 for x in bar)`.\npub(crate) fn fix_unnecessary_map(\n    call_ast_node: &ExprCall,\n    parent: Option<&Expr>,\n    object_type: ObjectType,\n    locator: &Locator,\n    stylist: &Stylist,\n) -> Result<Edit> {\n    let module_text = locator.slice(call_ast_node);\n    let mut tree = match_expression(module_text)?;\n    let call = match_call_mut(&mut tree)?;\n\n    let (lambda, iter) = match call.args.as_slice() {\n        [call] => {\n            let call = match_call(&call.value)?;\n            let [lambda, iter] = call.args.as_slice() else {\n                bail!(\"Expected two arguments\");\n            };\n            let lambda = match_lambda(&lambda.value)?;\n            let iter = &iter.value;\n            (lambda, iter)\n        }\n        [lambda, iter] => {\n            let lambda = match_lambda(&lambda.value)?;\n            let iter = &iter.value;\n            (lambda, iter)\n        }\n        _ => bail!(\"Expected a call or lambda\"),\n    };\n\n    // Format the lambda target.\n    let target = match lambda.params.params.as_slice() {\n        // Ex) `lambda: x`\n        [] => AssignTargetExpression::Name(Box::new(Name {\n            value: \"_\",","sourceCodeStart":662,"sourceCodeEnd":698,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs#L662-L698","documentation":"Raised by the C417/unnecessary-map fix while converting `map(lambda x: ..., iterable)` into a comprehension. When the outer call has a single argument, that argument must itself be a call whose two arguments are the lambda and the iterable; if the inner call's args are not exactly two, the fixer bails.","triggerScenarios":"`ruff check --fix` on code like `map(lambda f, *rest: f(*rest), fns)` where the inner call argument list is not exactly `[lambda, iterable]`, or `map(f(...))` whose callee call has extra arguments.","commonSituations":"Lambdas with default/star args wrapped in other calls; starmap-style patterns; refactored code where an extra argument was inserted between diagnostic generation and fixing.","solutions":["Re-run ruff to refresh diagnostics against current code.","Rewrite the `map`/`lambda` combination as an explicit comprehension by hand.","Add `# noqa: C417` if the lambda signature is intentionally non-trivial and the rule should not apply."],"exampleFix":"// before\nsquares = map(lambda x: x ** 2, numbers)\n// after\nsquares = (x ** 2 for x in numbers)  # or [x ** 2 for x in numbers]","handlingStrategy":"validation","validationCode":"assert len(inner_call.args) == 2, 'map(lambda ..., iter) fix needs exactly two inner args'","typeGuard":"def is_lambda_plus_iter(args):\n    return len(args) == 2 and isinstance(args[0], ast.Lambda)","tryCatchPattern":"try:\n    apply_autofix(diagnostic)\nexcept Exception:\n    write_comprehension_by_hand()","preventionTips":["Keep map-lambda patterns simple for autofix","Avoid star/default args in lambdas targeted by C417","Re-run ruff after refactoring map() calls"],"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"}