Skip to content

Commit 87207ff

Browse files
guillaume-dequennesonartech
authored andcommitted
SONARPY-2961 Fix FP when the task is within a return statement (#287)
GitOrigin-RevId: f64bfddfa6414c46a1d54dab2d440f39a5f2e189
1 parent 32c9133 commit 87207ff

2 files changed

Lines changed: 4 additions & 1 deletion

File tree

python-checks/src/main/java/org/sonar/python/checks/AsyncioTaskNotStoredCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private void checkCallExpression(SubscriptionContext ctx) {
5656
if (asyncioTaskTypeChecks.stream().noneMatch(t -> t.check(calleeType) == TriBool.TRUE)) {
5757
return;
5858
}
59-
if (TreeUtils.firstAncestorOfKind(callExpression, Tree.Kind.ASSIGNMENT_STMT, Tree.Kind.ASSIGNMENT_EXPRESSION, Tree.Kind.CALL_EXPR) == null) {
59+
if (TreeUtils.firstAncestorOfKind(callExpression, Tree.Kind.ASSIGNMENT_STMT, Tree.Kind.ASSIGNMENT_EXPRESSION, Tree.Kind.CALL_EXPR, Tree.Kind.RETURN_STMT) == null) {
6060
ctx.addIssue(callee, MESSAGE);
6161
}
6262
}

python-checks/src/test/resources/checks/asyncioTaskNotStored.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ async def compliant_examples():
2828
asyncio.create_task(another_coroutine())
2929
)
3030

31+
async def create_and_return_task():
32+
return asyncio.create_task(some_coroutine()) # Compliant
33+
3134
async def some_coroutine():
3235
await asyncio.sleep(1)
3336
return {"result": "value"}

0 commit comments

Comments
 (0)