[java] Add test for #2207: AvoidInstantiatingObjectsInLoops false positives

This commit is contained in:
Andreas Dangel committed 2020-07-09 18:54:53 +02:00
1 parent 8766d54a07
commit e48489450f
1 file changed
+42 -3
@@ -89,8 +89,7 @@ public class Foo {
]]></code>
</test-code>
<!-- FIXME -->
<!--test-code>
<test-code>
<description>#278 Semi-false positive for instantiating new object in loop</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
@@ -106,7 +105,7 @@ public class Foo {
}
}
]]></code>
</test-code-->
</test-code>
<test-code>
<description>#1215 AvoidInstantiatingObjectsInLoops matches the right side of a list iteration loop</description>
@@ -124,6 +123,46 @@ public class TestInstantiationInLoop {
System.out.println(filename);
}
}
}
]]></code>
</test-code>
<test-code>
<description>[java] False positive: AvoidInstantiatingObjectsInLoops should not flag objects with different parameters or objects assigned or passed as parameters #2207</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.awt.Dimension;
public class PMDDemo {
public static void main(final String[] args) {
final Dimension[] arr = new Dimension[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = new Dimension(i, i); // rule violation here
}
}
}
]]></code>
</test-code>
<test-code>
<description>False positive when assigning to a list/array (see #2207 and #1043)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.util.*;
public class PMDDemo {
public void checkArray() {
Car[] cars = new Car[3];
for(int i = 0; i < cars.length; ++i) {
cars[i] = new Car();
}
}
public void checkCollection() {
Collection<Car> cars = new ArrayList<>();
for(int i = 0; i < 3; ++i) {
cars.add(new Car());
}
}
}
]]></code>
</test-code>