forked from phoedos/pmd
Merge branch 'issue-2598'
This commit is contained in:
@ -25,6 +25,8 @@ This is a {{ site.pmd.release_type }} release.
|
||||
* [#2610](https://github.com/pmd/pmd/pull/2610): \[apex] Support top-level enums in rules
|
||||
* apex-bestpractices
|
||||
* [#2626](https://github.com/pmd/pmd/issues/2626): \[apex] UnusedLocalVariable - false positive on case insensitivity allowed in Apex
|
||||
* apex-performance
|
||||
* [#2598](https://github.com/pmd/pmd/issues/2598): \[apex] AvoidSoqlInLoops false positive for SOQL with in For-Loop
|
||||
* apex-security
|
||||
* [#2620](https://github.com/pmd/pmd/issues/2620): \[visualforce] False positive on VfUnescapeEl with new Message Channel feature
|
||||
* core
|
||||
|
@ -1,9 +1,10 @@
|
||||
/**
|
||||
/*
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.apex.rule.performance;
|
||||
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTBlockStatement;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTDoLoopStatement;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTForEachStatement;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTForLoopStatement;
|
||||
@ -25,7 +26,7 @@ public class AvoidSoqlInLoopsRule extends AbstractApexRule {
|
||||
|
||||
@Override
|
||||
public Object visit(ASTSoqlExpression node, Object data) {
|
||||
if (insideLoop(node) && parentNotReturn(node) && parentNotForEach(node)) {
|
||||
if (insideLoop(node) && parentNotReturn(node)) {
|
||||
addViolation(data, node);
|
||||
}
|
||||
return data;
|
||||
@ -35,16 +36,16 @@ public class AvoidSoqlInLoopsRule extends AbstractApexRule {
|
||||
return !(node.getParent() instanceof ASTReturnStatement);
|
||||
}
|
||||
|
||||
private boolean parentNotForEach(ASTSoqlExpression node) {
|
||||
return !(node.getParent() instanceof ASTForEachStatement);
|
||||
}
|
||||
|
||||
private boolean insideLoop(ASTSoqlExpression node) {
|
||||
Node n = node.getParent();
|
||||
|
||||
while (n != null) {
|
||||
if (n instanceof ASTBlockStatement && n.getParent() instanceof ASTForEachStatement) {
|
||||
// only consider the block of the for-each statement, not the iterator
|
||||
return true;
|
||||
}
|
||||
if (n instanceof ASTDoLoopStatement || n instanceof ASTWhileLoopStatement
|
||||
|| n instanceof ASTForLoopStatement || n instanceof ASTForEachStatement) {
|
||||
|| n instanceof ASTForLoopStatement) {
|
||||
return true;
|
||||
}
|
||||
n = n.getParent();
|
||||
|
@ -98,6 +98,21 @@ public class Foo {
|
||||
for(Account a : [SELECT Id FROM Account]) {
|
||||
}
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>[apex] AvoidSoqlInLoops false positive for SOQL with in For-Loop #2598</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
public void bar() {
|
||||
for(NAMESPACE__CustomObject__c customObject : DatabaseUtility.query([Select Id FROM NAMESPACE__CustomObject__c where id IN :setIds])) // <-- violation is reported here
|
||||
{
|
||||
// …
|
||||
}
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
Reference in New Issue
Block a user