[apex] AvoidNonRestrictiveQueries fix FP with multiline queries

This commit is contained in:
Andreas Dangel 2024-07-18 11:47:31 +02:00
parent fb6638c40d
commit ec92b39cf4
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
2 changed files with 25 additions and 2 deletions

View File

@ -24,8 +24,8 @@ 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 )|(limit )", Pattern.CASE_INSENSITIVE);
private static final Pattern SELECT_OR_FIND_PATTERN = Pattern.compile("(select|find )", Pattern.CASE_INSENSITIVE);
private static final Pattern RESTRICTIVE_PATTERN = Pattern.compile("(where\\s+)|(limit\\s+)", 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+[^)]+\\)");
@Override

View File

@ -237,6 +237,29 @@ public class Something {
List<List<SObject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (Id, Name WHERE Name like 'test'), Contact, Opportunity, Lead];
}
}
]]></code>
</test-code>
<test-code>
<description>False positives for SOQL queries with WHERE on multiple lines</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Something {
private static final String NAMESPACE_PREFIX = 'some_prefix';
private static final String PERMISSION_SET = 'some_permission';
public static Boolean hasPermission(Id userId) {
return ![
SELECT Id
FROM PermissionSetAssignment
WHERE
AssigneeId = :userId
AND PermissionSet.Name = :PERMISSION_SET
AND PermissionSet.NamespacePrefix = :NAMESPACE_PREFIX
]
.isEmpty();
}
}
]]></code>
</test-code>
</test-data>