[java] ArrayIsStoredDirectly - report assignment rather than formal param

Fixes #3929
This commit is contained in:
Andreas Dangel
2022-04-21 15:15:11 +02:00
parent 0d8373d290
commit f27c6453ca
3 changed files with 14 additions and 7 deletions

View File

@ -26,6 +26,7 @@ This is a {{ site.pmd.release_type }} release.
* [#1474](https://github.com/pmd/pmd/issues/1474): \[java] ArrayIsStoredDirectly false positive with method call
* [#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
* [#3929](https://github.com/pmd/pmd/issues/3929): \[java] ArrayIsStoredDirectly should report the assignment rather than formal parameter
* plsql
* [#3706](https://github.com/pmd/pmd/issues/3706): \[plsql] Parsing exception CURSOR statement with parenthesis groupings

View File

@ -7,6 +7,7 @@ package net.sourceforge.pmd.lang.java.rule.bestpractices;
import java.util.ArrayList;
import java.util.List;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator;
import net.sourceforge.pmd.lang.java.ast.ASTBlockStatement;
@ -120,10 +121,10 @@ public class ArrayIsStoredDirectlyRule extends AbstractSunSecureRule {
* Checks if the variable designed in parameter is written to a field (not
* local variable) in the statements.
*/
private boolean checkForDirectAssignment(Object ctx, final ASTFormalParameter parameter,
private void checkForDirectAssignment(Object ctx, final ASTFormalParameter parameter,
final List<ASTBlockStatement> bs) {
final ASTVariableDeclaratorId vid = parameter.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
final String varName = vid.getImage();
final String varName = vid.getName();
for (ASTBlockStatement b : bs) {
if (b.getChild(0) instanceof ASTStatement && b.getChild(0).getChild(0) instanceof ASTStatementExpression) {
final ASTStatementExpression se = b.getFirstDescendantOfType(ASTStatementExpression.class);
@ -173,13 +174,13 @@ public class ArrayIsStoredDirectlyRule extends AbstractSunSecureRule {
md = pe.getFirstParentOfType(ASTConstructorDeclaration.class);
}
if (!isLocalVariable(varName, md)) {
addViolation(ctx, parameter, varName);
RuleContext ruleContext = (RuleContext) ctx;
ruleContext.addViolation(e, varName);
}
}
}
}
}
return false;
}
private ASTFormalParameter[] getArrays(ASTFormalParameters params) {
@ -187,7 +188,7 @@ public class ArrayIsStoredDirectlyRule extends AbstractSunSecureRule {
if (l != null && !l.isEmpty()) {
List<ASTFormalParameter> l2 = new ArrayList<>();
for (ASTFormalParameter fp : l) {
if (fp.isArray() || fp.isVarargs()) {
if (fp.getVariableDeclaratorId().hasArrayType() || fp.isVarargs()) {
l2.add(fp);
}
}

View File

@ -7,6 +7,7 @@
<test-code>
<description>Clear violation</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>3</expected-linenumbers>
<code><![CDATA[
public class Foo {
String [] arr;
@ -18,6 +19,7 @@ public class Foo {
<test-code>
<description>Clear violation with this.</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>3</expected-linenumbers>
<code><![CDATA[
public class Foo {
String [] arr;
@ -29,6 +31,7 @@ public class Foo {
<test-code>
<description>assignment to an internal array</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>3</expected-linenumbers>
<code><![CDATA[
public class Foo {
String [] arr;
@ -96,6 +99,7 @@ public class Foo {
<test-code>
<description>Constructor clear violation</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>3</expected-linenumbers>
<code><![CDATA[
public class Foo {
String [] arr;
@ -129,6 +133,7 @@ public class Foo {
<test-code>
<description>Trigger on Varargs</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>3</expected-linenumbers>
<code><![CDATA[
public class Foo {
String [] arr;
@ -222,7 +227,7 @@ public class AISD {
<description>do not allow private methods</description>
<rule-property name="allowPrivate">false</rule-property>
<expected-problems>2</expected-problems>
<expected-linenumbers>3,6</expected-linenumbers>
<expected-linenumbers>4,7</expected-linenumbers>
<code><![CDATA[
public class AISD {
private final byte[] buf;
@ -273,7 +278,7 @@ public class Foo {
<test-code>
<description>[java] ArrayIsStoredDirectly reports duplicated violation #3879</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>5</expected-linenumbers>
<expected-linenumbers>7</expected-linenumbers>
<code><![CDATA[
public class ExampleAISD {