mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-01 12:25:10 +00:00
Avoid invalid fix for C417 with separate keys and values (#6954)
Closes https://github.com/astral-sh/ruff/issues/6951.
This commit is contained in:
parent
ecca125f9a
commit
d1ad20c9ea
2 changed files with 18 additions and 3 deletions
|
|
@ -37,3 +37,6 @@ map(lambda x: lambda x: x, range(4))
|
|||
map(lambda x=1: x, nums)
|
||||
map(lambda *args: len(args), range(4))
|
||||
map(lambda **kwargs: len(kwargs), range(4))
|
||||
|
||||
# Ok because multiple arguments are allowed.
|
||||
dict(map(lambda k, v: (k, v), keys, values))
|
||||
|
|
|
|||
|
|
@ -89,7 +89,7 @@ pub(crate) fn unnecessary_map(
|
|||
ObjectType::Generator => {
|
||||
// Exclude the parent if already matched by other arms.
|
||||
if parent
|
||||
.and_then(ruff_python_ast::Expr::as_call_expr)
|
||||
.and_then(Expr::as_call_expr)
|
||||
.and_then(|call| call.func.as_name_expr())
|
||||
.is_some_and(|name| matches!(name.id.as_str(), "list" | "set" | "dict"))
|
||||
{
|
||||
|
|
@ -122,7 +122,7 @@ pub(crate) fn unnecessary_map(
|
|||
// Only flag, e.g., `list(map(lambda x: x + 1, iterable))`.
|
||||
let [Expr::Call(ast::ExprCall {
|
||||
func,
|
||||
arguments: Arguments { args, .. },
|
||||
arguments: Arguments { args, keywords, .. },
|
||||
..
|
||||
})] = args
|
||||
else {
|
||||
|
|
@ -133,6 +133,10 @@ pub(crate) fn unnecessary_map(
|
|||
return;
|
||||
}
|
||||
|
||||
if !keywords.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
let Some(argument) = helpers::first_argument_with_matching_function("map", func, args)
|
||||
else {
|
||||
return;
|
||||
|
|
@ -163,13 +167,21 @@ pub(crate) fn unnecessary_map(
|
|||
// Only flag, e.g., `dict(map(lambda v: (v, v ** 2), values))`.
|
||||
let [Expr::Call(ast::ExprCall {
|
||||
func,
|
||||
arguments: Arguments { args, .. },
|
||||
arguments: Arguments { args, keywords, .. },
|
||||
..
|
||||
})] = args
|
||||
else {
|
||||
return;
|
||||
};
|
||||
|
||||
if args.len() != 2 {
|
||||
return;
|
||||
}
|
||||
|
||||
if !keywords.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
let Some(argument) = helpers::first_argument_with_matching_function("map", func, args)
|
||||
else {
|
||||
return;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue