Skip to content

[java] ExhaustiveSwitchHasDefault: Improve violation messages to list missing cases #6984

Closed

Description

@armandino

Affects PMD Version: 7.26.0 (also reproduced on 7.27.0-SNAPSHOT)

Rule: ExhaustiveSwitchHasDefault (https://docs.pmd-code.org/latest/pmd_rules_java_bestpractices.html#exhaustiveswitchhasdefault)

Description:

The switch below is not exhaustive. NEW is only handled by default. Yet PMD reports "exhaustive even without the default case". Removing default produces a compile error (the switch expression does not cover all possible input values).

Code Sample demonstrating the issue:

public final class Reproducer {
    enum State { NEW, ACTIVE, DONE, VOID }

    static String apply(State state) {
        return switch (state) {
            case ACTIVE -> "active";
            case DONE -> "done";
            case VOID -> "void";
            case null, default -> throw new IllegalStateException("unsupported: " + state);
        };
    }
}

Expected outcome: PMD should not report a violation.

Running PMD through: Maven (7.26.0), CLI (7.27.0-SNAPSHOT)

Related:

Activity

  1. added
    a:false-positive
    on Aug 18, 2026
  2. UncleOwen commented on Aug 18, 2026

    @UncleOwen
    Member

    No, this isn't a false positive. "Exhaustive" in this context doesn't mean "all possible cases are there" but "the compiler can check whether all possible cases are there". See the rule's description.

    But I agree that the violation message could be improved.

  3. removed
    a:false-positive
    on Aug 18, 2026
  4. armandino commented on Aug 19, 2026

    @armandino
    Author

    @UncleOwen thank you for clarifying. Couple of options for the error message if there's interest in updating it (happy to submit a PR):

    • A compiler-checkable type should omit the default case
    • Exhaustive enum/sealed switch loses its safety net once default case is added
  5. changed the title [-][java] ExhaustiveSwitchHasDefault false positive when enum switch has "case null"[/-] [+][java] ExhaustiveSwitchHasDefault improve violation message[/+] on Aug 19, 2026
  6. UncleOwen commented on Aug 19, 2026

    @UncleOwen
    Member

    The first one is very abstract. I like your second suggestion, especially that is mentions that this is about sealed types - but maybe try to make "safety net" more explicit?

    PRs are definitely welcome!

  7. zbynek commented on Aug 19, 2026

    @zbynek
    Contributor

    Maybe the rule could be rewritten in Java and the error message enhanced to list the enum constants / sealed interface implementations that are needed to replace default?

  8. armandino commented on Aug 20, 2026

    @armandino
    Author

    Other alternatives:

    • Exhaustive enum/sealed switch stops catching missing cases at compile time once default is added
    • Exhaustive enum/sealed switch statement stops catching missing cases at compile time once default case is added
  9. UncleOwen commented on Aug 20, 2026

    @UncleOwen
    Member

    I was thinking something like

    "A default case in a switch on sealed classes or enums prevents the compiler from checking that all cases are present."

    @zbynek It's already a java rule in current main :) Could you give an example of how this would look like? Just "(missing case: NEW)" added to whatever the new message is?

  10. zbynek commented on Aug 20, 2026

    @zbynek
    Contributor

    I was thinking something like

    "Replace default case with explicit cases (NEW) to make sure compiler keeps checking that all cases are present."

  11. UncleOwen commented on Aug 20, 2026

    @UncleOwen
    Member

    I like it!

  12. armandino commented on Aug 21, 2026

    @armandino
    Author

    Should it truncate the list if a failure has too many values to report?

    "Replace default case with explicit cases (FOO, BAR, BAZ, ...) to make sure compiler keeps checking that all cases are present."

  13. UncleOwen commented on Aug 21, 2026

    @UncleOwen
    Member

    Yes!

  14. AzazelSensei commented on Aug 25, 2026

    @AzazelSensei
    Contributor

    Opened #6996. The message now lists the missing cases and truncates after three.

  15. added a commit that references this issue on Aug 30, 2026
    8846018
  16. added
    an:enhancement
    in:documentation
    on Oct 18, 2026
  17. changed the title [-][java] ExhaustiveSwitchHasDefault improve violation message[/-] [+][java] ExhaustiveSwitchHasDefault: Improve violation message to list missing cases[/+] on Oct 18, 2026
  18. added this to the 7.29.0 milestone on Oct 18, 2026
  19. added a commit that references this issue on Oct 19, 2026
    128db42
  20. changed the title [-][java] ExhaustiveSwitchHasDefault: Improve violation message to list missing cases[/-] [+][java] ExhaustiveSwitchHasDefault: Improve violation messages to list missing cases[/+] on Oct 29, 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

    an:enhancementin:documentation

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions