mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-134677 ConstantCondition inspection could be smarter for enums
This commit is contained in:
+12
-10
@@ -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(); }
|
||||
|
||||
Reference in New Issue
Block a user