Skip to content

SONARJAVA-5730 S1301 should report on small but non-exhaustive switch over enum#5538

Merged
tomasz-tylenda-sonarsource merged 1 commit intomasterfrom
tt/fix-S1301
Mar 27, 2026
Merged

SONARJAVA-5730 S1301 should report on small but non-exhaustive switch over enum#5538
tomasz-tylenda-sonarsource merged 1 commit intomasterfrom
tt/fix-S1301

Conversation

@tomasz-tylenda-sonarsource
Copy link
Copy Markdown
Contributor

No description provided.

@hashicorp-vault-sonar-prod
Copy link
Copy Markdown
Contributor

hashicorp-vault-sonar-prod bot commented Mar 27, 2026

SONARJAVA-5730

@sonar-review-alpha
Copy link
Copy Markdown

sonar-review-alpha bot commented Mar 27, 2026

Summary

Improve S1301 rule to detect non-exhaustive switches over enums. Previously, the rule skipped reporting violations on all enum switches without a default case, assuming compiler exhaustiveness would catch missing cases. This fix refines the logic to only skip reporting when the switch actually covers all enum constants. Switches over enums with many constants but only a few cases are now properly flagged, improving code quality detection for partial enum handling that shouldn't rely on compiler checks.

What reviewers should know

The key change is in SwitchAtLeastThreeCasesCheck.java lines 56-63: the condition now checks enumConstantCount(typeSymbol) == count to verify the switch is truly exhaustive. The new enumConstantCount() method filters for actual enum constants while excluding fields/methods defined in the enum body. New test cases demonstrate the fix: a 7-constant enum with 1 case is now Noncompliant, while a 2-constant enum with 2 cases remains Compliant (exhaustive). Focus review on whether the enum constant counting logic correctly identifies only the enum constants and not other enum members.


  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

@sonarqube-next
Copy link
Copy Markdown

sonar-review-alpha[bot]

This comment was marked as resolved.

Copy link
Copy Markdown

@sonar-review-alpha sonar-review-alpha bot left a comment

Choose a reason for hiding this comment

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

LGTM! ✅

Correct and clean fix. The exhaustion check is only reachable when count < 3 (due to the early return at line 55), so comparing enumConstantCount against count is naturally scoped to small enums. Multi-label cases (case PRIVATE, PUBLIC:) are handled correctly because totalLabelCount already counts individual label expressions rather than case groups, so the comparison against enumConstantCount works for all syntactic forms.

🗣️ Give feedback

@tomasz-tylenda-sonarsource tomasz-tylenda-sonarsource merged commit 1f730dd into master Mar 27, 2026
20 checks passed
@tomasz-tylenda-sonarsource tomasz-tylenda-sonarsource deleted the tt/fix-S1301 branch March 27, 2026 14:57
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.

2 participants