[doc] UnusedLocalVariable with multiple for loop indices

Closes #4518
This commit is contained in:
Andreas Dangel
2023-04-28 09:29:12 +02:00
parent 429f0f076a
commit 9ecc1a84ed
2 changed files with 32 additions and 0 deletions

View File

@ -451,6 +451,7 @@ Language specific fixes:
* [#3675](https://github.com/pmd/pmd/pull/3675): \[java] MissingOverride - fix false positive with mixing type vars
* [#4516](https://github.com/pmd/pmd/issues/4516): \[java] UnusedLocalVariable: false-negative with try-with-resources
* [#4517](https://github.com/pmd/pmd/issues/4517): \[java] UnusedLocalVariable: false-negative with compound assignments
* [#4518](https://github.com/pmd/pmd/issues/4518): \[java] UnusedLocalVariable: false-positive with multiple for-loop indices
* java-codestyle
* [#1208](https://github.com/pmd/pmd/issues/1208): \[java] PrematureDeclaration rule false-positive on variable declared to measure time
* [#1429](https://github.com/pmd/pmd/issues/1429): \[java] PrematureDeclaration as result of method call (false positive)

View File

@ -506,6 +506,37 @@ public class Foo {
System.out.println(id);
}
}
}
]]></code>
</test-code>
<test-code>
<description>[java] UnusedLocalVariable: false-positive with multiple for-loop indices #4518</description>
<expected-problems>4</expected-problems>
<expected-linenumbers>3,3,6,15</expected-linenumbers>
<expected-messages>
<message>Avoid unused local variables such as 'a'.</message>
<message>Avoid unused local variables such as 'b'.</message>
<message>Avoid unused local variables such as 'b'.</message>
<message>Avoid unused local variables such as 'j'.</message>
</expected-messages>
<code><![CDATA[
public class UnusedLocalVarsMultiple {
public void sample1() {
int a = 0, b = 1; // a and b unused (line 3)
}
public void sample2() {
int a = 0, b = a; // only b unused (line 6)
}
public void sample3() {
for (int i = 0; i < 10; i++); // i is loop index, is used and should be ignored
}
public void sample4() {
for (int i = 0, j = 0; i < 10; i++, j++); // i and j are loop indices, both are used (line 12)
}
public void sample5() {
for (int i = 0, j = 0; i < 10; i++); // i and j are loop indices, but j is not used (line 15)
}
}
]]></code>
</test-code>