From 14cc641316bc52ab4cae9806fd7f0906c8ea9944 Mon Sep 17 00:00:00 2001 From: Romain Pelisse Date: Thu, 13 Oct 2011 15:54:39 +0000 Subject: [PATCH] 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 --- pmd/etc/changelog.txt | 1 + .../optimizations/xml/UseArraysAsList.xml | 70 ++++++++++++------- pmd/rulesets/optimizations.xml | 9 ++- pmd/xdocs/credits.xml | 2 +- 4 files changed, 52 insertions(+), 30 deletions(-) diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 04f9db7a2f..45b2938aae 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -7,6 +7,7 @@ Add C# support for CPD - thanks to Florian Bauer Fix small bug in Rule Designer UI Improve TooManyMethods rule - thanks to a patch from Riku Nykanen 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: Basic ruleset: DontCallThreadRun - thanks to Andy Throgmorton diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/optimizations/xml/UseArraysAsList.xml b/pmd/regress/test/net/sourceforge/pmd/rules/optimizations/xml/UseArraysAsList.xml index 0a2fa264ad..f088aa9274 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/optimizations/xml/UseArraysAsList.xml +++ b/pmd/regress/test/net/sourceforge/pmd/rules/optimizations/xml/UseArraysAsList.xml @@ -8,11 +8,11 @@ failure case @@ -25,11 +25,11 @@ adding first element repeatedly @@ -42,11 +42,11 @@ inside conditional 10) { l.add(ints[1]);} - } + Integer[] ints = new Integer(10); + List l= new ArrayList(10); + for (int i=0; i< 100; i++) { + if (y > 10) { l.add(ints[1]);} + } } } ]]> @@ -59,11 +59,11 @@ adding new object @@ -76,13 +76,31 @@ calling method - \ No newline at end of file + + Integer array passed as argument + 1 + + + diff --git a/pmd/rulesets/optimizations.xml b/pmd/rulesets/optimizations.xml index f64d29caec..dad9cc5486 100644 --- a/pmd/rulesets/optimizations.xml +++ b/pmd/rulesets/optimizations.xml @@ -187,7 +187,7 @@ public class Foo { ] //StatementExpression[ PrimaryExpression/PrimaryPrefix/Name[ - substring-before(@Image,'.add') = ancestor::MethodDeclaration//LocalVariableDeclaration[ + substring-before(@Image,'.add') = ancestor::MethodDeclaration//LocalVariableDeclaration[ ./Type//ClassOrInterfaceType[ @Image = 'Collection' or @Image = 'List' or @Image='ArrayList' @@ -202,8 +202,11 @@ public class Foo { ] and 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) =1]/PrimarySuffix/Expression/PrimaryExpression/PrimaryPrefix /Name diff --git a/pmd/xdocs/credits.xml b/pmd/xdocs/credits.xml index 9c40ce1117..b7fcfed358 100644 --- a/pmd/xdocs/credits.xml +++ b/pmd/xdocs/credits.xml @@ -57,7 +57,7 @@