mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 10:49:50 +00:00
Avoid unnecessary-comprehension-any-all
for async generators (#3823)
This commit is contained in:
parent
54ad9397e5
commit
9de1f82658
3 changed files with 38 additions and 26 deletions
|
@ -1,11 +1,4 @@
|
|||
# no error
|
||||
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})
|
||||
|
||||
# PIE 802
|
||||
# PIE802
|
||||
any([x.id for x in bar])
|
||||
all([x.id for x in bar])
|
||||
any( # first comment
|
||||
|
@ -14,3 +7,14 @@ any( # first comment
|
|||
all( # first comment
|
||||
[x.id for x in bar], # second comment
|
||||
) # third comment
|
||||
|
||||
# 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})
|
||||
|
||||
|
||||
async def f() -> bool:
|
||||
return all([await use_greeting(greeting) for greeting in await greetings()])
|
||||
|
|
|
@ -12,7 +12,7 @@ use ruff_diagnostics::{AlwaysAutofixableViolation, Violation};
|
|||
use ruff_diagnostics::{Diagnostic, Edit};
|
||||
use ruff_macros::{derive_message_formats, violation};
|
||||
use ruff_python_ast::comparable::ComparableExpr;
|
||||
use ruff_python_ast::helpers::{create_expr, match_trailing_comment, unparse_expr};
|
||||
use ruff_python_ast::helpers::{any_over_expr, create_expr, match_trailing_comment, unparse_expr};
|
||||
use ruff_python_ast::types::{Range, RefEquality};
|
||||
use ruff_python_stdlib::identifiers::is_identifier;
|
||||
use ruff_python_stdlib::keyword::KWLIST;
|
||||
|
@ -332,6 +332,11 @@ pub fn unnecessary_spread(checker: &mut Checker, keys: &[Option<Expr>], values:
|
|||
}
|
||||
}
|
||||
|
||||
/// Return `true` if the `Expr` contains an `await` expression.
|
||||
fn is_async_generator(expr: &Expr) -> bool {
|
||||
any_over_expr(expr, &|expr| matches!(expr.node, ExprKind::Await { .. }))
|
||||
}
|
||||
|
||||
/// PIE802
|
||||
pub fn unnecessary_comprehension_any_all(
|
||||
checker: &mut Checker,
|
||||
|
@ -344,7 +349,10 @@ pub fn unnecessary_comprehension_any_all(
|
|||
if !checker.ctx.is_builtin(id) {
|
||||
return;
|
||||
}
|
||||
if let ExprKind::ListComp { .. } = args[0].node {
|
||||
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()) {
|
||||
|
|
|
@ -8,19 +8,19 @@ expression: diagnostics
|
|||
suggestion: Remove unnecessary list comprehension
|
||||
fixable: true
|
||||
location:
|
||||
row: 9
|
||||
row: 2
|
||||
column: 4
|
||||
end_location:
|
||||
row: 9
|
||||
row: 2
|
||||
column: 23
|
||||
fix:
|
||||
edits:
|
||||
- content: any(x.id for x in bar)
|
||||
location:
|
||||
row: 9
|
||||
row: 2
|
||||
column: 0
|
||||
end_location:
|
||||
row: 9
|
||||
row: 2
|
||||
column: 24
|
||||
parent: ~
|
||||
- kind:
|
||||
|
@ -29,19 +29,19 @@ expression: diagnostics
|
|||
suggestion: Remove unnecessary list comprehension
|
||||
fixable: true
|
||||
location:
|
||||
row: 10
|
||||
row: 3
|
||||
column: 4
|
||||
end_location:
|
||||
row: 10
|
||||
row: 3
|
||||
column: 23
|
||||
fix:
|
||||
edits:
|
||||
- content: all(x.id for x in bar)
|
||||
location:
|
||||
row: 10
|
||||
row: 3
|
||||
column: 0
|
||||
end_location:
|
||||
row: 10
|
||||
row: 3
|
||||
column: 24
|
||||
parent: ~
|
||||
- kind:
|
||||
|
@ -50,19 +50,19 @@ expression: diagnostics
|
|||
suggestion: Remove unnecessary list comprehension
|
||||
fixable: true
|
||||
location:
|
||||
row: 12
|
||||
row: 5
|
||||
column: 4
|
||||
end_location:
|
||||
row: 12
|
||||
row: 5
|
||||
column: 23
|
||||
fix:
|
||||
edits:
|
||||
- content: "any( # first comment\n x.id for x in bar # second comment\n)"
|
||||
location:
|
||||
row: 11
|
||||
row: 4
|
||||
column: 0
|
||||
end_location:
|
||||
row: 13
|
||||
row: 6
|
||||
column: 1
|
||||
parent: ~
|
||||
- kind:
|
||||
|
@ -71,19 +71,19 @@ expression: diagnostics
|
|||
suggestion: Remove unnecessary list comprehension
|
||||
fixable: true
|
||||
location:
|
||||
row: 15
|
||||
row: 8
|
||||
column: 4
|
||||
end_location:
|
||||
row: 15
|
||||
row: 8
|
||||
column: 23
|
||||
fix:
|
||||
edits:
|
||||
- content: "all( # first comment\n x.id for x in bar # second comment\n)"
|
||||
location:
|
||||
row: 14
|
||||
row: 7
|
||||
column: 0
|
||||
end_location:
|
||||
row: 16
|
||||
row: 9
|
||||
column: 1
|
||||
parent: ~
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue