don't highlight usages of compile-time constants in variable initializers as always true/false (IDEA-157715)

This commit is contained in:
peter
2017-01-21 14:22:08 +01:00
parent d141da325e
commit edadcd1fb7
5 changed files with 10 additions and 42 deletions
@@ -402,7 +402,7 @@ public class DataFlowInspectionBase extends BaseJavaBatchLocalInspectionTool {
PsiExpression place = ((PushInstruction)instruction).getPlace();
DfaValue value = ((PushInstruction)instruction).getValue();
Object constant = value instanceof DfaConstValue ? ((DfaConstValue)value).getValue() : null;
if (place instanceof PsiPolyadicExpression && constant instanceof Boolean && reportedAnchors.add(place)) {
if (place instanceof PsiPolyadicExpression && constant instanceof Boolean && !isFlagCheck(place) && reportedAnchors.add(place)) {
reportConstantCondition(holder, visitor, place, (Boolean)constant);
}
}
@@ -739,15 +739,15 @@ public class DataFlowInspectionBase extends BaseJavaBatchLocalInspectionTool {
}
private static boolean isFlagCheck(PsiElement element) {
PsiStatement statement = PsiTreeUtil.getParentOfType(element, PsiStatement.class);
if (!(statement instanceof PsiIfStatement)) return false;
PsiElement scope = PsiTreeUtil.getParentOfType(element, PsiStatement.class, PsiVariable.class);
PsiExpression topExpression = scope instanceof PsiIfStatement ? ((PsiIfStatement)scope).getCondition() :
scope instanceof PsiVariable ? ((PsiVariable)scope).getInitializer() :
null;
if (!PsiTreeUtil.isAncestor(topExpression, element, false)) return false;
PsiExpression condition = ((PsiIfStatement)statement).getCondition();
if (!PsiTreeUtil.isAncestor(condition, element, false)) return false;
if (isCompileTimeFlagReference(topExpression)) return true;
if (isCompileTimeFlagReference(condition)) return true;
Collection<PsiReferenceExpression> refs = PsiTreeUtil.findChildrenOfType(condition, PsiReferenceExpression.class);
Collection<PsiReferenceExpression> refs = PsiTreeUtil.findChildrenOfType(topExpression, PsiReferenceExpression.class);
return ContainerUtil.or(refs, DataFlowInspectionBase::isCompileTimeFlagReference);
}
@@ -1,24 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>6</line>
<method>
<name>void foo(boolean param)</name>
<display_name>foo(boolean)</display_name>
<package>&lt;default&gt;</package>
<class>
<name>Y</name>
<display_name>Y</display_name>
</class>
</method>
<problem_class>Constant conditions &amp; exceptions</problem_class>
<description>Condition &lt;code&gt;b&lt;/code&gt; is always &lt;code&gt;true&lt;/code&gt;.</description>
</problem>
<problem>
<file>Test.java</file>
<line>5</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Constant conditions &amp; exceptions</problem_class>
<description>Condition &lt;code&gt;CONST&lt;/code&gt; is always &lt;code&gt;true&lt;/code&gt;</description>
</problem>
</problems>
@@ -1,9 +0,0 @@
public class Y {
private static final boolean CONST = true;
public void foo(boolean param) {
boolean b = CONST || param;
if (b) {
}
}
}
@@ -1,5 +1,7 @@
class Fun {
public static final boolean isDebug = true;
public static final boolean isProduction = false;
public static final boolean isDebugInProduction = isDebug && isProduction;
void foo() {
if (isDebug) {
@@ -100,7 +100,6 @@ public class DataFlowInspectionAncientTest extends InspectionTestCase {
public void testForEachNPE() { doTest15(); }
public void testArrayAccessNPE() { doTest15(); }
public void testArrayAccessDoesntCancelAnalysis() { doTest15(); }
public void testCompileTimeConst() { doTest15(true); }
public void testAutoboxing() { doTest15(true); }
public void testUnboxingNPE() { doTest15(true); }
public void testStrangeArrayIndexOutOfBounds() { doTest15(); }