mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
don't highlight usages of compile-time constants in variable initializers as always true/false (IDEA-157715)
This commit is contained in:
+8
-8
@@ -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><default></package>
|
||||
<class>
|
||||
<name>Y</name>
|
||||
<display_name>Y</display_name>
|
||||
</class>
|
||||
</method>
|
||||
<problem_class>Constant conditions & exceptions</problem_class>
|
||||
<description>Condition <code>b</code> is always <code>true</code>.</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>Test.java</file>
|
||||
<line>5</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Constant conditions & exceptions</problem_class>
|
||||
<description>Condition <code>CONST</code> is always <code>true</code></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(); }
|
||||
|
||||
Reference in New Issue
Block a user