IDEA-209947 Constant comparison

GitOrigin-RevId: 361545bd9303b8eac1e6c86da577ed5d9aad20c7
This commit is contained in:
Tagir Valeev
2019-05-07 14:13:32 +03:00
committed by intellij-monorepo-bot
parent 22624a343b
commit 55d9f7f395
5 changed files with 44 additions and 12 deletions
@@ -369,7 +369,6 @@ public class TrackingRunner extends StandardDataFlowRunner {
Warning caused by narrowing conversion
Warning caused by unary minus
Warning caused by final field initializer
Literal is not-null
TODO: 3. Check how it works with:
Inliners (notably: Stream API)
Boxed numbers
@@ -932,23 +931,25 @@ public class TrackingRunner extends StandardDataFlowRunner {
}
private static MemoryStateChange findRelationAddedChange(MemoryStateChange history, DfaVariableValue var, Relation relation) {
List<Relation> subRelations;
List<RelationType> subRelations;
switch (relation.myRelationType) {
case NE:
subRelations = Arrays.asList(relation, new Relation(RelationType.GT, relation.myCounterpart),
new Relation(RelationType.LT, relation.myCounterpart));
if (relation.myCounterpart instanceof DfaConstValue) {
return history.findRelation(var, rel -> rel.equals(relation) ||
rel.myRelationType == RelationType.EQ && rel.myCounterpart instanceof DfaConstValue,
true);
}
subRelations = Arrays.asList(RelationType.NE, RelationType.GT, RelationType.LT);
break;
case LE:
subRelations = Arrays.asList(new Relation(RelationType.EQ, relation.myCounterpart),
new Relation(RelationType.LT, relation.myCounterpart));
subRelations = Arrays.asList(RelationType.EQ, RelationType.LT);
break;
case GE:
subRelations = Arrays.asList(new Relation(RelationType.EQ, relation.myCounterpart),
new Relation(RelationType.GT, relation.myCounterpart));
subRelations = Arrays.asList(RelationType.EQ, RelationType.GT);
break;
default:
subRelations = Collections.singletonList(relation);
subRelations = Collections.singletonList(relation.myRelationType);
}
return history.findRelation(var, subRelations::contains, true);
return history.findRelation(var, rel -> rel.myCounterpart == relation.myCounterpart && subRelations.contains(rel.myRelationType), true);
}
}
@@ -0,0 +1,13 @@
/*
Value is always false (s == null; line#11)
Result of 's != null' is known from line #10 (!"foo".equals(s) && !"bar".equals(s); line#10)
*/
import java.util.List;
class Test {
void test(String s) {
if (!"foo".equals(s) && !"bar".equals(s)) return;
if (<selection>s == null</selection>) {}
}
}
@@ -0,0 +1,16 @@
/*
Value is always false (s == null; line#14)
's' was assigned (=; line#13)
One of the following happens:
Expression cannot be null as it's literal ("foo"; line#13)
or expression cannot be null as it's literal ("bar"; line#13)
*/
import java.util.List;
class Test {
void test(boolean b) {
String s = b ? "foo" : "bar";
if (<selection>s == null</selection>) {}
}
}
@@ -1,6 +1,6 @@
/*
Value is always true (list.add("foo"); line#9)
According to contract, method 'add' always returns 'true' value (add; line#9)
Value is always true (list.add("foo"); line#10)
According to contract, method 'add' always returns 'true' value (add; line#10)
*/
import java.util.List;
@@ -156,4 +156,6 @@ public class DataFlowInspectionTrackerTest extends LightCodeInsightFixtureTestCa
public void testInstanceOfSecondCheck() { doTest(); }
public void testNullCheckNpeNullCheck() { doTest(); }
public void testListAddContract() { doTest(); }
public void testConstantStrings() { doTest(); }
public void testConstantStrings2() { doTest(); }
}