[apex] AvoidNonRestrictiveQueries: Fix regex for detecting LIMIT clause

Fixes #5270
This commit is contained in:
Andreas Dangel 2024-10-11 11:57:10 +02:00
parent 00bf6fe2f7
commit c595fea83f
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
3 changed files with 33 additions and 1 deletions

View File

@ -30,6 +30,8 @@ after JUnit / JUnit 4, even when they applied to JUnit 5 and / or TestNG.
The old rule names still work but are deprecated.
### 🐛 Fixed Issues
* apex-performance
* [#5270](https://github.com/pmd/pmd/issues/5270): \[apex] AvoidNonRestrictiveQueries when LIMIT is followed by bind expression
* java
* [#4532](https://github.com/pmd/pmd/issues/4532): \[java] Rule misnomer for JUnit* rules
* java-errorprone

View File

@ -24,7 +24,7 @@ import net.sourceforge.pmd.lang.rule.RuleTargetSelector;
import net.sourceforge.pmd.reporting.RuleContext;
public class AvoidNonRestrictiveQueriesRule extends AbstractApexRule {
private static final Pattern RESTRICTIVE_PATTERN = Pattern.compile("(where\\s+)|(limit\\s+)", Pattern.CASE_INSENSITIVE);
private static final Pattern RESTRICTIVE_PATTERN = Pattern.compile("\\b(where|limit)\\b", Pattern.CASE_INSENSITIVE);
private static final Pattern SELECT_OR_FIND_PATTERN = Pattern.compile("(select\\s+|find\\s+)", Pattern.CASE_INSENSITIVE);
private static final Pattern SUB_QUERY_PATTERN = Pattern.compile("(?i)\\(\\s*select\\s+[^)]+\\)");

View File

@ -260,6 +260,36 @@ public class Something {
.isEmpty();
}
}
]]></code>
</test-code>
<test-code>
<description>[apex] AvoidNonRestrictiveQueries when LIMIT is followed by bind expression #5270</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public with sharing class DemoController {
public static final Integer LIMIT_ACCOUNTS = 2;
@AuraEnabled
public static List<Account> getTwoAccounts() {
List<Account> result = [
SELECT Id, Name FROM Account WITH SECURITY_ENFORCED
LIMIT:LIMIT_ACCOUNTS // note: no spaces... - false positive here
];
List<Account> result2 = [
SELECT Id, Name FROM Account WITH SECURITY_ENFORCED
LIMIT :LIMIT_ACCOUNTS
];
List<Account> result3 = [
SELECT Id, Name FROM Account WITH SECURITY_ENFORCED
LIMIT : LIMIT_ACCOUNTS
];
// sosl:
List<List<SObject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead LIMIT:LIMIT_ACCOUNTS];
return result;
}
}
]]></code>
</test-code>
</test-data>