mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-12 00:45:28 +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 x=1: x, nums)
|
||||||
map(lambda *args: len(args), range(4))
|
map(lambda *args: len(args), range(4))
|
||||||
map(lambda **kwargs: len(kwargs), 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 => {
|
ObjectType::Generator => {
|
||||||
// Exclude the parent if already matched by other arms.
|
// Exclude the parent if already matched by other arms.
|
||||||
if parent
|
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())
|
.and_then(|call| call.func.as_name_expr())
|
||||||
.is_some_and(|name| matches!(name.id.as_str(), "list" | "set" | "dict"))
|
.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))`.
|
// Only flag, e.g., `list(map(lambda x: x + 1, iterable))`.
|
||||||
let [Expr::Call(ast::ExprCall {
|
let [Expr::Call(ast::ExprCall {
|
||||||
func,
|
func,
|
||||||
arguments: Arguments { args, .. },
|
arguments: Arguments { args, keywords, .. },
|
||||||
..
|
..
|
||||||
})] = args
|
})] = args
|
||||||
else {
|
else {
|
||||||
|
|
@ -133,6 +133,10 @@ pub(crate) fn unnecessary_map(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !keywords.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let Some(argument) = helpers::first_argument_with_matching_function("map", func, args)
|
let Some(argument) = helpers::first_argument_with_matching_function("map", func, args)
|
||||||
else {
|
else {
|
||||||
return;
|
return;
|
||||||
|
|
@ -163,13 +167,21 @@ pub(crate) fn unnecessary_map(
|
||||||
// Only flag, e.g., `dict(map(lambda v: (v, v ** 2), values))`.
|
// Only flag, e.g., `dict(map(lambda v: (v, v ** 2), values))`.
|
||||||
let [Expr::Call(ast::ExprCall {
|
let [Expr::Call(ast::ExprCall {
|
||||||
func,
|
func,
|
||||||
arguments: Arguments { args, .. },
|
arguments: Arguments { args, keywords, .. },
|
||||||
..
|
..
|
||||||
})] = args
|
})] = args
|
||||||
else {
|
else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if args.len() != 2 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if !keywords.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let Some(argument) = helpers::first_argument_with_matching_function("map", func, args)
|
let Some(argument) = helpers::first_argument_with_matching_function("map", func, args)
|
||||||
else {
|
else {
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue