IDEA-134677 ConstantCondition inspection could be smarter for enums

This commit is contained in:
peter
2015-01-02 17:19:53 +01:00
parent 4a0b740e80
commit 0a175d30bc
3 changed files with 41 additions and 10 deletions
@@ -647,11 +647,19 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
try {
ControlFlow.ControlFlowOffset offset = getStartOffset(statement);
PsiExpression caseValue = psiLabelStatement.getCaseValue();
if (caseValue != null &&
if (enumValues != null && caseValue instanceof PsiReferenceExpression) {
//noinspection SuspiciousMethodCalls
enumValues.remove(((PsiReferenceExpression)caseValue).resolve());
}
boolean alwaysTrue = enumValues != null && enumValues.isEmpty();
if (alwaysTrue) {
addInstruction(new PushInstruction(myFactory.getConstFactory().getTrue(), null));
}
else if (caseValue != null &&
caseExpression instanceof PsiReferenceExpression &&
((PsiReferenceExpression)caseExpression).getQualifierExpression() == null &&
JavaPsiFacade.getInstance(body.getProject()).getConstantEvaluationHelper().computeConstantExpression(caseValue) != null) {
((PsiReferenceExpression)caseExpression).getQualifierExpression() == null) {
addInstruction(new PushInstruction(myFactory.createValue(caseExpression), caseExpression));
caseValue.accept(this);
@@ -663,12 +671,6 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
addInstruction(new ConditionalGotoInstruction(offset, false, statement));
if (enumValues != null) {
if (caseValue instanceof PsiReferenceExpression) {
//noinspection SuspiciousMethodCalls
enumValues.remove(((PsiReferenceExpression)caseValue).resolve());
}
}
}
catch (IncorrectOperationException e) {
LOG.error(e);
@@ -0,0 +1,28 @@
import org.jetbrains.annotations.*;
class InspectionTest {
enum Type {
PUBLIC, PRIVATE
}
@Nullable
public static String foo(Type type) {
Object obj = null;
if (type == Type.PUBLIC) {
obj = new Object();
}
switch (type) {
case PUBLIC:
return test(obj);
case PRIVATE:
default:
return null;
}
}
public static String test(@NotNull Object a) {
return a.toString();
}
}
@@ -92,6 +92,7 @@ public class DataFlowInspectionTest extends LightCodeInsightFixtureTestCase {
public void testVisitFinallyOnce() throws Throwable { doTest(); }
public void testNotEqualsDoesntImplyNotNullity() throws Throwable { doTest(); }
public void testEqualsEnumConstant() throws Throwable { doTest(); }
public void testSwitchEnumConstant() { doTest(); }
public void testEnumConstantNotNull() throws Throwable { doTest(); }
public void testEqualsConstant() throws Throwable { doTest(); }
public void testFinalLoopVariableInstanceof() throws Throwable { doTest(); }