|
88 | 88 | import org.sonar.plugins.java.api.tree.TypeCastTree; |
89 | 89 | import org.sonar.plugins.java.api.tree.TypeTree; |
90 | 90 | import org.sonar.plugins.java.api.tree.VariableTree; |
| 91 | +import org.sonar.plugins.java.api.tree.YieldStatementTree; |
91 | 92 |
|
92 | 93 | import static org.assertj.core.api.Assertions.assertThat; |
93 | 94 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
@@ -559,6 +560,36 @@ void expression_switch() { |
559 | 560 | } |
560 | 561 | } |
561 | 562 |
|
| 563 | + @Test |
| 564 | + void switch_expression_with_yield_of_unknown_identifier_without_default_clause() { |
| 565 | + SwitchExpressionTreeImpl switchExpression = (SwitchExpressionTreeImpl) expression("switch (unknownIdentifier) { case A -> 0; case B -> 1; }"); |
| 566 | + assertThat(switchExpression.expression().symbolType().isUnknown()).isTrue(); |
| 567 | + // the expression type of the full switch expression the should have been int or unknown |
| 568 | + // instead of java.lang.Object, it is probably a limitation in the JDT parser when it face a missing default clause error |
| 569 | + assertThat(switchExpression.symbolType().isUnknown()).isFalse(); |
| 570 | + assertThat(switchExpression.symbolType().fullyQualifiedName()).isEqualTo("java.lang.Object"); |
| 571 | + assertThat(switchExpression.cases().get(0).body().get(0)).isInstanceOf(YieldStatementTree.class); |
| 572 | + } |
| 573 | + |
| 574 | + @Test |
| 575 | + void switch_expression_of_unknown_identifier_without_default_clause() { |
| 576 | + SwitchExpressionTreeImpl switchExpression = (SwitchExpressionTreeImpl) expression("switch (unknownIdentifier) { case A: return 0; case B: return 1; }"); |
| 577 | + assertThat(switchExpression.expression().symbolType().isUnknown()).isTrue(); |
| 578 | + assertThat(switchExpression.symbolType().isUnknown()).isTrue(); |
| 579 | + assertThat(switchExpression.cases().get(0).body().get(0)).isInstanceOf(ReturnStatementTree.class); |
| 580 | + } |
| 581 | + |
| 582 | + @Test |
| 583 | + void switch_expression_of_enum_without_default_clause() { |
| 584 | + CompilationUnitTree cu = test("class C { Object m(java.time.DayOfWeek x) { return switch (x) { case MONDAY: return 0; case TUESDAY: return 1; } ; } }"); |
| 585 | + ClassTree c = (ClassTree) cu.types().get(0); |
| 586 | + MethodTree m = (MethodTree) c.members().get(0); |
| 587 | + ReturnStatementTree s = (ReturnStatementTree) Objects.requireNonNull(m.block()).body().get(0); |
| 588 | + SwitchExpressionTreeImpl switchExpression = (SwitchExpressionTreeImpl) s.expression(); |
| 589 | + assertThat(switchExpression.expression().symbolType().isUnknown()).isFalse(); |
| 590 | + assertThat(switchExpression.symbolType().isUnknown()).isTrue(); |
| 591 | + } |
| 592 | + |
562 | 593 | /** |
563 | 594 | * Pattern Matching for instanceof |
564 | 595 | * (Preview in Java 14) https://openjdk.java.net/jeps/305 |
|
0 commit comments