Skip to content

Commit

Permalink
SONARJAVA-3049 Compute type for switch-expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Godin authored and Wohops committed Jul 16, 2020
1 parent 213bb67 commit aada3ec
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1771,9 +1771,7 @@ private List<CaseGroupTreeImpl> convertSwitchStatements(List list) {

private ExpressionTree convertExpression(Expression node) {
ExpressionTree t = createExpression(node);
if (!t.is(Tree.Kind.SWITCH_EXPRESSION)) {
((AbstractTypedTree) t).typeBinding = node.resolveTypeBinding();
}
((AbstractTypedTree) t).typeBinding = node.resolveTypeBinding();
return t;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import java.util.Objects;
import org.sonar.java.model.InternalSyntaxToken;
import org.sonar.java.model.expression.AssessableExpressionTree;
import org.sonar.java.resolve.Symbols;
import org.sonar.plugins.java.api.semantic.Type;
import org.sonar.plugins.java.api.tree.CaseGroupTree;
import org.sonar.plugins.java.api.tree.ExpressionTree;
import org.sonar.plugins.java.api.tree.SwitchExpressionTree;
Expand Down Expand Up @@ -57,11 +55,6 @@ public SwitchExpressionTreeImpl(InternalSyntaxToken switchKeyword, InternalSynta
this.closeBraceToken = closeBraceToken;
}

@Override
public Type symbolType() {
return Symbols.unknownType;
}

@Override
public Kind kind() {
return Kind.SWITCH_EXPRESSION;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
import org.sonar.plugins.java.api.tree.MethodTree;
import org.sonar.plugins.java.api.tree.ParameterizedTypeTree;
import org.sonar.plugins.java.api.tree.ReturnStatementTree;
import org.sonar.plugins.java.api.tree.StatementTree;
import org.sonar.plugins.java.api.tree.SwitchStatementTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.TryStatementTree;
Expand Down Expand Up @@ -479,14 +478,16 @@ void expression_super_method_reference() {
void expression_switch() {
{
SwitchExpressionTreeImpl switchExpression = (SwitchExpressionTreeImpl) expression("switch (0) { default: yield 0; }");
assertThat(switchExpression.symbolType().isUnknown()).isFalse();

YieldStatementTreeImpl statement = (YieldStatementTreeImpl) switchExpression.cases().get(0).body().get(0);
assertThat(statement.yieldKeyword().text()).isEqualTo("yield");
assertThat(statement.expression()).isNotNull();
}
{
SwitchExpressionTreeImpl switchExpression = (SwitchExpressionTreeImpl) expression("switch (0) { default -> 0; case 0, 1 -> 0; }");
assertThat(switchExpression).isInstanceOf(AbstractTypedTree.class);
assertThat(switchExpression.symbolType().isUnknown()).isTrue();
assertThat(switchExpression.symbolType().isUnknown()).isFalse();

YieldStatementTreeImpl statement = (YieldStatementTreeImpl) switchExpression.cases().get(0).body().get(0);
assertThat(statement.yieldKeyword()).as("implicit yield-statement").isNull();
Expand Down

0 comments on commit aada3ec

Please sign in to comment.