Skip to content

Commit e2ede47

Browse files
committed
Python: treat await expressions as side-effecting
Prevent ineffectual-statement false positives when awaiting a task or coroutine without assigning its returned value. Add regression cases for both forms. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: f0800d30-e4f9-463c-a0d8-660fe1aceaf1
1 parent 69787a2 commit e2ede47

3 files changed

Lines changed: 21 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Await expressions are now treated as having side effects. This prevents false positives from the "Statement has no effect" query (`py/ineffectual-statement`) when an awaited task or coroutine's result is discarded.

‎python/ql/lib/semmle/python/Exprs.qll‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,8 @@ class None extends NameConstant {
623623
class Await extends Await_ {
624624
/* syntax: await Expr */
625625
override Expr getASubExpression() { result = this.getValue() }
626+
627+
override predicate hasSideEffects() { any() }
626628
}
627629

628630
/** A formatted string literal expression, such as `f'hello {world!s}'` */
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import asyncio
2+
3+
4+
async def returns_value():
5+
return 42
6+
7+
8+
async def await_task_result():
9+
task = asyncio.create_task(returns_value())
10+
await task
11+
12+
13+
async def await_coroutine_result():
14+
coroutine = returns_value()
15+
await coroutine

0 commit comments

Comments
 (0)