Skip to content

Commit

Permalink
SONARJAVA-4908 Fix FP on S131 with type patterns in switch case claus…
Browse files Browse the repository at this point in the history
…es (#4714)
  • Loading branch information
Wohops authored Mar 15, 2024
1 parent 23337de commit 30f96fa
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,58 @@ void foo(MyEnum myEnum, DayOfWeek dow) {
break;
}
}

sealed interface Animal permits Cat, Dog, Phoenix {}
record Cat() implements Animal { }
record Dog() implements Animal { }
record Phoenix() implements Animal { }

static void sealedClass(Animal animal) {
switch (animal) { // Compliant
case Cat ignored -> { }
case Dog ignored -> { }
case Phoenix ignored -> { }
}
}

static void incompleteSealedClass(Animal animal) {
switch (animal) { // Compliant
case Cat ignored -> { }
default -> { }
}
}

static void typePattern(Object object) {
switch (object) { // Compliant
case String s -> { }
case Number n -> { }
case Object o -> { } // alternative to default
}
}

static void typePatternWithDefault(CharSequence cs) {
switch (cs) { // Compliant
case String s -> { }
case CharSequence c -> { } // type of the expression
}
}

static void typePatternWithDefault(Object object) {
switch (object) { // Compliant
case String s -> { }
case Number n -> { }
default -> { }
}
}

record MyRecord(int x, int y) { }

static void recordSwitch1(MyRecord object) {
switch (object) { // Compliant
case MyRecord(int x, int y) when x > 42 -> { }
case MyRecord(int x, int y) -> { }
}
}
}

enum MyEnum {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public List<Tree.Kind> nodesToVisit() {
@Override
public void visitNode(Tree tree) {
SwitchStatementTree switchStatementTree = (SwitchStatementTree) tree;
if (getDefaultLabel(switchStatementTree)) {
if (missesDefaultLabel(switchStatementTree) && !isSwitchOnTypePattern(switchStatementTree)) {
if (!isSwitchOnEnum(switchStatementTree)) {
reportIssue(switchStatementTree.switchKeyword(), "Add a default case to this switch.");
} else if (missingCasesOfEnum(switchStatementTree)) {
Expand All @@ -55,7 +55,7 @@ public void visitNode(Tree tree) {
}
}

private static boolean getDefaultLabel(SwitchStatementTree switchStatementTree) {
private static boolean missesDefaultLabel(SwitchStatementTree switchStatementTree) {
return allLabels(switchStatementTree).noneMatch(SwitchLastCaseIsDefaultCheck::isDefault);
}

Expand All @@ -66,6 +66,11 @@ private static boolean isDefault(CaseLabelTree caseLabelTree) {
return caseLabelTree.expressions().stream().anyMatch(expr -> expr.is(Tree.Kind.DEFAULT_PATTERN));
}

private static boolean isSwitchOnTypePattern(SwitchStatementTree switchStatementTree) {
return allExpressions(switchStatementTree)
.anyMatch(expression -> expression.is(Tree.Kind.TYPE_PATTERN, Tree.Kind.RECORD_PATTERN, Tree.Kind.GUARDED_PATTERN));
}

private static boolean equalsDefaultKeyword(String text) {
return DEFAULT_LABEL_STRING.equals(text);
}
Expand Down

0 comments on commit 30f96fa

Please sign in to comment.