Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,33 @@ public void f(int variable) {
doSomethingElse();
}
}

public enum SmallEnum {
ONE,
TWO
}

public void switchOverSmallEnum1(SmallEnum smallEnum) {
switch (smallEnum) {
case ONE -> {
System.out.println("1");
}
case TWO -> {
System.out.println("2");
}
};
}

public int switchOverSmallEnum2(SmallEnum smallEnum) {
int ret = -1;
switch (smallEnum) {
case ONE:
ret = 1;
break;
case TWO:
ret = 2;
break;
};
return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.tree.CaseGroupTree;
import org.sonar.plugins.java.api.tree.CaseLabelTree;
import org.sonar.plugins.java.api.tree.SwitchStatementTree;
Expand Down Expand Up @@ -46,7 +47,10 @@ public void visitNode(Tree tree) {
count += totalLabelCount(caseGroup);
}
if (count < 3) {
reportIssue(switchStatementTree.switchKeyword(), "Replace this \"switch\" statement by \"if\" statements to increase readability.");
Symbol.TypeSymbol typeSymbol = switchStatementTree.expression().symbolType().symbol();
if (!typeSymbol.isUnknown() && !typeSymbol.isEnum()) {
reportIssue(switchStatementTree.switchKeyword(), "Replace this \"switch\" statement by \"if\" statements to increase readability.");
}
}
}

Expand Down
Loading