Skip to content

Commit 5307b9e

Browse files
committed
Python: Extend reachability analysis with common guards
Adds `if False: ...` and `if typing.TYPE_CHECKING: ...` to the set of nodes that are unlikely to be reachable.
1 parent 72a0b06 commit 5307b9e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

python/ql/lib/semmle/python/dataflow/new/internal/DataFlowDispatch.qll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2379,6 +2379,24 @@ module Reachability {
23792379
succ = node.getASuccessor() and
23802380
not succ = node.getAnExceptionalSuccessor() and
23812381
not succ.getNode() instanceof Yield
2382+
or
2383+
// True branch of `if False:` or `if TYPE_CHECKING:`
2384+
isAlwaysFalseGuard(node) and
2385+
succ = node.getATrueSuccessor()
2386+
}
2387+
2388+
/**
2389+
* Holds if `node` is a condition that is always `False` at runtime.
2390+
* This covers `if False:` and `if typing.TYPE_CHECKING:`.
2391+
*/
2392+
private predicate isAlwaysFalseGuard(ControlFlowNode node) {
2393+
node.getNode() instanceof False
2394+
or
2395+
node =
2396+
API::moduleImport("typing")
2397+
.getMember("TYPE_CHECKING")
2398+
.getAValueReachableFromSource()
2399+
.asCfgNode()
23822400
}
23832401

23842402
private predicate startBbLikelyReachable(BasicBlock b) {

0 commit comments

Comments
 (0)