Skip to content

Commit

Permalink
Fix utils.is_error to account for functions returning early.
Browse files Browse the repository at this point in the history
This fixes a false negative with ``unused-variable`` which was no longer triggered
when a function raised an exception as the last instruction, but the body of the function
still had unused variables.

Close #3028
  • Loading branch information
PCManticore committed Sep 30, 2019
1 parent 4ec293c commit 552fa8a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
8 changes: 8 additions & 0 deletions ChangeLog
Expand Up @@ -22,6 +22,14 @@ Release date: TBA

Close #3141

* Fix ``utils.is_error`` to account for functions returning early.

This fixes a false negative with ``unused-variable`` which was no longer triggered
when a function raised an exception as the last instruction, but the body of the function
still had unused variables.

Close #3028


What's New in Pylint 2.4.1?
===========================
Expand Down
10 changes: 7 additions & 3 deletions pylint/checkers/utils.py
Expand Up @@ -266,10 +266,14 @@ def is_super(node: astroid.node_classes.NodeNG) -> bool:

def is_error(node: astroid.node_classes.NodeNG) -> bool:
"""return true if the function does nothing but raising an exception"""
for child_node in node.get_children():
raises = False
returns = False
for child_node in node.nodes_of_class((astroid.Raise, astroid.Return)):
if isinstance(child_node, astroid.Raise):
return True
return False
raises = True
if isinstance(child_node, astroid.Return):
returns = True
return raises and not returns


builtins = builtins.__dict__.copy() # type: ignore
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/u/unused_variable.py
Expand Up @@ -58,3 +58,10 @@ def unused_import_from():
def unused_import_in_function(value):
from six import PY2, text_type # [unused-import]
return value.encode("utf-8") if PY2 else value


def hello(arg):
my_var = 'something' # [unused-variable]
if arg:
return True
raise Exception
1 change: 1 addition & 0 deletions tests/functional/u/unused_variable.txt
Expand Up @@ -11,3 +11,4 @@ unused-variable:46:locals_does_not_account_for_subscopes:Unused variable 'value'
unused-import:54:unused_import_from:Unused wraps imported from functools as abc
unused-import:55:unused_import_from:Unused namedtuple imported from collections
unused-import:59:unused_import_in_function:Unused text_type imported from six
unused-variable:64:hello:Unused variable 'my_var'

0 comments on commit 552fa8a

Please sign in to comment.