Repository navigation
Description
Affects PMD Version: 7.10.0
Rule: ExhaustiveSwitchHasDefault
Description:
The new ExhaustiveSwitchHasDefault rule applies to both classic switch statements and the newer switch expressions. It states:
When switching over an enum or sealed class, the compiler will ensure that all possible cases are covered. If a case is missing, this will result in a compilation error.
But according to Oracle's documentation, this is not true:
Unlike switch statements, the cases of switch expressions must be exhaustive, which means that for all possible values, there must be a matching switch label. Thus, switch expressions normally require a default clause. However, for enum switch expressions that cover all known constants, the compiler inserts an implicit default clause.
Java will happily compile a classic switch statement over an enum even if it is non-exhaustive and has no default case. This means a switch statement that passes this new rule (i.e. is exhaustive and has no default case) can eventually become non-exhaustive if a new element is later added to the enum. That can be guarded against by having a default case that does something appropriate like throwing an exception, but that default case causes this new rule to fail. The NonExhaustiveSwitch rule can catch this added enum element making a switch no longer exhaustive, but the compiler will not. Also there are still times were the default case would be necessary in an exhaustive switch statement, like when setting a final variable from all branches.
Please compare with checkstyle's similar MissingSwitchDefault rule.
I think this new rule should only apply to switch expressions and the newer types of switch statements where the compiler actually does check exhaustiveness.
Code Sample demonstrating the issue:
enum Thing {
ONE,
TWO,
// Assume this additional element added later after below class Test was written.
THREE
}
class Test {
void test(Thing thing) {
switch (thing) {
ONE:
System.out.println("one");
break;
TWO:
System.out.println("two");
break;
// No case for THREE or default, but should still compile (although will violate the NonExhaustiveSwitch rule).
// A default case could have done something like throw an exception for unhandled Thing type.
}
}
}
class Test2 {
void test(Thing thing) {
final int num;
switch (thing) {
case ONE:
num = 1;
break;
case TWO:
num = 2;
break;
case THREE:
num = 3;
break;
// This violates ExhaustiveSwitchHasDefault, but removing would cause compile error - final num may not be initialized
default:
throw new IllegalStateException("unimplemented thing: " + thing);
}
// Pretend this is something more complex where we want/need num to be final.
System.out.println(num);
}
}Running PMD through: Maven
Java 17
Activity
adangel commentedon Feb 27, 2025
Thanks for reporting this issue.
That's true. The rule description is a bit sloppy about this.
I remember now, what I wanted to add (but forgot) - exactly this case, that the compiler doesn't enforce exhaustiveness for switch statements over enums. That's why I wanted to add the sentence, something like:
Note 2: The compiler doesn't create a compilation error for a switch statement over an enum that is non-exhaustive. When enabling this rule, you should consider also enabling the rule NonExhaustiveSwitch, so that you are still notified, if
someone adds a new enum constant and that new constant is not covered in an existing switch statement that previously was exhaustive but now is not anymore.
So in essence, the rule ExhaustiveSwitchHasDefault should always be used in tandem with NonExhaustiveSwitch.
Does the above make sense?
adangel commentedon Feb 27, 2025
I think, is is essentially a duplicate of #5514