File tree Expand file tree Collapse file tree 4 files changed +71
-0
lines changed
test/queries/style/ValidatePredicateGetReturns Expand file tree Collapse file tree 4 files changed +71
-0
lines changed Original file line number Diff line number Diff line change
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."
Original file line number Diff line number Diff line change
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. |
Original file line number Diff line number Diff line change
1
+ queries/style/ValidatePredicateGetReturns.ql
Original file line number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments