Affects PMD Version: 6.x
Rule: UnusedLocalVariable
Description:
If a variable is only used in a compound statement (e.g. +=), then this should not count as usage and a violation should be reported.
Note: This has already been fixed with PMD 7.0.0-rc1. (via #3113)
Found via #3123.
Code Sample demonstrating the issue:
public class UnusedCompoundAssignment {
void this_is_unused() {
int x = 0; // violation expected (line 3)
x += 2; // doesn't count as usage
}
void transitive_used_but_unused1() {
int a = 1; // _no_ violation expected, a is used
int b = a; // _no_ violation expected, b is used
int c = b; // violation expected (line 9)
}
void transitive_used_but_unused2() {
int a, b, c; // 1 violation expected, only c is unused (line 12)
a = 1;
b = a;
c = b;
}
void transitive_used() {
int a, b, c; // _no_ violation expected
a = 1;
b = a;
c = b;
System.out.println(c); // usage
}
void this_is_used() {
int y = 0; // _no_ violation expected
y += 2;
System.out.println(y); // usage
}
}
Expected outcome:
PMD should report a violation at line 3, but doesn't. This is a false-negative.
Affects PMD Version: 6.x
Rule: UnusedLocalVariable
Description:
If a variable is only used in a compound statement (e.g.
+=), then this should not count as usage and a violation should be reported.Note: This has already been fixed with PMD 7.0.0-rc1. (via #3113)
Found via #3123.
Code Sample demonstrating the issue:
Expected outcome:
PMD should report a violation at line 3, but doesn't. This is a false-negative.