dfa: suggest to replace variable with int constant (IDEA-111178)

This commit is contained in:
peter
2013-07-29 19:04:20 +02:00
parent d1d92234bb
commit 96d78ee5e8
5 changed files with 39 additions and 1 deletions

View File

@@ -271,7 +271,7 @@ public class StandardInstructionVisitor extends InstructionVisitor {
if (methodType == MethodCallInstruction.MethodType.CAST) {
if (qualifierValue instanceof DfaConstValue) {
return factory.getConstFactory().createFromValue(castConstValue((DfaConstValue)qualifierValue), type, null);
return factory.getConstFactory().createFromValue(castConstValue((DfaConstValue)qualifierValue), type, ((DfaConstValue)qualifierValue).getConstant());
}
return qualifierValue;
}

View File

@@ -0,0 +1,10 @@
class Test {
public static final int CONST = 23942;
private void test(int a) {
if (a == CONST) {
System.out.println(<caret><warning descr="Value 'a' is always 'CONST'">a</warning>);
}
}
}

View File

@@ -0,0 +1,10 @@
class Test {
public static final int CONST = 23942;
private void test(int a) {
if (a == CONST) {
System.out.println(<caret>CONST);
}
}
}

View File

@@ -6,4 +6,17 @@ class Test {
}
}
private void test2(int state) {
switch (state) {
case ONE:
case TWO:
if (state == TWO) {
System.out.println("hello");
}
}
}
public static final int ONE = 1;
public static final int TWO = 2;
}

View File

@@ -164,6 +164,11 @@ public class DataFlowInspectionTest extends LightCodeInsightFixtureTestCase {
myFixture.launchAction(myFixture.findSingleIntention("Replace with 'CONST'"));
myFixture.checkResultByFile(getTestName(false) + "_after.java");
}
public void testReportConstantReferences_ReplaceWithIntConstant() {
doTestReplaceConstantReferences();
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();