From a04f2e9512d20f308ce9e4f2c352c0cc3b3bb028 Mon Sep 17 00:00:00 2001 From: "Aleksandr.Govenko" Date: Tue, 25 Mar 2025 12:55:19 +0000 Subject: [PATCH] PY-79986 For loop inside finally statement marked unreachable Merge-request: IJ-MR-158267 Merged-by: Aleksandr Govenko GitOrigin-RevId: 52d7e16c4a6c339773ecbe90fe5d26d7d5ddfb03 --- .../controlflow/PyControlFlowBuilder.java | 2 +- .../inspections/PyUnreachableCodeInspectionTest.java | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java index 691c14d67a8a..db851c0c3671 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java +++ b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java @@ -769,7 +769,7 @@ public class PyControlFlowBuilder extends PyRecursiveElementVisitor { myBuilder.flowAbrupted(); finallyFailInstruction = myBuilder.startNode(finallyPart); finallyPart.accept(this); - myBuilder.addNode(new PyFinallyFailExitInstruction(myBuilder, finallyFailInstruction)); + myBuilder.addNodeAndCheckPending(new PyFinallyFailExitInstruction(myBuilder, finallyFailInstruction)); myBuilder.addPendingEdge(null, myBuilder.prevInstruction); myBuilder.flowAbrupted(); } diff --git a/python/testSrc/com/jetbrains/python/inspections/PyUnreachableCodeInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyUnreachableCodeInspectionTest.java index 6e62cb7a0f03..f743e0302246 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyUnreachableCodeInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyUnreachableCodeInspectionTest.java @@ -24,6 +24,18 @@ public class PyUnreachableCodeInspectionTest extends PyInspectionTestCase { public void testUnreachable() { runWithLanguageLevel(LanguageLevel.PYTHON26, () -> doTest()); } + + // PY-79986 + public void testForInsideFinally() { + doTestByText(""" +def bar(): + try: + return + finally: + for i in range(10): + print("reachable") + """); + } // PY-51564 public void testWithNotContext() {