Skip to content

Commit

Permalink
Now flag aliases with the 'get' or 'as' prefix that resolve to predic…
Browse files Browse the repository at this point in the history
…ates lacking a return type.

Co-authored-by: asgerf <[email protected]>
  • Loading branch information
Napalys and asgerf committed Dec 2, 2024
1 parent 67745e6 commit 01b62ad
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
17 changes: 8 additions & 9 deletions ql/ql/src/queries/style/ValidatePredicateGetReturns.ql
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@ predicate isGetPredicate(Predicate pred, string prefix) {
}

/**
* Checks if a predicate has a return type.
* Checks if a predicate has a return type. This is phrased negatively to not flag unresolved aliases.
*/
predicate hasReturnType(Predicate pred) { exists(pred.getReturnTypeExpr()) }

/**
* Checks if a predicate is an alias using getAlias().
*/
predicate isAlias(Predicate pred) { exists(pred.(ClasslessPredicate).getAlias()) }
predicate hasNoReturnType(Predicate pred) {
not exists(pred.getReturnTypeExpr()) and
not pred.(ClasslessPredicate).getAlias() instanceof PredicateExpr
or
hasNoReturnType(pred.(ClasslessPredicate).getAlias().(PredicateExpr).getResolvedPredicate())
}

from Predicate pred, string prefix
where
isGetPredicate(pred, prefix) and
not hasReturnType(pred) and
not isAlias(pred)
hasNoReturnType(pred)
select pred, "This predicate starts with '" + prefix + "' but does not return a value."
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
| test.qll:4:11:4:18 | ClasslessPredicate getValue | This predicate starts with 'get' but does not return a value. |
| test.qll:25:11:25:28 | ClasslessPredicate getImplementation2 | This predicate starts with 'get' but does not return a value. |
| test.qll:28:11:28:19 | ClasslessPredicate getAlias2 | This predicate starts with 'get' but does not return a value. |
| test.qll:31:11:31:17 | ClasslessPredicate asValue | This predicate starts with 'as' but does not return a value. |
| test.qll:48:11:48:19 | ClasslessPredicate getAlias4 | This predicate starts with 'get' but does not return a value. |
| test.qll:61:11:61:22 | ClasslessPredicate getDistance2 | This predicate starts with 'get' but does not return a value. |
27 changes: 26 additions & 1 deletion ql/ql/test/queries/style/ValidatePredicateGetReturns/test.qll
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ predicate retrieveValue() { none() }
// NOT OK -- starts with get and does not return value
predicate getImplementation2() { none() }

// OK -- is an alias
// NOT OK -- is an alias for a predicate which does not have a return value
predicate getAlias2 = getImplementation2/0;

// NOT OK -- starts with as and does not return value
Expand All @@ -40,3 +40,28 @@ string asString() { result = "string" }
HiddenType getInjectableCompositeActionNode() {
exists(HiddenType hidden | result = hidden.toString())
}

// OK -- starts with get and does not return value
predicate implementation4() { none() }

// NOT OK -- is an alias
predicate getAlias4 = implementation4/0;

// OK -- is an alias
predicate alias5 = implementation4/0;

int root() { none() }

predicate edge(int x, int y) { none() }

// OK -- Higher-order predicate
int getDistance(int x) = shortestDistances(root/0, edge/2)(_, x, result)

// NOT OK -- Higher-order predicate that does not return a value even though has 'get' in the name
predicate getDistance2(int x, int y) = shortestDistances(root/0, edge/2)(_, x, y)

// OK
predicate unresolvedAlias = unresolved/0;

// NOT OK -- unresolved alias
predicate getUnresolvedAlias = unresolved/0;

0 comments on commit 01b62ad

Please sign in to comment.