diff options
author | hippo91 <guillaume.peillex@gmail.com> | 2021-04-08 11:18:21 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-08 11:18:21 +0200 |
commit | 5540bd45fcfd3db7ddb9a1b952bd5d327f49c84b (patch) | |
tree | ece481fbaf6475062e4302397d0be9024e0c6501 | |
parent | db9b7cbfd131404be19002c5e6d1e143d73832d1 (diff) | |
download | pylint-git-5540bd45fcfd3db7ddb9a1b952bd5d327f49c84b.tar.gz |
Bug pylint 4019 (#4311)
* Detects an `assert False` and consider it as a return node
* Test the detection of `assert False`
* Adds an entry
* Takes into account @cdce8p and @Pierre-Sassoulas remarks
* Formatting
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | pylint/checkers/refactoring/refactoring_checker.py | 7 | ||||
-rw-r--r-- | tests/functional/i/inconsistent/inconsistent_returns.py | 19 | ||||
-rw-r--r-- | tests/functional/i/inconsistent/inconsistent_returns.txt | 1 |
4 files changed, 31 insertions, 0 deletions
@@ -34,6 +34,10 @@ Release date: Undefined .. Put bug fixes that will be cherry-picked to latest major version here +* During detection of ``inconsistent-return-statements`` consider that ``assert False`` is a return node. + + Closes #4019 + * Run will not fail if score exactly equals ``config.fail_under`. * Functions that never returns may declare ``NoReturn`` as type hints, so that diff --git a/pylint/checkers/refactoring/refactoring_checker.py b/pylint/checkers/refactoring/refactoring_checker.py index 57955a23f..b1db89581 100644 --- a/pylint/checkers/refactoring/refactoring_checker.py +++ b/pylint/checkers/refactoring/refactoring_checker.py @@ -1381,6 +1381,13 @@ class RefactoringChecker(checkers.BaseTokenChecker): return any( self._is_node_return_ended(_child) for _child in all_but_handler ) and all(self._is_node_return_ended(_child) for _child in handlers) + if ( + isinstance(node, astroid.Assert) + and isinstance(node.test, astroid.Const) + and not node.test.value + ): + # consider assert False as a return node + return True # recurses on the children of the node return any(self._is_node_return_ended(_child) for _child in node.get_children()) diff --git a/tests/functional/i/inconsistent/inconsistent_returns.py b/tests/functional/i/inconsistent/inconsistent_returns.py index a1782979c..cc8458e6a 100644 --- a/tests/functional/i/inconsistent/inconsistent_returns.py +++ b/tests/functional/i/inconsistent/inconsistent_returns.py @@ -387,3 +387,22 @@ def bug_pylint_4122_bis(s): return n except ValueError: parser_error_name('parser error') + + +# https://github.com/PyCQA/pylint/issues/4019 +def bug_pylint_4019(x): + """ + assert False is equivalent to a return + """ + if x == 1: + return 42 + assert False + + +def bug_pylint_4019_wrong(x): # [inconsistent-return-statements] + """ + assert True is not equivalent to a return + """ + if x == 1: + return 42 + assert True diff --git a/tests/functional/i/inconsistent/inconsistent_returns.txt b/tests/functional/i/inconsistent/inconsistent_returns.txt index 74b0ea1a7..749797f91 100644 --- a/tests/functional/i/inconsistent/inconsistent_returns.txt +++ b/tests/functional/i/inconsistent/inconsistent_returns.txt @@ -15,3 +15,4 @@ inconsistent-return-statements:267:0:bug_3468:Either all return statements in a inconsistent-return-statements:277:0:bug_3468_variant:Either all return statements in a function should return an expression, or none of them should. inconsistent-return-statements:322:0:bug_pylint_3873_1:Either all return statements in a function should return an expression, or none of them should. inconsistent-return-statements:366:0:bug_pylint_4122_wrong:Either all return statements in a function should return an expression, or none of them should. +inconsistent-return-statements:402:0:bug_pylint_4019_wrong:Either all return statements in a function should return an expression, or none of them should. |