You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[java] ExhaustiveSwitchHasDefault: False positive for non-exhaustive switch statements #5514
Description:
The new ExhaustiveSwitchHasDefault rule introduced in 7.10.0 is failing for non-exhaustive switches.
The rule description says:
When switching over an enum or sealed class, the compiler will ensure that all possible cases are covered.
However, in the example below, the switch statement is not exhaustive, so the compiler will not error when not all cases are covered.
Code Sample demonstrating the issue:
switch (e) {
caseA -> logger.info("A");
caseB -> logger.info("B");
default -> logger.info("default"); // PMD fails here with "The switch block is exhaustive even without the default case."
}
switch (e) {
caseA -> logger.info("A");
// The compiler DOES NOT ERROR here, because this is not an exhaustive switch.
}
publicenumEnum {
A,
B
}
Expected outcome:
PMD reports a violation at line 1, but that's wrong. That's a false positive.
changed the title [-][java] `ExhaustiveSwitchHasDefault` fails for non-exhaustive switch statements[/-][+][java] ExhaustiveSwitchHasDefault fails for non-exhaustive switch statements[/+]on Feb 27, 2025
Thanks for reporting this issue. Repeating here now, what I posted over there: #5540 (comment)
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.
I see the following open tasks:
Update rule description to emphasize the cases, when the java compiler won't enforce exhaustiveness
Maybe only apply this rule in such cases via opt-in property and describe that if enabling this, one should also use NonEhaustiveSwitch
Even if you have an a classical switch statement with all cases for an enum plus a default case, the rule mentioned here should not throw if the default case is just throwing an exception.
Maybe even the NonEhaustiveSwitch rule should be enhanced to have a finding if you have an exhaustive switch statement without throwing default case.
If you have a switch statement over an enum that you compile along with the switch statement, you can ensure the branches at compile time.
But if you for example switch over an enum from a different artifact, then at runtime that other artifact could be of a different version and thus there can be more enum constants available, so a default case that throws makes sense to detect this.
If this is added, it would also be questionable whether the NonEhaustiveSwitch should consider a switch statement with not all constants but a throwing default as excaustive already or not.
Maybe it should not be "default case that throws" but "default case that throws AssertionError", so that you can identify the runtime-exhaustiveness-protection default branch from a default branch that intentionally throws an excepetion for other enum-cases.
changed the title [-][java] ExhaustiveSwitchHasDefault fails for non-exhaustive switch statements[/-][+][java] ExhaustiveSwitchHasDefault: False positive for non-exhaustive switch statements[/+]on Aug 20, 2026
Activity
[-][java] `ExhaustiveSwitchHasDefault` fails for non-exhaustive switch statements[/-][+][java] ExhaustiveSwitchHasDefault fails for non-exhaustive switch statements[/+]adangel commentedon Feb 27, 2025
Thanks for reporting this issue. Repeating here now, what I posted over there: #5540 (comment)
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.
I see the following open tasks:
Does the above make sense?
jjlharrison commentedon Mar 19, 2025
Hi @adangel. Thanks, that does make sense.
Vampire commentedon Nov 26, 2025
Maybe another detail to consider.
Even if you have an a classical switch statement with all cases for an enum plus a default case, the rule mentioned here should not throw if the default case is just throwing an exception.
Maybe even the
NonEhaustiveSwitchrule should be enhanced to have a finding if you have an exhaustive switch statement without throwing default case.If you have a switch statement over an enum that you compile along with the switch statement, you can ensure the branches at compile time.
But if you for example switch over an enum from a different artifact, then at runtime that other artifact could be of a different version and thus there can be more enum constants available, so a default case that throws makes sense to detect this.
If this is added, it would also be questionable whether the
NonEhaustiveSwitchshould consider a switch statement with not all constants but a throwing default as excaustive already or not.Maybe it should not be "default case that throws" but "default case that throws AssertionError", so that you can identify the runtime-exhaustiveness-protection default branch from a default branch that intentionally throws an excepetion for other enum-cases.
Add note about switch statement.
Add note about switch statement.
Add note about switch statement.
Add note about switch statement.
Add note about switch statement.
[java] Fix #5670, #5514: ExhaustiveSwitchHasDefault when default is n…
[-][java] ExhaustiveSwitchHasDefault fails for non-exhaustive switch statements[/-][+][java] ExhaustiveSwitchHasDefault: False positive for non-exhaustive switch statements[/+]