-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add query to ensure predicates starting with 'get' return a value #18164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
986e1cb
a763dd7
a5521b9
029b567
e33f7aa
96c1086
a462ec9
7c1aa84
67745e6
7db9b7d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /** | ||
| * @name Predicates starting with "get" should return a value | ||
| * @description Checks if predicates that start with "get" actually return a value. | ||
| * @kind problem | ||
| * @problem.severity warning | ||
| * @id ql/predicates-get-should-return-value | ||
| * @tags correctness | ||
| * maintainability | ||
| * @precision high | ||
| */ | ||
|
|
||
| import ql | ||
| import codeql_ql.ast.Ast | ||
|
|
||
| /** | ||
| * Identifies predicates whose names start with "get" 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[A-Z].*") } | ||
|
|
||
| /** | ||
| * Checks if a predicate has a return type. | ||
| */ | ||
| predicate hasReturnType(Predicate pred) { | ||
| exists(Type returnType | pred.getReturnType() = returnType) | ||
|
||
| } | ||
|
|
||
| /** | ||
| * Checks if a predicate is an alias using getAlias(). | ||
| */ | ||
| predicate isAlias(Predicate pred) { | ||
| pred instanceof ClasslessPredicate and exists(pred.(ClasslessPredicate).getAlias()) | ||
Napalys marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| from Predicate pred | ||
| where | ||
| isGetPredicate(pred) and | ||
| not hasReturnType(pred) and | ||
| not isAlias(pred) | ||
|
||
| select pred, "This predicate starts with 'get' but does not return a value." | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| | 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. | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| queries/style/ValidatePredicateGetReturns.ql |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import ql | ||
|
|
||
| // NOT OK -- Predicate starts with "get" but does not return a value | ||
| predicate getValue() { none() } | ||
|
|
||
| // OK -- starts with get and returns a value | ||
| string getData() { result = "data" } | ||
|
|
||
| // OK -- starts with get but followed by a lowercase letter, probably should be ignored | ||
| predicate getterFunction() { none() } | ||
|
|
||
| // OK -- starts with get and returns a value | ||
| string getImplementation() { result = "implementation" } | ||
|
|
||
| // OK -- is an alias | ||
| predicate getAlias = getImplementation/0; | ||
|
|
||
| // OK -- Starts with "get" but followed by a lowercase letter, probably be ignored | ||
| predicate getvalue() { none() } | ||
|
|
||
| // OK -- Does not start with "get", should be ignored | ||
| predicate retrieveValue() { none() } | ||
|
|
||
| // NOT OK -- starts with get and does not return value | ||
| predicate getImplementation2() { none() } | ||
|
|
||
| // OK -- is an alias | ||
| predicate getAlias2 = getImplementation2/0; |
Uh oh!
There was an error while loading. Please reload this page.