Python: Add AnnAssign DefinitionNode

This was a source of false positives for the
`py/uninitialized-local-variable` query, as exemplified by the test
case.
This commit is contained in:
Taus
2021-07-20 11:57:26 +00:00
committed by GitHub
parent f91e826781
commit 8b3fa789da
2 changed files with 5 additions and 1 deletions

View File

@@ -653,6 +653,8 @@ class DefinitionNode extends ControlFlowNode {
DefinitionNode() {
exists(Assign a | a.getATarget().getAFlowNode() = this)
or
exists(AnnAssign a | a.getTarget().getAFlowNode() = this and exists(a.getValue()))
or
exists(Alias a | a.getAsname().getAFlowNode() = this)
or
augstore(_, this)
@@ -795,6 +797,9 @@ private AstNode assigned_value(Expr lhs) {
/* lhs = result */
exists(Assign a | a.getATarget() = lhs and result = a.getValue())
or
/* lhs : annotation = result */
exists(AnnAssign a | a.getTarget() = lhs and result = a.getValue())
or
/* import result as lhs */
exists(Alias a | a.getAsname() = lhs and result = a.getValue())
or

View File

@@ -11,5 +11,4 @@
| UninitializedLocal.py:163:7:163:7 | x | Local variable 'x' may be used before it is initialized. |
| UninitializedLocal.py:176:16:176:16 | x | Local variable 'x' may be used before it is initialized. |
| UninitializedLocal.py:178:16:178:16 | y | Local variable 'y' may be used before it is initialized. |
| UninitializedLocal.py:294:14:294:22 | annotated | Local variable 'annotated' may be used before it is initialized. |
| odasa3987.py:11:8:11:10 | var | Local variable 'var' may be used before it is initialized. |