Affects PMD Version: 7.19.0
Rule: UnusedLocalVariable
Description:
using an instanceof with a pattern variable inside a for-each without brackets gives a false positive
Code Sample demonstrating the issue:
public void test() {
Object[] array = {1, "a"};
for(Object o : array)
if(o instanceof String s)
System.out.println(s);
return;
}
Expected outcome:
Variable s should not be reported as unused. If i use the brackets on the for-each, the warning disappears.
PMD reports a violation at line "instanceof", but that's wrong. That's a false positive.
Running PMD through: Maven
With this code the warning is not reported:
public void test() {
Object[] array = {1, "a"};
for(Object o : array) {
if(o instanceof String s)
System.out.println(s);
}
return;
}
Affects PMD Version: 7.19.0
Rule: UnusedLocalVariable
Description:
using an instanceof with a pattern variable inside a for-each without brackets gives a false positive
Code Sample demonstrating the issue:
Expected outcome:
Variable s should not be reported as unused. If i use the brackets on the for-each, the warning disappears.
PMD reports a violation at line "instanceof", but that's wrong. That's a false positive.
Running PMD through: Maven
With this code the warning is not reported: