Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,12 @@ def _defines_name_raises_or_returns(name: str, node: nodes.NodeNG) -> bool:
return True
if isinstance(node, (nodes.ClassDef, nodes.FunctionDef)) and node.name == name:
return True
if (
isinstance(node, nodes.ExceptHandler)
and node.name
and node.name.name == name
):
return True
return False

@staticmethod
Expand Down
17 changes: 17 additions & 0 deletions tests/functional/u/unused/unused_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,20 @@ def nonlocal_writer():
nonlocal_writer()

assert a == 9, a

def test_regression_8595():
# pylint: disable=broad-exception-caught
import logging
def compute():
pass
try:
compute()
error = False
except Exception as e:
logging.error(e)
error = True
if error:
try:
compute()
except Exception as e: # [unused-variable]
pass
1 change: 1 addition & 0 deletions tests/functional/u/unused/unused_variable.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ unused-variable:150:4:154:26:func4:Unused variable 'error':UNDEFINED
redefined-outer-name:153:8:154:26:func4:Redefining name 'error' from outer scope (line 150):UNDEFINED
unused-variable:161:4:162:12:main:Unused variable 'e':UNDEFINED
undefined-loop-variable:168:10:168:11:main:Using possibly undefined loop variable 'e':UNDEFINED
unused-variable:217:8:218:16:test_regression_8595:Unused variable 'e':UNDEFINED