[java] ArrayIsStoredDirectly - report assignment rather than formal param
Fixes #3929
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
||||
|
Reference in New Issue
Block a user