Skip to content
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

Extend unncessary-generator-any-all to set comprehensions #3824

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions crates/ruff/resources/test/fixtures/flake8_pie/PIE802.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
all( # first comment
[x.id for x in bar], # second comment
) # third comment
any({x.id for x in bar})

# OK
all((x.id for x in bar))
all(x.id for x in bar)
all(x.id for x in bar)
any(x.id for x in bar)
any({x.id for x in bar})

all((x.id for x in bar))

async def f() -> bool:
return all([await use_greeting(greeting) for greeting in await greetings()])
41 changes: 19 additions & 22 deletions crates/ruff/src/rules/flake8_pie/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,29 +344,26 @@ pub fn unnecessary_comprehension_any_all(
func: &Expr,
args: &[Expr],
) {
if let ExprKind::Name { id, .. } = &func.node {
if (id == "all" || id == "any") && args.len() == 1 {
if !checker.ctx.is_builtin(id) {
return;
}
if let ExprKind::ListComp { elt, .. } = &args[0].node {
if is_async_generator(elt) {
return;
}
let mut diagnostic =
Diagnostic::new(UnnecessaryComprehensionAnyAll, Range::from(&args[0]));
if checker.patch(diagnostic.kind.rule()) {
diagnostic.try_set_fix(|| {
fixes::fix_unnecessary_comprehension_any_all(
checker.locator,
checker.stylist,
expr,
)
});
}
checker.diagnostics.push(diagnostic);
}
let ExprKind::Name { id, .. } = &func.node else {
return;
};
if (matches!(id.as_str(), "all" | "any")) && args.len() == 1 {
let (ExprKind::ListComp { elt, .. } | ExprKind::SetComp { elt, .. }) = &args[0].node else {
return;
};
if is_async_generator(elt) {
return;
}
if !checker.ctx.is_builtin(id) {
return;
}
let mut diagnostic = Diagnostic::new(UnnecessaryComprehensionAnyAll, Range::from(&args[0]));
if checker.patch(diagnostic.kind.rule()) {
diagnostic.try_set_fix(|| {
fixes::fix_unnecessary_comprehension_any_all(checker.locator, checker.stylist, expr)
});
}
checker.diagnostics.push(diagnostic);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,18 @@ expression: diagnostics
row: 9
column: 1
parent: ~
- kind:
name: UnnecessaryComprehensionAnyAll
body: Unnecessary list comprehension.
suggestion: Remove unnecessary list comprehension
fixable: true
location:
row: 10
column: 4
end_location:
row: 10
column: 23
fix:
edits: []
parent: ~