Skip to content

Commit a57756f

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 4dbb223 commit a57756f

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
@@ -2396,6 +2396,24 @@ module Reachability {
23962396
// Exception edge from a node that is unlikely to raise
23972397
unlikelyToRaise(node) and
23982398
succ = node.getAnExceptionalSuccessor()
2399+
or
2400+
// True branch of `if False:` or `if TYPE_CHECKING:`
2401+
isAlwaysFalseGuard(node) and
2402+
succ = node.getATrueSuccessor()
2403+
}
2404+
2405+
/**
2406+
* Holds if `node` is a condition that is always `False` at runtime.
2407+
* This covers `if False:` and `if typing.TYPE_CHECKING:`.
2408+
*/
2409+
private predicate isAlwaysFalseGuard(ControlFlowNode node) {
2410+
node.getNode() instanceof False
2411+
or
2412+
node =
2413+
API::moduleImport("typing")
2414+
.getMember("TYPE_CHECKING")
2415+
.getAValueReachableFromSource()
2416+
.asCfgNode()
23992417
}
24002418

24012419
private predicate startBbLikelyReachable(BasicBlock b) {

0 commit comments

Comments
 (0)