Skip to content

Commit 073e9bc

Browse files
authored
Merge pull request #11173 from erik-krogh/notDead
QL: improve the dead-code query
2 parents b1e6a86 + c5fece7 commit 073e9bc

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

ql/ql/src/codeql_ql/style/DeadCodeQuery.qll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ private AstNode alive() {
7777
or
7878
// recursive cases
7979
result = aliveStep(alive())
80+
or
81+
// cached predicates inside a cached module, because they can group cached predicate.
82+
// this is deliberately not part of `aliveStep`, as it only means the predicate is live, but not if it's queryable.
83+
exists(Module mod, ClasslessPredicate pred | pred = alive() |
84+
not pred.isPrivate() and
85+
not result.(ClasslessPredicate).isPrivate() and
86+
pred.hasAnnotation("cached") and
87+
result.hasAnnotation("cached") and
88+
pred.getParent() = mod and
89+
result.getParent() = mod and
90+
mod.hasAnnotation("cached")
91+
)
8092
}
8193

8294
private AstNode aliveStep(AstNode prev) {
@@ -172,6 +184,8 @@ private AstNode aliveStep(AstNode prev) {
172184
or
173185
// the implements of a module
174186
result = prev.(Module).getImplements(_)
187+
or
188+
result = prev.(PredicateExpr).getQualifier()
175189
}
176190

177191
private AstNode deprecated() {

ql/ql/test/queries/style/DeadCode/Foo.qll

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,26 @@ private module Mixed {
77
}
88

99
predicate usesAlive() { Mixed::alive1() }
10+
11+
cached
12+
private module Cached {
13+
cached
14+
predicate isUsed() { any() }
15+
16+
cached
17+
predicate isNotUsed() { any() }
18+
}
19+
20+
module UseCache {
21+
private import Cached
22+
23+
predicate usesCached() { isUsed() }
24+
}
25+
26+
private module Foo {
27+
signature predicate bar();
28+
}
29+
30+
module ValidationMethod<Foo::bar/0 sig> {
31+
predicate impl() { sig() }
32+
}

0 commit comments

Comments
 (0)