ControlFlowAnalyzer: do not produce case-tests for invalid switch labels

GitOrigin-RevId: 8bc49191864fea56a3925f113892323f1cc7c58a
This commit is contained in:
Tagir Valeev
2020-02-19 05:11:11 +00:00
committed by intellij-monorepo-bot
parent 4c20010c0e
commit 52ee08cd92
3 changed files with 18 additions and 3 deletions
@@ -943,12 +943,16 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
if (values != null) {
for (PsiExpression caseValue : values.getExpressions()) {
boolean enumConstant = false;
if (enumValues != null && caseValue instanceof PsiReferenceExpression) {
//noinspection SuspiciousMethodCalls
enumValues.remove(((PsiReferenceExpression)caseValue).resolve());
PsiEnumConstant target = ObjectUtils.tryCast(((PsiReferenceExpression)caseValue).resolve(), PsiEnumConstant.class);
if (target != null) {
enumValues.remove(target);
enumConstant = true;
}
}
if (caseValue != null && expressionValue != null) {
if (caseValue != null && expressionValue != null && (enumConstant || PsiUtil.isConstantExpression(caseValue))) {
addInstruction(new PushInstruction(expressionValue, null));
caseValue.accept(this);
addInstruction(new BinopInstruction(
@@ -0,0 +1,10 @@
import org.jetbrains.annotations.*;
class Test {
void test(int x) {
Integer y = null;
switch (x) {
case <error descr="Constant expression required">y</error>:
}
}
}
@@ -668,4 +668,5 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase {
public void testNullabilityAfterCastAndInstanceOf() { doTest(); }
public void testInstanceOfTernary() { doTest(); }
public void testStringContains() { doTest(); }
public void testSwitchLabelNull() { doTest(); }
}