Skip to content

[java] Fix #6984: ExhaustiveSwitchHasDefault: list missing cases in the violation - #6996

Merged
adangel merged 5 commits into
pmd:mainfrom
AzazelSensei:fix/exhaustive-switch-message-6984
Oct 18, 2026
Merged

adangel merged 5 commits into
pmd:mainfrom
AzazelSensei:fix/exhaustive-switch-message-6984

Conversation

@AzazelSensei

Copy link
Copy Markdown
Contributor

Describe the PR

ExhaustiveSwitchHasDefault used to say the switch was exhaustive even without default. That read like a false positive when a constant (NEW in the issue) only lived in default.

The message is now:

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

If nothing is missing, the parenthetical is dropped. More than three names get truncated with ....

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

@pmd-actions-helper

pmd-actions-helper Bot commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 91 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 16 errors and 9 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-10-18 15:30:01+00:00 for b787944)

@zbynek

zbynek commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

By checking the regressionreport it's obvious there are multiple cases that need different messages:

  • switch expression where some cases are covered by the default branch
    • here "Replace default case with explicit cases (...) to make sure compiler keeps checking that all cases are present." message makes sense
  • switch expression where the default branch covers 0 cases
    • if arrow branches are used, it would be better to say "Remove default case to make sure compiler keeps checking that all cases are present. "
    • if old-style branches (using colon) are used, the default branch cannot be removed -- should this special case be reported at all?
  • switch statement where some cases are covered by the default branch
    • here it's not true that compiler checks anything -- however it's still good to replace default with explicit values and use NonExhaustiveSwitch PMD rule to make sure the list stays exhaustive
  • switch statement where the default branch covers 0 cases
    • again, the compiler does not check anything, so removing the default branch is only safe if NonExhaustiveSwitch rule is active

The old message said the switch was already exhaustive. That hid the
constants still only handled by default. UncleOwen asked for the missing
names, truncated after three.

Fixes pmd#6984
@AzazelSensei
AzazelSensei force-pushed the fix/exhaustive-switch-message-6984 branch from 046992b to 8846018 Compare August 30, 2026 17:57
@AzazelSensei

Copy link
Copy Markdown
Contributor Author

Rebased onto main. Arrow switch expressions with no missing constants now say to remove the default. Colon-style branches and switch statements still use the original message — those look like a separate call on whether to report them.

The helper was package-private for tests; cover enum, sealed, fully
covered, and non-class selectors.
@UncleOwen

Copy link
Copy Markdown
Member

@zbynek What do you think about this one? I think the message for the other cases should be changed as well. Maybe "... so that NonExhaustiveSwitch can check them" for the cases where the compiler doesn't check them?

<expected-problems>1</expected-problems>
<expected-linenumbers>6</expected-linenumbers>
<expected-messages>
<message>Replace default case with explicit cases to make sure compiler keeps checking that all cases are present.</message>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this one the suggestion should be also "remove" rather than "replace" and it should not mention compiler checks.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll replace the message with "Remove default case and use the rule NonExhaustiveSwitch to make sure the switch stays exhaustive."

The rule NonExhaustiveSwitch should be used
to make sure the switch stays exhaustive.

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. I'll update the message (remove the compiler check hint for switch statements) and merge, if it looks ok.

<expected-problems>1</expected-problems>
<expected-linenumbers>6</expected-linenumbers>
<expected-messages>
<message>Replace default case with explicit cases to make sure compiler keeps checking that all cases are present.</message>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll replace the message with "Remove default case and use the rule NonExhaustiveSwitch to make sure the switch stays exhaustive."

@adangel adangel added this to the 7.29.0 milestone Oct 18, 2026
@adangel adangel changed the title [java] ExhaustiveSwitchHasDefault: list missing cases in the violation [java] Fix #6984: ExhaustiveSwitchHasDefault: list missing cases in the violation Oct 18, 2026
@adangel
adangel merged commit 128db42 into pmd:main Oct 18, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants