From b054fe76759a3853ae3eed043630d0be34193c39 Mon Sep 17 00:00:00 2001 From: Oleg Shpynov Date: Thu, 8 Jul 2010 19:04:59 +0400 Subject: [PATCH] PY-1192 "Unused local variable" false positive with try/finally --- .../PyUnusedLocalVariableInspectionVisitor.java | 4 ++++ .../codeInsight/controlflow/tryfinally.py | 6 ++++++ .../codeInsight/controlflow/tryfinally.txt | 17 +++++++++++++++++ .../python/PyControlFlowBuilderTest.java | 4 ++++ 4 files changed, 31 insertions(+) create mode 100644 python/testData/codeInsight/controlflow/tryfinally.py create mode 100644 python/testData/codeInsight/controlflow/tryfinally.txt diff --git a/python/src/com/jetbrains/python/inspections/PyUnusedLocalVariableInspectionVisitor.java b/python/src/com/jetbrains/python/inspections/PyUnusedLocalVariableInspectionVisitor.java index bc9184f18506..7d70f9331872 100644 --- a/python/src/com/jetbrains/python/inspections/PyUnusedLocalVariableInspectionVisitor.java +++ b/python/src/com/jetbrains/python/inspections/PyUnusedLocalVariableInspectionVisitor.java @@ -150,6 +150,10 @@ class PyUnusedLocalVariableInspectionVisitor extends PyInspectionVisitor { final PsiElement instrElement = rwInstr.getElement(); myUsedElements.add(instrElement); myUnusedElements.remove(instrElement); + // In case when assignment is inside try part we should move further + if (PsiTreeUtil.getParentOfType(instrElement, PyTryPart.class) != null){ + return PyControlFlowUtil.Operation.NEXT; + } return PyControlFlowUtil.Operation.CONTINUE; } }); diff --git a/python/testData/codeInsight/controlflow/tryfinally.py b/python/testData/codeInsight/controlflow/tryfinally.py new file mode 100644 index 000000000000..432c30be2747 --- /dev/null +++ b/python/testData/codeInsight/controlflow/tryfinally.py @@ -0,0 +1,6 @@ +status = None +try: + status = open('/proc/self/status', 'r') +finally: + if status is not None: + status.close() \ No newline at end of file diff --git a/python/testData/codeInsight/controlflow/tryfinally.txt b/python/testData/codeInsight/controlflow/tryfinally.txt new file mode 100644 index 000000000000..66914e427d74 --- /dev/null +++ b/python/testData/codeInsight/controlflow/tryfinally.txt @@ -0,0 +1,17 @@ +0(1) element: null +1(2) element: PyAssignmentStatement +2(3) READ ACCESS: None +3(4) WRITE ACCESS: status +4(5) element: PyTryExceptStatement +5(6) element: PyTryPart +6(7) element: PyAssignmentStatement +7(8) READ ACCESS: open +8(9) WRITE ACCESS: status +9(10) element: PyFinallyPart +10(11) element: PyIfStatement +11(12) READ ACCESS: status +12(13,16) READ ACCESS: None +13(14) element: PyStatementList. Condition: status is not None:true +14(15) element: PyExpressionStatement +15(16) READ ACCESS: status +16() element: null \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyControlFlowBuilderTest.java b/python/testSrc/com/jetbrains/python/PyControlFlowBuilderTest.java index 995f3591c632..2d9127bc314a 100644 --- a/python/testSrc/com/jetbrains/python/PyControlFlowBuilderTest.java +++ b/python/testSrc/com/jetbrains/python/PyControlFlowBuilderTest.java @@ -119,6 +119,10 @@ public class PyControlFlowBuilderTest extends LightMarkedTestCase { doTest(); } + public void testTryFinally() throws Exception { + doTest(); + } + public void testDoubleTry() throws Exception { doTest(); }