Skip to content

Commit 9e2ac8b

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 9baa262 commit 9e2ac8b

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
@@ -2441,6 +2441,24 @@ module Reachability {
24412441
// Exception edge from a node that is unlikely to raise
24422442
unlikelyToRaise(node) and
24432443
succ = node.getAnExceptionalSuccessor()
2444+
or
2445+
// True branch of `if False:` or `if TYPE_CHECKING:`
2446+
isAlwaysFalseGuard(node) and
2447+
succ = node.getATrueSuccessor()
2448+
}
2449+
2450+
/**
2451+
* Holds if `node` is a condition that is always `False` at runtime.
2452+
* This covers `if False:` and `if typing.TYPE_CHECKING:`.
2453+
*/
2454+
private predicate isAlwaysFalseGuard(ControlFlowNode node) {
2455+
node.getNode() instanceof False
2456+
or
2457+
node =
2458+
API::moduleImport("typing")
2459+
.getMember("TYPE_CHECKING")
2460+
.getAValueReachableFromSource()
2461+
.asCfgNode()
24442462
}
24452463

24462464
private predicate startBbLikelyReachable(BasicBlock b) {

0 commit comments

Comments
 (0)