[java] ArrayIsStoredDirectly - fix tree traversal

Fixes #3879
This commit is contained in:
Andreas Dangel
2022-04-21 14:12:47 +02:00
parent a6d8c1b646
commit 8d5c2e5683
3 changed files with 25 additions and 2 deletions

View File

@ -22,6 +22,7 @@ This is a {{ site.pmd.release_type }} release.
* doc
* [#2505](https://github.com/pmd/pmd/issues/2505): \[doc] Improve side bar to show release date
* java
* [#3879](https://github.com/pmd/pmd/issues/3879) \[java] ArrayIsStoredDirectly reports duplicated violation
* [#3889](https://github.com/pmd/pmd/pull/3889): \[java] Catch LinkageError in UselessOverridingMethodRule
* plsql
* [#3706](https://github.com/pmd/pmd/issues/3706): \[plsql] Parsing exception CURSOR statement with parenthesis groupings

View File

@ -19,6 +19,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
import net.sourceforge.pmd.lang.java.ast.ASTStatement;
import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.properties.PropertyDescriptor;
@ -105,9 +106,12 @@ public class ArrayIsStoredDirectlyRule extends AbstractSunSecureRule {
final ASTVariableDeclaratorId vid = parameter.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
final String varName = vid.getImage();
for (ASTBlockStatement b : bs) {
if (b.hasDescendantOfType(ASTAssignmentOperator.class)) {
if (b.getChild(0) instanceof ASTStatement && b.getChild(0).getChild(0) instanceof ASTStatementExpression) {
final ASTStatementExpression se = b.getFirstDescendantOfType(ASTStatementExpression.class);
if (se == null || !(se.getChild(0) instanceof ASTPrimaryExpression)) {
if (se == null
|| se.getNumChildren() < 2
|| !(se.getChild(0) instanceof ASTPrimaryExpression)
|| !(se.getChild(1) instanceof ASTAssignmentOperator)) {
continue;
}
String assignedVar = getExpressionVarName(se);

View File

@ -269,4 +269,22 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description>[java] ArrayIsStoredDirectly reports duplicated violation #3879</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>5</expected-linenumbers>
<code><![CDATA[
public class ExampleAISD {
public String[] strs;
public void func(String[] strs, boolean flag) { // line 5
if (flag) {
this.strs = strs; // line 7
}
}
}
]]></code>
</test-code>
</test-data>