Skip to content

Commit aa1d49c

Browse files
committed
Add OmittableExists QL-for-QL query
1 parent 4480262 commit aa1d49c

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @name Omittable 'exists' argument
3+
* @description Writing 'exists(x | pred(x))' is bad practice and can be omitted.
4+
* @kind problem
5+
* @problem.severity warning
6+
* @precision high
7+
* @id ql/omittable-exists
8+
* @tags maintainability
9+
*/
10+
11+
import ql
12+
13+
from VarDecl existsArgument, VarAccess use
14+
where
15+
existsArgument = any(Exists e).getAnArgument() and
16+
strictcount(existsArgument.getAnAccess()) = 1 and
17+
use = existsArgument.getAnAccess() and
18+
exists(Call c, int argPos | c.getArgument(argPos) = use |
19+
not existsArgument.getType().getASuperType+() = c.getTarget().getParameterType(argPos)
20+
)
21+
select existsArgument, "This exists argument can be omitted by using a don't-care expression $@.",
22+
use, "in this argument"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| Test.qll:10:10:10:14 | i | This exists argument can be omitted by using a don't-care expression $@. | Test.qll:10:29:10:29 | i | in this argument |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/style/OmittableExists.ql
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
predicate aPredicate(int i) { none() }
2+
3+
predicate anotherPredicate(int i) { none() }
4+
5+
class SmallInt extends int {
6+
SmallInt() { this = [0 .. 10] }
7+
}
8+
9+
predicate test() {
10+
exists(int i | aPredicate(i)) // BAD
11+
or
12+
exists(int i | aPredicate(i) or anotherPredicate(i)) // BAD [NOT DETECTED]
13+
or
14+
exists(int i | aPredicate(i) and i.abs() = 0) // GOOD
15+
or
16+
exists(int i | aPredicate(i) and exists(int i2 | i = i2)) // GOOD
17+
or
18+
exists(SmallInt i | aPredicate(i)) // GOOD
19+
}

0 commit comments

Comments
 (0)