From 9a859a4e65aa907ecfd867fcd367abb449b713a8 Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Mon, 10 Sep 2012 16:58:37 +0400 Subject: [PATCH] Fixed false positive in unused locals for condition vars in while loops with if statement at the end (PY-7517) --- .../codeInsight/controlflow/PyControlFlowBuilder.java | 7 ++++--- .../inspections/PyUnusedLocalVariableInspection/test.py | 9 +++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) 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