From 5e168b68c685f512444360f8937e5900c40093df Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Wed, 13 Mar 2024 17:17:08 +0100 Subject: [PATCH] [java-dfa] IDEA-349093 Cast to unresolved reference inside try-catch causes 'Unreachable code' warning GitOrigin-RevId: c9098e741070cddd1c89936045481521d5823bf7 --- .../dataFlow/java/inst/TypeCastInstruction.java | 2 +- .../dataFlow/unreachableCode/UnresolvedCast.java | 11 +++++++++++ .../dataFlow/UnreachableCodeInspectionTest.java | 2 ++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/inspection/dataFlow/unreachableCode/UnresolvedCast.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/inst/TypeCastInstruction.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/inst/TypeCastInstruction.java index b1839e95b0cb..a341ae7103c2 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/inst/TypeCastInstruction.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/inst/TypeCastInstruction.java @@ -79,7 +79,7 @@ public class TypeCastInstruction extends ExpressionPushingInstruction { List result = new ArrayList<>(); if (myTransferValue != null) { DfaMemoryState castFail = stateBefore.createCopy(); - if (fromType != null && myCastTo.isConvertibleFrom(fromType)) { + if (fromType != null && (!constraint.isResolved() || myCastTo.isConvertibleFrom(fromType))) { if (!castTopOfStack(factory, stateBefore, constraint)) { castPossible = false; } else { diff --git a/java/java-tests/testData/inspection/dataFlow/unreachableCode/UnresolvedCast.java b/java/java-tests/testData/inspection/dataFlow/unreachableCode/UnresolvedCast.java new file mode 100644 index 000000000000..dc7405688da4 --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/unreachableCode/UnresolvedCast.java @@ -0,0 +1,11 @@ + +class Test { + void test(Object obj) { + try { + System.out.println((MyUnresolvedClass)obj); + System.out.println("hello"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/dataFlow/UnreachableCodeInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/dataFlow/UnreachableCodeInspectionTest.java index 36393274c446..620942718e57 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/dataFlow/UnreachableCodeInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/dataFlow/UnreachableCodeInspectionTest.java @@ -41,6 +41,8 @@ public class UnreachableCodeInspectionTest extends LightJavaCodeInsightFixtureTe public void testLambdaCast() { doTest(); } public void testFailingCast() { doTest(); } + + public void testUnresolvedCast() { doTest(); } private void doTest() { myFixture.configureByFile(getTestName(false) + ".java");