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

Respect async expressions in comprehension bodies #11219

Merged
merged 1 commit into from
Apr 30, 2024
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: 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),
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(No change to this method, just reordered to match trait definition.)

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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the fix.

}
}
}
Expand Down
Loading