GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] ExhaustiveSwitchHasDefault: False positive for non-exhaustive switch statements · Issue #5514 · pmd/pmd

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

New issue
Copy link
New issue
Copy link
Closed
#6822
Closed
[java] ExhaustiveSwitchHasDefault: False positive for non-exhaustive switch statements#5514
#6822
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematic
Milestone
7.27.0

Description

@jjlharrison
jjlharrison
opened on Feb 7, 2025
Issue body actions

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

Reacted by Björn Kautler

Activity

  1. jjlharrison
    added
    a:false-positivePMD flags a piece of code that is not problematic
    on Feb 7, 2025
  2. adangel
    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
    adangel
    on Feb 27, 2025
    Member
    More actions

    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. adangel
    marked [java] ExhaustiveSwitchHasDefault should not apply to classic switch statements #5540 as a duplicate of this issue on Feb 27, 2025
  5. jjlharrison commented on Mar 19, 2025

    @jjlharrison
    jjlharrison
    on Mar 19, 2025
    ContributorAuthor
    More actions

    Hi @adangel. Thanks, that does make sense.

  6. Vampire commented on Nov 26, 2025

    @Vampire
    Vampire
    on Nov 26, 2025
    Contributor
    More actions

    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.

    Reacted by David Cohen
  7. UncleOwen
    added 5 commits that reference this issue on Jun 27, 2026

    Add note about switch statement.

    59ca5f8

    Add note about switch statement.

    00e0047

    Add note about switch statement.

    39a1aa4

    Add note about switch statement.

    22eef61

    Add note about switch statement.

    0df9c94
  8. UncleOwen
    added a commit that references this issue on Jul 15, 2026

    [java] Fix #5670, #5514: ExhaustiveSwitchHasDefault when default is n…

    df58615
  9. UncleOwen
    closed this as completedin #6822on Jul 15, 2026
  10. UncleOwen
    added this to the 7.27.0 milestone on Jul 15, 2026
  11. adangel
    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-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

      Milestone

      • 7.27.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.