diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/EnumSwitchStatementWhichMissesCasesInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/EnumSwitchStatementWhichMissesCasesInspection.java index f41c61ca240b..05cb5d61bc38 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/EnumSwitchStatementWhichMissesCasesInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/EnumSwitchStatementWhichMissesCasesInspection.java @@ -120,12 +120,17 @@ public class EnumSwitchStatementWhichMissesCasesInspection extends AbstractBaseJ if (constants.isEmpty()) return; CommonDataflow.DataflowResult dataflow = CommonDataflow.getDataflowResult(expression); if (dataflow != null) { - Set values = dataflow.getValuesNotEqualToExpression(expression); - for (Object value : values) { + Set notValues = dataflow.getValuesNotEqualToExpression(expression); + for (Object value : notValues) { if (value instanceof PsiEnumConstant) { constants.remove(((PsiEnumConstant)value).getName()); } } + Set values = StreamEx.of(dataflow.getExpressionValues(expression)).select(PsiEnumConstant.class) + .map(PsiEnumConstant::getName).toSet(); + if (!values.isEmpty()) { + constants.retainAll(values); + } } if (constants.isEmpty()) return; String message = buildErrorString(aClass.getQualifiedName(), constants); diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/EnumSwitchStatementWhichMissesCasesInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/EnumSwitchStatementWhichMissesCasesInspectionTest.java index c4d87530603e..d063e98cb458 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/EnumSwitchStatementWhichMissesCasesInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/EnumSwitchStatementWhichMissesCasesInspectionTest.java @@ -24,6 +24,7 @@ import org.jetbrains.annotations.Nullable; /** * @author Bas Leijdekkers */ +@SuppressWarnings("EnumSwitchStatementWhichMissesCases") public class EnumSwitchStatementWhichMissesCasesInspectionTest extends LightInspectionTestCase { public void testSimple() { @@ -120,7 +121,36 @@ public class EnumSwitchStatementWhichMissesCasesInspectionTest extends LightInsp " }\n" + "}"); } - + + public void testDfaPossibleValues() { + doTest("enum E {A, B, C}\n" + + "\n" + + "class X {\n" + + " void m(E e) {\n" + + " if(e == E.A || e == E.B) {\n" + + " switch (e) {\n" + + " case A:\n" + + " case B:\n" + + " }\n" + + " }\n" + + " }\n" + + "}"); + } + + public void testDfaPossibleValuesNotCovered() { + doTest("enum E {A, B, C}\n" + + "\n" + + "class X {\n" + + " void m(E e) {\n" + + " if(e == E.A || e == E.B) {\n" + + " /*'switch' statement on enum type 'E' misses case 'B'*/switch/**/ (e) {\n" + + " case A:\n" + + " }\n" + + " }\n" + + " }\n" + + "}"); + } + public void testJava12Preview() { doTest("enum E {A, B, C}\n" + "\n" +