Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
tausbn committed Apr 8, 2026
commit 3e7986a14a61147239653e036d18d7de055457ef
Original file line number Diff line number Diff line change
Expand Up @@ -2446,6 +2446,24 @@ module Reachability {
// Exception edge from a node that is unlikely to raise
unlikelyToRaise(node) and
succ = node.getAnExceptionalSuccessor()
or
// True branch of `if False:` or `if TYPE_CHECKING:`
isAlwaysFalseGuard(node) and
succ = node.getATrueSuccessor()
Comment on lines +2483 to +2484
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a bit specific and ad-hoc. Do we happen to know that this is the prevalent pattern? I guess if false is a quick way to comment out stuff. Did the extractor prune these at some point?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was indeed added to remove a bunch of false positives. I think these were implicitly pruned by the reachability check inherent to points-to (i.e. the fact that unreachable branches simply have no points-to information available).

}

/**
* Holds if `node` is a condition that is always `False` at runtime.
* This covers `if False:` and `if typing.TYPE_CHECKING:`.
*/
private predicate isAlwaysFalseGuard(ControlFlowNode node) {
node.getNode() instanceof False
or
node =
API::moduleImport("typing")
.getMember("TYPE_CHECKING")
.getAValueReachableFromSource()
.asCfgNode()
}

private predicate startBbLikelyReachable(BasicBlock b) {
Expand Down