Skip to content

SONARJAVA-5730 S1301 Should not raise on switch over small enums#5530

Merged
tomasz-tylenda-sonarsource merged 1 commit intomasterfrom
tt/SONARJAVA-5730_S1301
Mar 26, 2026
Merged

SONARJAVA-5730 S1301 Should not raise on switch over small enums#5530
tomasz-tylenda-sonarsource merged 1 commit intomasterfrom
tt/SONARJAVA-5730_S1301

Conversation

@tomasz-tylenda-sonarsource
Copy link
Contributor

No description provided.

@hashicorp-vault-sonar-prod
Copy link
Contributor

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

SONARJAVA-5730

@tomasz-tylenda-sonarsource tomasz-tylenda-sonarsource force-pushed the tt/SONARJAVA-5730_S1301 branch 2 times, most recently from 2bca513 to 54352d5 Compare March 13, 2026 15:52
@tomasz-tylenda-sonarsource tomasz-tylenda-sonarsource changed the title SONARJAVA-5730 S1301 Should allow switch over small enums SONARJAVA-5730 S1301 Should not raise on switch over small enums Mar 13, 2026
@sonarqube-next
Copy link

@github-actions
Copy link
Contributor

This PR is stale because it has been open 7 days with no activity. If there is no activity in the next 7 days it will be closed automatically

@github-actions github-actions bot added the stale label Mar 21, 2026
@tomasz-tylenda-sonarsource tomasz-tylenda-sonarsource force-pushed the tt/SONARJAVA-5730_S1301 branch 2 times, most recently from f74b8ea to 21e205a Compare March 26, 2026 13:49
@tomasz-tylenda-sonarsource tomasz-tylenda-sonarsource marked this pull request as ready for review March 26, 2026 13:54
@sonar-review-alpha
Copy link

sonar-review-alpha bot commented Mar 26, 2026

Summary

Fixes rule S1301 to not recommend converting switch statements to if statements when switching over enums without a default case. When an enum adds a new constant, a switch statement will fail to compile if the new case isn't handled, providing important safety feedback. Converting to if statements loses this safety net. The fix detects enum switches and skips the rule violation if no default case exists.

What reviewers should know

Start with SwitchAtLeastThreeCasesCheck.java to see the core logic: the check now exits early if it detects an enum switch without a default case. The key insight is that semantic analysis (symbolType().symbol().isEnum()) is used to identify enums—pay attention to how the new helper containsDefaultLabel() works and the comment explaining the rationale. The test file shows the expected behavior: small enum switches should not trigger issues (cases 1 & 2), but those with a default case should still be flagged (case 3). Note that the test suite includes a test_without_semantic() which verifies the check still works safely when semantic information is unavailable.


  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

@sonarqube-next
Copy link

Copy link

@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.

Conclusion: The rule logic is sound and the motivation is clear, but the without-semantic test is broken and will fail.

🗣️ Give feedback

@Test
void test_without_semantic() {
CheckVerifier.newVerifier()
.onFile(testCodeSourcesPath("checks/tests/SwitchAtLeastThreeCasesCheckSample.java"))

Choose a reason for hiding this comment

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

This test will fail. Without semantic, switchStatementTree.expression().symbolType().symbol().isEnum() returns false (unknown type resolves to a non-enum symbol), so the new enum guard never fires. The rule then reports issues on switchOverSmallEnum1 and switchOverSmallEnum2 — both have count < 3, no default, and no // Noncompliant annotation — which causes verifyIssues() to fail with unexpected issues.

This test needs its own sample file (or inline source) that reflects the actual without-semantic behavior: either mark those two methods // Noncompliant in a copy of the fixture, or restrict the without-semantic file to the non-enum cases from the original fixture.

  • Mark as noise

@tomasz-tylenda-sonarsource tomasz-tylenda-sonarsource merged commit 7a9542a into master Mar 26, 2026
30 of 31 checks passed
@tomasz-tylenda-sonarsource tomasz-tylenda-sonarsource deleted the tt/SONARJAVA-5730_S1301 branch March 26, 2026 15:53
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