IDEA-42948 Idea fails to recognize "non-nullness" after unchecked cast

This commit is contained in:
peter
2012-10-29 12:57:22 +01:00
parent 30769e9b16
commit 51515e63ed
3 changed files with 21 additions and 25 deletions
@@ -28,13 +28,11 @@ import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.RedundantCastUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.refactoring.psi.PropertyUtils;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.containers.Stack;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
@@ -1596,33 +1594,14 @@ class ControlFlowAnalyzer extends JavaElementVisitor {
pushTypeOrUnknown(castExpression);
}
addInstruction(createCastInstruction(castExpression));
final PsiTypeElement typeElement = castExpression.getCastType();
if (typeElement != null && operand != null) {
addInstruction(new TypeCastInstruction(castExpression, operand, typeElement.getType()));
}
finishElement(castExpression);
}
@Override public void visitClass(PsiClass aClass) {
}
@NotNull
private static Instruction createCastInstruction(PsiTypeCastExpression castExpression) {
PsiExpression expr = castExpression.getOperand();
final PsiTypeElement typeElement = castExpression.getCastType();
if (typeElement != null && !RedundantCastUtil.isTypeCastSemantical(castExpression)) {
PsiType castType = typeElement.getType();
if (expr != null) {
return new TypeCastInstruction(castExpression, expr, castType);
}
}
return new Instruction() {
@Override
public DfaInstructionState[] accept(DataFlowRunner runner, DfaMemoryState stateBefore, InstructionVisitor visitor) {
stateBefore.pop();
stateBefore.push(DfaUnknownValue.getInstance());
return nextInstruction(runner, stateBefore);
}
};
}
}
@@ -0,0 +1,16 @@
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class BrokenAlignment {
void t() {
@NotNull Collection list = new ArrayList();
List<String> strings = (List<String>) list;
if (<warning descr="Condition 'strings != null' is always 'true'">strings != null</warning>) {
int foo = 42;
}
}
}
@@ -90,5 +90,6 @@ public class DataFlowInspectionFixtureTest extends JavaCodeInsightFixtureTestCas
public void testReturningNullFromVoidMethod() throws Throwable { doTest(); }
public void testCatchRuntimeException() throws Throwable { doTest(); }
public void testPreserveNullableOnUncheckedCast() throws Throwable { doTest(); }
}