From 434301c3b983f933ba2dbe4b6c06de99f36afa87 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Fri, 26 Jan 2024 12:43:23 +0100 Subject: [PATCH] [java-dfa] Stop analysis completely after impossible cast outside of try-catch Improves UnreachableCodeInspection GitOrigin-RevId: 87a98ad8c3c0ca544e86c9bb1c98267dc939a728 --- .../dataFlow/java/inst/TypeCastInstruction.java | 4 +++- .../inspection/dataFlow/unreachableCode/FailingCast.java | 8 ++++++++ .../dataFlow/UnreachableCodeInspectionTest.java | 2 ++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/inspection/dataFlow/unreachableCode/FailingCast.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 884323966712..4edaeba0d2dc 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 @@ -103,7 +103,9 @@ public class TypeCastInstruction extends ExpressionPushingInstruction { } } - result.add(nextState(interpreter, stateBefore)); + if (castPossible) { + result.add(nextState(interpreter, stateBefore)); + } pushResult(interpreter, stateBefore, stateBefore.pop()); } UnsatisfiedConditionProblem problem = getConditionProblem(); diff --git a/java/java-tests/testData/inspection/dataFlow/unreachableCode/FailingCast.java b/java/java-tests/testData/inspection/dataFlow/unreachableCode/FailingCast.java new file mode 100644 index 000000000000..ca271334befb --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/unreachableCode/FailingCast.java @@ -0,0 +1,8 @@ + +class Test { + public static void main(String[] args) { + Object o = "12"; + Integer a = (Integer) o; + System.out.println("1"); + } +} \ 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 15390325f7ff..36393274c446 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 @@ -39,6 +39,8 @@ public class UnreachableCodeInspectionTest extends LightJavaCodeInsightFixtureTe public void testCatchLinkageError() { doTest(); } public void testLambdaCast() { doTest(); } + + public void testFailingCast() { doTest(); } private void doTest() { myFixture.configureByFile(getTestName(false) + ".java");