Skip to content

Commit

Permalink
Respect async expressions in comprehension bodies (#11219)
Browse files Browse the repository at this point in the history
## Summary

We weren't recursing into the comprehension body.
  • Loading branch information
charliermarsh authored Apr 30, 2024
1 parent 4779dd1 commit 414990c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
5 changes: 5 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF029.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,8 @@ async def fail_4c(): # RUF029: the /inner/ function does not await

async def test():
return [check async for check in async_func()]


async def test() -> str:
vals = [str(val) for val in await async_func(1)]
return ",".join(vals)
18 changes: 10 additions & 8 deletions crates/ruff_linter/src/rules/ruff/rules/unused_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,6 @@ impl<'a> preorder::PreorderVisitor<'a> for AsyncExprVisitor {
preorder::TraversalSignal::Traverse
}
}
fn visit_expr(&mut self, expr: &'a Expr) {
match expr {
Expr::Await(_) => {
self.found_await_or_async = true;
}
_ => preorder::walk_expr(self, expr),
}
}
fn visit_stmt(&mut self, stmt: &'a Stmt) {
match stmt {
Stmt::With(ast::StmtWith { is_async: true, .. }) => {
Expand All @@ -84,9 +76,19 @@ impl<'a> preorder::PreorderVisitor<'a> for AsyncExprVisitor {
_ => preorder::walk_stmt(self, stmt),
}
}
fn visit_expr(&mut self, expr: &'a Expr) {
match expr {
Expr::Await(_) => {
self.found_await_or_async = true;
}
_ => preorder::walk_expr(self, expr),
}
}
fn visit_comprehension(&mut self, comprehension: &'a ast::Comprehension) {
if comprehension.is_async {
self.found_await_or_async = true;
} else {
preorder::walk_comprehension(self, comprehension);
}
}
}
Expand Down

0 comments on commit 414990c

Please sign in to comment.