From 67745e6332564b343537ab63e08330a056eeed4f Mon Sep 17 00:00:00 2001 From: Napalys Date: Mon, 2 Dec 2024 09:10:54 +0100 Subject: [PATCH] Reused isGetPredicate to retrieve the prefix of the predicate --- .../style/ValidatePredicateGetReturns.ql | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/ql/ql/src/queries/style/ValidatePredicateGetReturns.ql b/ql/ql/src/queries/style/ValidatePredicateGetReturns.ql index f65b883a09b0..4d0196509c33 100644 --- a/ql/ql/src/queries/style/ValidatePredicateGetReturns.ql +++ b/ql/ql/src/queries/style/ValidatePredicateGetReturns.ql @@ -16,7 +16,9 @@ import codeql_ql.ast.Ast * Identifies predicates whose names start with "get", "as" followed by an uppercase letter. * This ensures that only predicates like "getValue" are matched, excluding names like "getter". */ -predicate isGetPredicate(Predicate pred) { pred.getName().regexpMatch("(get|as)[A-Z].*") } +predicate isGetPredicate(Predicate pred, string prefix) { + prefix = pred.getName().regexpCapture("(get|as)[A-Z].*", 1) +} /** * Checks if a predicate has a return type. @@ -28,21 +30,9 @@ predicate hasReturnType(Predicate pred) { exists(pred.getReturnTypeExpr()) } */ predicate isAlias(Predicate pred) { exists(pred.(ClasslessPredicate).getAlias()) } -/** - * Returns "get" if the predicate name starts with "get", otherwise "as". - */ -string getPrefix(Predicate pred) { - if pred.getName().matches("get%") - then result = "get" - else - if pred.getName().matches("as%") - then result = "as" - else result = "" -} - -from Predicate pred +from Predicate pred, string prefix where - isGetPredicate(pred) and + isGetPredicate(pred, prefix) and not hasReturnType(pred) and not isAlias(pred) -select pred, "This predicate starts with '" + getPrefix(pred) + "' but does not return a value." +select pred, "This predicate starts with '" + prefix + "' but does not return a value."