EnumSwitchStatementWhichMissesCasesInspection: utilize possible values list

This commit is contained in:
Tagir Valeev
2019-01-23 15:32:36 +07:00
parent 809ab588e7
commit b78984a1e1
2 changed files with 38 additions and 3 deletions
@@ -120,12 +120,17 @@ public class EnumSwitchStatementWhichMissesCasesInspection extends AbstractBaseJ
if (constants.isEmpty()) return;
CommonDataflow.DataflowResult dataflow = CommonDataflow.getDataflowResult(expression);
if (dataflow != null) {
Set<Object> values = dataflow.getValuesNotEqualToExpression(expression);
for (Object value : values) {
Set<Object> notValues = dataflow.getValuesNotEqualToExpression(expression);
for (Object value : notValues) {
if (value instanceof PsiEnumConstant) {
constants.remove(((PsiEnumConstant)value).getName());
}
}
Set<String> 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);
@@ -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" +