Skip to content

[java] ExhaustiveSwitchHasDefault: False positive for non-exhaustive switch statements #5514

Closed

Description

@jjlharrison

Affects PMD Version:
7.10.0

Rule:
ExhaustiveSwitchHasDefault

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) {
    case A -> logger.info("A");
    case B -> logger.info("B");
    default -> logger.info("default"); // PMD fails here with "The switch block is exhaustive even without the default case."
}

switch (e) {
    case A -> logger.info("A");
    // The compiler DOES NOT ERROR here, because this is not an exhaustive switch.
}

public enum Enum {
    A,
    B
}

Expected outcome:

PMD reports a violation at line 1, but that's wrong. That's a false positive.

Running PMD through:
Maven

Activity

  1. added
    a:false-positive
    on Feb 7, 2025
  2. changed the title [-][java] `ExhaustiveSwitchHasDefault` fails for non-exhaustive switch statements[/-] [+][java] ExhaustiveSwitchHasDefault fails for non-exhaustive switch statements[/+] on Feb 27, 2025
  3. adangel commented on Feb 27, 2025

    @adangel
    Member

    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

    Does the above make sense?

  4. jjlharrison commented on Mar 19, 2025

    @jjlharrison
    ContributorAuthor

    Hi @adangel. Thanks, that does make sense.

  5. Vampire commented on Nov 26, 2025

    @Vampire
    Contributor

    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 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.

  6. added a commit that references this issue on Jul 15, 2026
  7. added this to the 7.27.0 milestone on Jul 15, 2026
  8. changed the title [-][java] ExhaustiveSwitchHasDefault fails for non-exhaustive switch statements[/-] [+][java] ExhaustiveSwitchHasDefault: False positive for non-exhaustive switch statements[/+] on Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions