IDEA-69573 ConstantConditions - Warn when reading a variable that is guaranteed to be null

This commit is contained in:
peter
2013-07-09 16:12:33 +02:00
parent de046ce8d9
commit e5c829c66b
11 changed files with 208 additions and 31 deletions
@@ -541,6 +541,19 @@ public class DfaMemoryStateImpl implements DfaMemoryState {
return false;
}
@Override
@Nullable
public DfaConstValue getConstantValue(DfaVariableValue value) {
DfaConstValue result = null;
for (DfaValue equal : getEqClassesFor(value)) {
if (equal == value) continue;
DfaConstValue constValue = asConstantValue(equal);
if (constValue == null) return null;
result = constValue;
}
return result;
}
@Override
public boolean applyInstanceofOrNull(DfaRelationValue dfaCond) {
DfaValue left = dfaCond.getLeftOperand();
@@ -886,13 +899,16 @@ public class DfaMemoryStateImpl implements DfaMemoryState {
myVariableStates.remove(varNegated);
}
@Nullable private static DfaConstValue asConstantValue(DfaValue value) {
if (value instanceof DfaConstValue) return (DfaConstValue)value;
if (value instanceof DfaBoxedValue && ((DfaBoxedValue)value).getWrappedValue() instanceof DfaConstValue) return (DfaConstValue)((DfaBoxedValue)value).getWrappedValue();
return null;
}
private boolean containsConstantsOnly(int id) {
SortedIntSet varClass = myEqClasses.get(id);
for (int i = 0; i < varClass.size(); i++) {
int cl = varClass.get(i);
DfaValue value = myFactory.getValue(cl);
if (!(value instanceof DfaConstValue) &&
!(value instanceof DfaBoxedValue && ((DfaBoxedValue)value).getWrappedValue() instanceof DfaConstValue)) {
if (asConstantValue(myFactory.getValue(varClass.get(i))) == null) {
return false;
}
}