diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C417.py b/crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C417.py index 18b6525be62fd..70dfcf841987f 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C417.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C417.py @@ -47,3 +47,13 @@ def func(arg1: int, arg2: int = 4): map(lambda x: x, y if y else z) map(lambda x: x, (y if y else z)) map(lambda x: x, (x, y, z)) + +# See https://github.com/astral-sh/ruff/issues/14808 +# The following should be Ok since +# named expressions are a syntax error inside comprehensions +a = [1, 2, 3] +b = map(lambda x: x, c := a) +print(c) + +# Check nested as well +map(lambda x:x, [c:=a]) diff --git a/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_map.rs b/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_map.rs index e0811a7e988d5..f8a50dca3b042 100644 --- a/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_map.rs +++ b/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_map.rs @@ -3,6 +3,7 @@ use std::fmt; use ruff_diagnostics::{Diagnostic, Fix}; use ruff_diagnostics::{FixAvailability, Violation}; use ruff_macros::{derive_message_formats, ViolationMetadata}; +use ruff_python_ast::helpers::any_over_expr; use ruff_python_ast::visitor; use ruff_python_ast::visitor::Visitor; use ruff_python_ast::{self as ast, Arguments, Expr, ExprContext, Parameters, Stmt}; @@ -99,11 +100,17 @@ pub(crate) fn unnecessary_map( // Only flag, e.g., `map(lambda x: x + 1, iterable)`. let [Expr::Lambda(ast::ExprLambda { parameters, body, .. - }), _] = args + }), iterable] = args else { return; }; + // For example, (x+1 for x in (c:=a)) is invalid syntax + // so we can't suggest it. + if any_over_expr(iterable, &|expr| expr.is_named_expr()) { + return; + } + if parameters.as_ref().is_some_and(|parameters| { late_binding(parameters, body) || parameters diff --git a/crates/ruff_linter/src/rules/flake8_comprehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__C417_C417.py.snap b/crates/ruff_linter/src/rules/flake8_comprehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__C417_C417.py.snap index 66f8ecc443ccd..69b8be4704427 100644 --- a/crates/ruff_linter/src/rules/flake8_comprehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__C417_C417.py.snap +++ b/crates/ruff_linter/src/rules/flake8_comprehensions/snapshots/ruff_linter__rules__flake8_comprehensions__tests__C417_C417.py.snap @@ -329,6 +329,7 @@ C417.py:47:1: C417 [*] Unnecessary `map()` usage (rewrite using a generator expr 47 |+(x for x in (y if y else z)) 48 48 | map(lambda x: x, (y if y else z)) 49 49 | map(lambda x: x, (x, y, z)) +50 50 | C417.py:48:1: C417 [*] Unnecessary `map()` usage (rewrite using a generator expression) | @@ -347,6 +348,8 @@ C417.py:48:1: C417 [*] Unnecessary `map()` usage (rewrite using a generator expr 48 |-map(lambda x: x, (y if y else z)) 48 |+(x for x in (y if y else z)) 49 49 | map(lambda x: x, (x, y, z)) +50 50 | +51 51 | # See https://github.com/astral-sh/ruff/issues/14808 C417.py:49:1: C417 [*] Unnecessary `map()` usage (rewrite using a generator expression) | @@ -354,6 +357,8 @@ C417.py:49:1: C417 [*] Unnecessary `map()` usage (rewrite using a generator expr 48 | map(lambda x: x, (y if y else z)) 49 | map(lambda x: x, (x, y, z)) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ C417 +50 | +51 | # See https://github.com/astral-sh/ruff/issues/14808 | = help: Replace `map()` with a generator expression @@ -363,3 +368,6 @@ C417.py:49:1: C417 [*] Unnecessary `map()` usage (rewrite using a generator expr 48 48 | map(lambda x: x, (y if y else z)) 49 |-map(lambda x: x, (x, y, z)) 49 |+(x for x in (x, y, z)) +50 50 | +51 51 | # See https://github.com/astral-sh/ruff/issues/14808 +52 52 | # The following should be Ok since