SONARJAVA-5730 S1301 Should not raise on switch over small enums#5530
SONARJAVA-5730 S1301 Should not raise on switch over small enums#5530tomasz-tylenda-sonarsource merged 1 commit intomasterfrom
switch over small enums#5530Conversation
2bca513 to
54352d5
Compare
switch over small enums
|
|
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 |
f74b8ea to
21e205a
Compare
21e205a to
6e59395
Compare
SummaryFixes 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 knowStart with
|
|
| @Test | ||
| void test_without_semantic() { | ||
| CheckVerifier.newVerifier() | ||
| .onFile(testCodeSourcesPath("checks/tests/SwitchAtLeastThreeCasesCheckSample.java")) |
There was a problem hiding this comment.
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




No description provided.