-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[flake8-comprehensions
] Detect overshadowed list
/set
/dict
, ignore variadics and named expressions (C417
)
#15955
Conversation
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. It's sort of difficult to see the actual fixes in between the refactors you did. Would you mind commenting on the diff to highlight the fixes for each bullet point?
crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_map.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_map.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_map.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_map.rs
Outdated
Show resolved
Hide resolved
…nore variadics and named expressions (`C417`)
The diff is a tad too big to reason about, so here's something else to the same effect.
if parent
.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"))
{
return;
} if parent_call_func.is_some_and(|func| is_list_set_or_dict(func, semantic)) {
return;
}
// ...
fn is_list_set_or_dict(func: &Expr, semantic: &SemanticModel) -> bool {
semantic
.resolve_qualified_name(func)
.is_some_and(|qualified_name| {
matches!(
qualified_name.segments(),
["" | "builtins", "list" | "set" | "dict"]
)
})
}
This check was present, but only for the // 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;
} Now, it is placed outside of the branches, along with a new check for starred expressions: for iterable in iterables {
// 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 iterable.is_starred_expr() {
return;
}
} Similarly, other pieces that are applicable to all branches have also been moved into |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect and thanks for the explanations. They were very helpful when reviewing the PR
Summary
Part of #15809 and #15876.
This change brings several bugfixes:
map()
call inlist(map(lambda x: x, []))
wherelist
is overshadowed is now correctly reported.map()
are variadic.Test Plan
cargo nextest run
andcargo insta test
.