Affects PMD Version: 6.26.0
Description:
When using local variable inference PMD can't determine that a switch variable is an enum and requires the default branch.
Code Sample demonstrating the issue:
enum Type {
CAT, DOG, COW
}
...
for (var animal : getAnimals()) {
switch (animal.getType()) { // false violation
case CAT:
// do something 1
break;
case DOG:
// do something 2
break;
case COW:
// do something 3
break;
}
}
...
public void setValue(List<Type> list) {
// it works with explicit types: list.forEach((Type eIn) -> {
list.forEach(eIn -> {
switch (eIn) {
case CAT:
// do something 1
break;
case DOG:
// do something 2
break;
case COW:
// do something 3
break;
});
}
Workaround:
Either explicit type must be specified in the for-loop or default branch must be added to the switch statement.
Affects PMD Version: 6.26.0
Description:
When using local variable inference PMD can't determine that a switch variable is an enum and requires the
defaultbranch.Code Sample demonstrating the issue:
Workaround:
Either explicit type must be specified in the for-loop or
defaultbranch must be added to theswitchstatement.