Applied patch 2822173: Fix false negative for UseArraysAsList when the array was passed as method parameter. Thanks to Andy Throgmorton.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/branches/pmd/4.3.x@7400 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Romain Pelisse
2011-10-13 15:54:39 +00:00
parent b60d442777
commit 14cc641316
4 changed files with 52 additions and 30 deletions

View File

@ -7,6 +7,7 @@ Add C# support for CPD - thanks to Florian Bauer
Fix small bug in Rule Designer UI Fix small bug in Rule Designer UI
Improve TooManyMethods rule - thanks to a patch from Riku Nykanen Improve TooManyMethods rule - thanks to a patch from Riku Nykanen
Improve DoNotCallSystemExit - thanks to a patch from Steven Christou Improve DoNotCallSystemExit - thanks to a patch from Steven Christou
Fix false negative for UseArraysAsList when the array was passed as method parameter - thanks to Andy Throgmorton
New Rule: New Rule:
Basic ruleset: DontCallThreadRun - thanks to Andy Throgmorton Basic ruleset: DontCallThreadRun - thanks to Andy Throgmorton

View File

@ -8,11 +8,11 @@ failure case
<code><![CDATA[ <code><![CDATA[
public class Bar { public class Bar {
void foo() { void foo() {
Integer[] ints = new Integer(10); Integer[] ints = new Integer(10);
List l= new ArrayList(10); List l= new ArrayList(10);
for (int i=0; i< 100; i++) { for (int i=0; i< 100; i++) {
l.add(ints[i]); l.add(ints[i]);
} }
} }
} }
]]></code> ]]></code>
@ -25,11 +25,11 @@ adding first element repeatedly
<code><![CDATA[ <code><![CDATA[
public class Bar { public class Bar {
void foo() { void foo() {
Integer[] ints = new Integer(10); Integer[] ints = new Integer(10);
List l= new ArrayList(10); List l= new ArrayList(10);
for (int i=0; i< 100; i++) { for (int i=0; i< 100; i++) {
l.add(ints[1]); l.add(ints[1]);
} }
} }
} }
]]></code> ]]></code>
@ -42,11 +42,11 @@ inside conditional
<code><![CDATA[ <code><![CDATA[
public class Bar { public class Bar {
void foo() { void foo() {
Integer[] ints = new Integer(10); Integer[] ints = new Integer(10);
List l= new ArrayList(10); List l= new ArrayList(10);
for (int i=0; i< 100; i++) { for (int i=0; i< 100; i++) {
if (y > 10) { l.add(ints[1]);} if (y > 10) { l.add(ints[1]);}
} }
} }
} }
]]></code> ]]></code>
@ -59,11 +59,11 @@ adding new object
<code><![CDATA[ <code><![CDATA[
public class Bar { public class Bar {
void foo() { void foo() {
Integer[] ints = new Integer(10); Integer[] ints = new Integer(10);
List l= new ArrayList(10); List l= new ArrayList(10);
for (int i=0; i< 100; i++) { for (int i=0; i< 100; i++) {
l.add(new Integer(i+1)); l.add(new Integer(i+1));
} }
} }
} }
]]></code> ]]></code>
@ -76,13 +76,31 @@ calling method
<code><![CDATA[ <code><![CDATA[
public class Bar { public class Bar {
void foo() { void foo() {
Integer[] ints = new Integer(10); Integer[] ints = new Integer(10);
List l= new ArrayList(10); List l= new ArrayList(10);
for (int i=0; i< 100; i++) { for (int i=0; i< 100; i++) {
l.add(String.valueOf(i)); l.add(String.valueOf(i));
} }
} }
} }
]]></code> ]]></code>
</test-code> </test-code>
</test-data> <test-code>
<description>Integer array passed as argument</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Test {
public void foo(Integer[] ints) {
// could just use Arrays.asList(ints)
List l = new ArrayList(10);
for (int i=0; i< 100; i++) {
l.add(ints[i]);
}
for (int i=0; i< 100; i++) {
l.add(a[i].toString()); // won't trigger the rule
}
}
}
]]></code>
</test-code>
</test-data>

View File

@ -187,7 +187,7 @@ public class Foo {
] ]
//StatementExpression[ //StatementExpression[
PrimaryExpression/PrimaryPrefix/Name[ PrimaryExpression/PrimaryPrefix/Name[
substring-before(@Image,'.add') = ancestor::MethodDeclaration//LocalVariableDeclaration[ substring-before(@Image,'.add') = ancestor::MethodDeclaration//LocalVariableDeclaration[
./Type//ClassOrInterfaceType[ ./Type//ClassOrInterfaceType[
@Image = 'Collection' or @Image = 'Collection' or
@Image = 'List' or @Image='ArrayList' @Image = 'List' or @Image='ArrayList'
@ -202,8 +202,11 @@ public class Foo {
] ]
and and
PrimaryExpression/PrimarySuffix/Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix/Name PrimaryExpression/PrimarySuffix/Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix/Name
[@Image = ancestor::MethodDeclaration//LocalVariableDeclaration [
[@Array="true"]/VariableDeclarator/VariableDeclaratorId/@Image] @Image = ancestor::MethodDeclaration//LocalVariableDeclaration[@Array="true"]/VariableDeclarator/VariableDeclaratorId/@Image
or
@Image = ancestor::MethodDeclaration//FormalParameter/VariableDeclaratorId/@Image
]
/../..[count(.//PrimarySuffix) /../..[count(.//PrimarySuffix)
=1]/PrimarySuffix/Expression/PrimaryExpression/PrimaryPrefix =1]/PrimarySuffix/Expression/PrimaryExpression/PrimaryPrefix
/Name /Name

View File

@ -57,7 +57,7 @@
</subsection> </subsection>
<subsection name="Contributors"> <subsection name="Contributors">
<ul> <ul>
<li>Andy Throgmorton - New XPath getCommentOn function, new rule DontCallThreadRun</li> <li>Andy Throgmorton - New XPath getCommentOn function, new rule DontCallThreadRun, fix for rule UseArraysAsList</li>
<li>Nicolas Dordet - Fixed an issue on CloseResource</li> <li>Nicolas Dordet - Fixed an issue on CloseResource</li>
<li>Juan Jesús García de Soria - Rework CPD algorithm</li> <li>Juan Jesús García de Soria - Rework CPD algorithm</li>
<li>Sergey Pariev - Fixed an ugly ArrayIndexOutOfBoundsException in CPD for Ruby</li> <li>Sergey Pariev - Fixed an ugly ArrayIndexOutOfBoundsException in CPD for Ruby</li>