mirror of
https://github.com/github/codeql.git
synced 2026-04-30 19:26:02 +02:00
Python: Fix FP in py/unused-local-variable
This is only a temporary fix, as indicated by the TODO comment. The real underlying issue is the fact that `isUnused` is defined in terms of the underlying SSA variables (as these are only created for variables that are actually used), and the fact that annotated assignments are always considered to redefine their targets, which may not actually be the case. Thus, the correct fix would be to change the extractor to _disregard_ mere type annotations for the purposes of figuring out whether an SSA variable should be created or not. However, in the short term the present fix is likely sufficient.
This commit is contained in:
@@ -19,6 +19,7 @@ predicate unused_local(Name unused, LocalVariable v) {
|
||||
def.getVariable() = v and
|
||||
def.isUnused() and
|
||||
not exists(def.getARedef()) and
|
||||
not exists(annotation_without_assignment(v)) and
|
||||
def.isRelevant() and
|
||||
not v = any(Nonlocal n).getAVariable() and
|
||||
not exists(def.getNode().getParentNode().(FunctionDef).getDefinedFunction().getADecorator()) and
|
||||
@@ -26,6 +27,17 @@ predicate unused_local(Name unused, LocalVariable v) {
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets any annotation of the local variable `v` that does not also reassign its value.
|
||||
*
|
||||
* TODO: This predicate should not be needed. Rather, annotated "assignments" that do not actually
|
||||
* assign a value should not result in the creation of an SSA variable (which then goes unused).
|
||||
*/
|
||||
private AnnAssign annotation_without_assignment(LocalVariable v) {
|
||||
result.getTarget() = v.getAStore() and
|
||||
not exists(result.getValue())
|
||||
}
|
||||
|
||||
from Name unused, LocalVariable v
|
||||
where
|
||||
unused_local(unused, v) and
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
| type_annotation_fp.py:5:5:5:7 | foo | The value assigned to local variable 'foo' is never used. |
|
||||
| variables_test.py:29:5:29:5 | x | The value assigned to local variable 'x' is never used. |
|
||||
| variables_test.py:89:5:89:5 | a | The value assigned to local variable 'a' is never used. |
|
||||
| variables_test.py:89:7:89:7 | b | The value assigned to local variable 'b' is never used. |
|
||||
|
||||
Reference in New Issue
Block a user