no matter if there are other variables having that constant value, it's still constant

This commit is contained in:
peter
2013-09-05 16:27:41 +02:00
parent 68d131bbbc
commit 559ab965eb
3 changed files with 28 additions and 8 deletions
@@ -549,7 +549,7 @@ public class DfaMemoryStateImpl implements DfaMemoryState {
public DfaConstValue getConstantValue(DfaVariableValue value) {
DfaConstValue result = null;
for (DfaValue equal : getEqClassesFor(value)) {
if (equal == value) continue;
if (equal instanceof DfaVariableValue) continue;
DfaConstValue constValue = asConstantValue(equal);
if (constValue == null) return null;
result = constValue;
@@ -0,0 +1,18 @@
public class Foo {
private final boolean field = hashCode() == 2;
public void main(boolean b) {
if (!b) {
System.out.println(<warning descr="Value 'b' is always 'false'">b</warning>);
}
if (field) {
System.out.println(b);
}
if (b) {
System.out.println(<warning descr="Value 'b' is always 'true'">b</warning>);
}
}
}
@@ -147,12 +147,14 @@ public class DataFlowInspectionTest extends LightCodeInsightFixtureTestCase {
}
public void testReportConstantReferences() {
doTestReplaceConstantReferences();
doTestReportConstantReferences();
myFixture.launchAction(myFixture.findSingleIntention("Replace with 'null'"));
myFixture.checkResultByFile(getTestName(false) + "_after.java");
}
private void doTestReplaceConstantReferences() {
public void testReportConstantReferencesAfterFinalFieldAccess() { doTestReportConstantReferences(); }
private void doTestReportConstantReferences() {
DataFlowInspection inspection = new DataFlowInspection();
inspection.SUGGEST_NULLABLE_ANNOTATIONS = true;
myFixture.enableInspections(inspection);
@@ -160,27 +162,27 @@ public class DataFlowInspectionTest extends LightCodeInsightFixtureTestCase {
}
public void testReportConstantReferences_ReplaceWithString() {
doTestReplaceConstantReferences();
doTestReportConstantReferences();
myFixture.launchAction(myFixture.findSingleIntention("Replace with 'CONST'"));
myFixture.checkResultByFile(getTestName(false) + "_after.java");
}
public void testReportConstantReferences_ReplaceWithIntConstant() {
doTestReplaceConstantReferences();
doTestReportConstantReferences();
myFixture.launchAction(myFixture.findSingleIntention("Replace with 'CONST'"));
myFixture.checkResultByFile(getTestName(false) + "_after.java");
}
public void testReportConstantReferences_ReplaceWithEnum() {
myFixture.addClass("package foo; public enum MyEnum { FOO }");
doTestReplaceConstantReferences();
doTestReportConstantReferences();
myFixture.launchAction(myFixture.findSingleIntention("Replace with 'FOO'"));
myFixture.checkResultByFile(getTestName(false) + "_after.java");
}
public void testReportConstantReferences_NotInComplexAssignment() {
myFixture.addClass("package foo; public enum MyEnum { FOO }");
doTestReplaceConstantReferences();
doTestReportConstantReferences();
assertEmpty(myFixture.filterAvailableIntentions("Replace with"));
}
public void testReportConstantReferences_Switch() { doTestReplaceConstantReferences(); }
public void testReportConstantReferences_Switch() { doTestReportConstantReferences(); }
public void testCheckFieldInitializers() {
doTest();