PY-1192 "Unused local variable" false positive with try/finally

This commit is contained in:
Oleg Shpynov
2010-07-08 19:09:02 +04:00
parent fd5fe7d94b
commit b054fe7675
4 changed files with 31 additions and 0 deletions
@@ -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;
}
});
@@ -0,0 +1,6 @@
status = None
try:
status = open('/proc/self/status', 'r')
finally:
if status is not None:
status.close()
@@ -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
@@ -119,6 +119,10 @@ public class PyControlFlowBuilderTest extends LightMarkedTestCase {
doTest();
}
public void testTryFinally() throws Exception {
doTest();
}
public void testDoubleTry() throws Exception {
doTest();
}