From 51515e63ed6ccb46b9b7902bd7a97491a75d7f8f Mon Sep 17 00:00:00 2001 From: peter Date: Sun, 28 Oct 2012 17:56:14 +0100 Subject: [PATCH] IDEA-42948 Idea fails to recognize "non-nullness" after unchecked cast --- .../dataFlow/ControlFlowAnalyzer.java | 29 +++---------------- .../PreserveNullableOnUncheckedCast.java | 16 ++++++++++ .../DataFlowInspectionFixtureTest.java | 1 + 3 files changed, 21 insertions(+), 25 deletions(-) create mode 100644 java/java-tests/testData/inspection/dataFlow/fixture/PreserveNullableOnUncheckedCast.java diff --git a/java/java-impl/src/com/intellij/codeInspection/dataFlow/ControlFlowAnalyzer.java b/java/java-impl/src/com/intellij/codeInspection/dataFlow/ControlFlowAnalyzer.java index 63de65d5db4f..ed0df551fe51 100644 --- a/java/java-impl/src/com/intellij/codeInspection/dataFlow/ControlFlowAnalyzer.java +++ b/java/java-impl/src/com/intellij/codeInspection/dataFlow/ControlFlowAnalyzer.java @@ -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); - } - }; - } - - } diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/PreserveNullableOnUncheckedCast.java b/java/java-tests/testData/inspection/dataFlow/fixture/PreserveNullableOnUncheckedCast.java new file mode 100644 index 000000000000..f063d128d9ca --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/PreserveNullableOnUncheckedCast.java @@ -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 strings = (List) list; + if (strings != null) { + int foo = 42; + } + } + +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionFixtureTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionFixtureTest.java index f2ad9d55233f..e32605c17a37 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionFixtureTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionFixtureTest.java @@ -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(); } }