diff --git a/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java b/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java index 3727f9936db8..1d580254ce18 100644 --- a/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java +++ b/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java @@ -334,10 +334,11 @@ public class PyControlFlowBuilder extends PyRecursiveElementVisitor { if (list != null) { myBuilder.startConditionalNode(list, condition, true); list.accept(this); - final Instruction prevInstruction = myBuilder.prevInstruction != null ? myBuilder.prevInstruction : getPrevInstruction(list); - if (prevInstruction != null) { - myBuilder.addEdge(prevInstruction, instruction); //loop + // Loop edges + if (myBuilder.prevInstruction != null) { + myBuilder.addEdge(myBuilder.prevInstruction, instruction); } + myBuilder.checkPending(instruction); } myBuilder.prevInstruction = head; if (elsePart != null) { diff --git a/python/testData/inspections/PyUnusedLocalVariableInspection/test.py b/python/testData/inspections/PyUnusedLocalVariableInspection/test.py index 345b4ce18440..5edffda057d2 100644 --- a/python/testData/inspections/PyUnusedLocalVariableInspection/test.py +++ b/python/testData/inspections/PyUnusedLocalVariableInspection/test.py @@ -294,3 +294,12 @@ def test_unused_variable_in_cycle(x, c): x -= 1 #pass if c: break + + +# PY-7517 +def test_unused_condition_local_with_last_if_in_cycle(c): + x = True + while x: + x = False #pass + if c: + x = True