Skip to content

Commit 8b3fa78

Browse files
authored
Python: Add AnnAssign DefinitionNode
This was a source of false positives for the `py/uninitialized-local-variable` query, as exemplified by the test case.
1 parent f91e826 commit 8b3fa78

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

python/ql/src/semmle/python/Flow.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,8 @@ class DefinitionNode extends ControlFlowNode {
653653
DefinitionNode() {
654654
exists(Assign a | a.getATarget().getAFlowNode() = this)
655655
or
656+
exists(AnnAssign a | a.getTarget().getAFlowNode() = this and exists(a.getValue()))
657+
or
656658
exists(Alias a | a.getAsname().getAFlowNode() = this)
657659
or
658660
augstore(_, this)
@@ -795,6 +797,9 @@ private AstNode assigned_value(Expr lhs) {
795797
/* lhs = result */
796798
exists(Assign a | a.getATarget() = lhs and result = a.getValue())
797799
or
800+
/* lhs : annotation = result */
801+
exists(AnnAssign a | a.getTarget() = lhs and result = a.getValue())
802+
or
798803
/* import result as lhs */
799804
exists(Alias a | a.getAsname() = lhs and result = a.getValue())
800805
or

python/ql/test/query-tests/Variables/undefined/UninitializedLocal.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@
1111
| UninitializedLocal.py:163:7:163:7 | x | Local variable 'x' may be used before it is initialized. |
1212
| UninitializedLocal.py:176:16:176:16 | x | Local variable 'x' may be used before it is initialized. |
1313
| UninitializedLocal.py:178:16:178:16 | y | Local variable 'y' may be used before it is initialized. |
14-
| UninitializedLocal.py:294:14:294:22 | annotated | Local variable 'annotated' may be used before it is initialized. |
1514
| odasa3987.py:11:8:11:10 | var | Local variable 'var' may be used before it is initialized. |

0 commit comments

Comments
 (0)