From ddb0ecfceb7c4bdb3c07c8a23d4078877244b7a1 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 8 Apr 2022 15:30:25 +0200 Subject: [PATCH] [java] Fix UseArraysAsList with method calls - Fixes #3867 --- docs/pages/release_notes.md | 2 ++ .../resources/category/java/performance.xml | 12 ++++++---- .../rule/performance/xml/UseArraysAsList.xml | 24 +++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 262bf7df56..66deb1df5f 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -17,6 +17,8 @@ This is a {{ site.pmd.release_type }} release. ### Fixed Issues * core * [#3882](https://github.com/pmd/pmd/pull/3882): \[core] Fix AssertionError about exhaustive switch +* java-performance + * [#3867](https://github.com/pmd/pmd/issues/3867): \[java] UseArraysAsList with method call ### API Changes diff --git a/pmd-java/src/main/resources/category/java/performance.xml b/pmd-java/src/main/resources/category/java/performance.xml index 6f2f1f49d5..de0987520d 100644 --- a/pmd-java/src/main/resources/category/java/performance.xml +++ b/pmd-java/src/main/resources/category/java/performance.xml @@ -998,6 +998,8 @@ You must use `new ArrayList<>(Arrays.asList(...))` if that is inconvenient for y PrimaryPrefix/Name/@Image = ancestor::MethodDeclaration[1]//FormalParameter/VariableDeclaratorId[@ArrayType=true()]/@Name and PrimarySuffix/Expression/PrimaryExpression/PrimaryPrefix/Name ] + (: ignore method calls :) + [not(PrimarySuffix/Arguments)] ] ] ]]> @@ -1009,12 +1011,14 @@ You must use `new ArrayList<>(Arrays.asList(...))` if that is inconvenient for y public class Test { public void foo(Integer[] ints) { // could just use Arrays.asList(ints) - List l= new ArrayList<>(100); - for (int i=0; i< 100; i++) { + List l = new ArrayList<>(100); + 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 + + List anotherList = new ArrayList<>(); + for (int i = 0; i < 100; i++) { + anotherList.add(ints[i].toString()); // won't trigger the rule } } } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/UseArraysAsList.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/UseArraysAsList.xml index 467a414020..df5c9d6c0a 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/UseArraysAsList.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/UseArraysAsList.xml @@ -163,6 +163,30 @@ public class UseArraysAsListFN { } return result; } +} + ]]> + + + + [java] UseArraysAsList with method call #3867 + 1 + 9 + l = new ArrayList<>(100); + for (int i = 0; i < 100; i++) { + l.add(ints[i]); // line 9, here is the violation + } + List anotherList = new ArrayList<>(); + for (int i = 0; i < 100; i++) { + anotherList.add(ints[i].toString()); // line 13 - false positive + } + } } ]]>