Skip to content

Commit f6b201c

Browse files
committed
Python: Port UnusedImport.ql
Changes the "has points-to value" check into a "is reachable" check instead. No test changes.
1 parent 37ce948 commit f6b201c

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

python/ql/src/Imports/UnusedImport.ql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
*/
1313

1414
import python
15-
private import LegacyPointsTo
1615
import Variables.Definition
1716
import semmle.python.ApiGraphs
17+
private import semmle.python.dataflow.new.internal.ImportResolution
18+
private import semmle.python.dataflow.new.internal.DataFlowDispatch
1819

1920
private predicate is_pytest_fixture(Import imp, Variable name) {
2021
exists(Alias a, API::Node pytest_fixture, API::Node decorator |
@@ -95,7 +96,7 @@ private string typehint_annotation_in_module(Module module_scope) {
9596
or
9697
annotation = any(FunctionExpr f).getReturns().getASubExpression*()
9798
|
98-
annotation.(ExprWithPointsTo).pointsTo(Value::forString(result)) and
99+
annotation.getText() = result and
99100
annotation.getEnclosingModule() = module_scope
100101
)
101102
}
@@ -143,9 +144,8 @@ predicate unused_import(Import imp, Variable name) {
143144
not imported_module_used_in_doctest(imp) and
144145
not imported_alias_used_in_typehint(imp, name) and
145146
not is_pytest_fixture(imp, name) and
146-
// Only consider import statements that actually point-to something (possibly an unknown module).
147-
// If this is not the case, it's likely that the import statement never gets executed.
148-
imp.getAName().getValue().(ExprWithPointsTo).pointsTo(_)
147+
// Only consider import statements in reachable code.
148+
Reachability::likelyReachable(imp.getAName().getValue().getAFlowNode().getBasicBlock())
149149
}
150150

151151
from Stmt s, Variable name

0 commit comments

Comments
 (0)