SONARJAVA-5730 S1301 should report on small but non-exhaustive switch over enum#5538
SONARJAVA-5730 S1301 should report on small but non-exhaustive switch over enum#5538tomasz-tylenda-sonarsource merged 1 commit intomasterfrom
Conversation
SummaryImprove 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 knowThe key change is in SwitchAtLeastThreeCasesCheck.java lines 56-63: the condition now checks
|
|
There was a problem hiding this comment.
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.




No description provided.