Skip to content

Commit 970349b

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 47421a6 commit 970349b

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
@@ -2417,6 +2417,24 @@ module Reachability {
24172417
// Exception edge from a node that is unlikely to raise
24182418
unlikelyToRaise(node) and
24192419
succ = node.getAnExceptionalSuccessor()
2420+
or
2421+
// True branch of `if False:` or `if TYPE_CHECKING:`
2422+
isAlwaysFalseGuard(node) and
2423+
succ = node.getATrueSuccessor()
2424+
}
2425+
2426+
/**
2427+
* Holds if `node` is a condition that is always `False` at runtime.
2428+
* This covers `if False:` and `if typing.TYPE_CHECKING:`.
2429+
*/
2430+
private predicate isAlwaysFalseGuard(ControlFlowNode node) {
2431+
node.getNode() instanceof False
2432+
or
2433+
node =
2434+
API::moduleImport("typing")
2435+
.getMember("TYPE_CHECKING")
2436+
.getAValueReachableFromSource()
2437+
.asCfgNode()
24202438
}
24212439

24222440
private predicate startBbLikelyReachable(BasicBlock b) {

0 commit comments

Comments
 (0)