Skip to content

Commit 2c9d585

Browse files
authored
Regression fix for unused-variable false negative (#8684)
1 parent 4e48d46 commit 2c9d585

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

pylint/checkers/variables.py

+6
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,12 @@ def _defines_name_raises_or_returns(name: str, node: nodes.NodeNG) -> bool:
950950
return True
951951
if isinstance(node, (nodes.ClassDef, nodes.FunctionDef)) and node.name == name:
952952
return True
953+
if (
954+
isinstance(node, nodes.ExceptHandler)
955+
and node.name
956+
and node.name.name == name
957+
):
958+
return True
953959
return False
954960

955961
@staticmethod

tests/functional/u/unused/unused_variable.py

+17
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,20 @@ def nonlocal_writer():
199199
nonlocal_writer()
200200

201201
assert a == 9, a
202+
203+
def test_regression_8595():
204+
# pylint: disable=broad-exception-caught
205+
import logging
206+
def compute():
207+
pass
208+
try:
209+
compute()
210+
error = False
211+
except Exception as e:
212+
logging.error(e)
213+
error = True
214+
if error:
215+
try:
216+
compute()
217+
except Exception as e: # [unused-variable]
218+
pass

tests/functional/u/unused/unused_variable.txt

+1
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ unused-variable:150:4:154:26:func4:Unused variable 'error':UNDEFINED
2626
redefined-outer-name:153:8:154:26:func4:Redefining name 'error' from outer scope (line 150):UNDEFINED
2727
unused-variable:161:4:162:12:main:Unused variable 'e':UNDEFINED
2828
undefined-loop-variable:168:10:168:11:main:Using possibly undefined loop variable 'e':UNDEFINED
29+
unused-variable:217:8:218:16:test_regression_8595:Unused variable 'e':UNDEFINED

0 commit comments

Comments
 (0)