Skip to content

Commit 986e1cb

Browse files
committed
Add ValidatePredicateGetReturns query and tests
1 parent bd56a35 commit 986e1cb

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @name Predicates starting with "get" should return a value
3+
* @description Checks if predicates that start with "get" actually return a value.
4+
* @kind problem
5+
* @problem.severity warning
6+
* @id ql/predicates-get-should-return-value
7+
* @tags correctness
8+
* maintainability
9+
* @precision high
10+
*/
11+
12+
import ql
13+
import codeql_ql.ast.Ast
14+
15+
/**
16+
* Identifies predicates whose names start with "get" followed by an uppercase letter.
17+
* This ensures that only predicates like "getValue" are matched, excluding names like "getter".
18+
*/
19+
predicate isGetPredicate(Predicate pred) { pred.getName().regexpMatch("get[A-Z].*") }
20+
21+
/**
22+
* Checks if a predicate has a return type.
23+
*/
24+
predicate hasReturnType(Predicate pred) {
25+
exists(Type returnType | pred.getReturnType() = returnType)
26+
}
27+
28+
/**
29+
* Checks if a predicate is an alias using getAlias().
30+
*/
31+
predicate isAlias(Predicate pred) {
32+
pred instanceof ClasslessPredicate and exists(pred.(ClasslessPredicate).getAlias())
33+
}
34+
35+
from Predicate pred
36+
where
37+
isGetPredicate(pred) and
38+
not hasReturnType(pred) and
39+
not isAlias(pred)
40+
select pred, "This predicate starts with 'get' but does not return a value."
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.qll:4:11:4:18 | ClasslessPredicate getValue | This predicate starts with 'get' but does not return a value. |
2+
| test.qll:25:11:25:28 | ClasslessPredicate getImplementation2 | This predicate starts with 'get' but does not return a value. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/style/ValidatePredicateGetReturns.ql
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import ql
2+
3+
// NOT OK -- Predicate starts with "get" but does not return a value
4+
predicate getValue() { none() }
5+
6+
// OK -- starts with get and returns a value
7+
string getData() { result = "data" }
8+
9+
// OK -- starts with get but followed by a lowercase letter, probably should be ignored
10+
predicate getterFunction() { none() }
11+
12+
// OK -- starts with get and returns a value
13+
string getImplementation() { result = "implementation" }
14+
15+
// OK -- is an alias
16+
predicate getAlias = getImplementation/0;
17+
18+
// OK -- Starts with "get" but followed by a lowercase letter, probably be ignored
19+
predicate getvalue() { none() }
20+
21+
// OK -- Does not start with "get", should be ignored
22+
predicate retrieveValue() { none() }
23+
24+
// NOT OK -- starts with get and does not return value
25+
predicate getImplementation2() { none() }
26+
27+
// OK -- is an alias
28+
predicate getAlias2 = getImplementation2/0;

0 commit comments

Comments
 (0)