Skip to content

Commit 3e7986a

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 ec9e72e commit 3e7986a

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

24512469
private predicate startBbLikelyReachable(BasicBlock b) {

0 commit comments

Comments
 (0)