summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhippo91 <guillaume.peillex@gmail.com>2018-01-04 12:10:39 +0100
committerGitHub <noreply@github.com>2018-01-04 12:10:39 +0100
commit1f46bffe6a4acdaac2107b2a3f8215d8913baee2 (patch)
treedfaeb9ba6ef7b108792a4b4bd1d8b35ac59300b1
parent28bdc5c4bd4b879a5ea66c3acf835613f51f4b13 (diff)
parent8c71387590fa180d06f25886feb39d2e32af6742 (diff)
downloadpylint-git-1f46bffe6a4acdaac2107b2a3f8215d8913baee2.tar.gz
Merge pull request #1816 from hippo91/1.8
Backport of PR #1806
-rw-r--r--ChangeLog4
-rw-r--r--doc/whatsnew/1.8.rst2
-rw-r--r--pylint/checkers/refactoring.py4
-rw-r--r--pylint/test/functional/inconsistent_returns.py16
-rw-r--r--pylint/test/functional/inconsistent_returns.txt15
5 files changed, 34 insertions, 7 deletions
diff --git a/ChangeLog b/ChangeLog
index 3c1c8730e..fb6b3e258 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -24,6 +24,10 @@ Release data: |TBA|
Close #1770
+ * Fix a false positive ``inconsistent-return-statements`` message when
+ while loop are used.
+
+ Close #1772
What's New in Pylint 1.8.1?
===========================
diff --git a/doc/whatsnew/1.8.rst b/doc/whatsnew/1.8.rst
index 186712248..5690ed5c4 100644
--- a/doc/whatsnew/1.8.rst
+++ b/doc/whatsnew/1.8.rst
@@ -370,3 +370,5 @@ Other Changes
* Fix a false positive inconsistent-return-statements message when if statement is inside try/except.
(backport from 2.0)
+
+* Fix a false positive inconsistent-return-statements message when while loop are used. (backport from 2.0)
diff --git a/pylint/checkers/refactoring.py b/pylint/checkers/refactoring.py
index f65f0df3d..db7877572 100644
--- a/pylint/checkers/refactoring.py
+++ b/pylint/checkers/refactoring.py
@@ -48,6 +48,10 @@ def _is_node_return_ended(node):
# Recursion base case
if isinstance(node, astroid.Return):
return True
+ # Avoid the check inside while loop as we don't know
+ # if they will be completed
+ if isinstance(node, astroid.While):
+ return True
if isinstance(node, astroid.Raise):
# a Raise statement doesn't need to end with a return statement
# but if the exception raised is handled, then the handler has to
diff --git a/pylint/test/functional/inconsistent_returns.py b/pylint/test/functional/inconsistent_returns.py
index a085b2d6e..8532c22de 100644
--- a/pylint/test/functional/inconsistent_returns.py
+++ b/pylint/test/functional/inconsistent_returns.py
@@ -98,6 +98,14 @@ def explicit_returns7(arg):
arg = 3 * arg
return 'above 0'
+def bug_1772():
+ """Don't check inconsistent return statements inside while loop"""
+ counter = 1
+ while True:
+ counter += 1
+ if counter == 100:
+ return 7
+
# Next ones are not consistent
def explicit_implicit_returns(var): # [inconsistent-return-statements]
if var >= 0:
@@ -162,3 +170,11 @@ def blarg(someval):
return 5
except BlargException:
raise
+
+def bug_1772_counter_example(): # [inconsistent-return-statements]
+ counter = 1
+ if counter == 1:
+ while True:
+ counter += 1
+ if counter == 100:
+ return 7
diff --git a/pylint/test/functional/inconsistent_returns.txt b/pylint/test/functional/inconsistent_returns.txt
index 52bf05519..893c00b29 100644
--- a/pylint/test/functional/inconsistent_returns.txt
+++ b/pylint/test/functional/inconsistent_returns.txt
@@ -1,7 +1,8 @@
-inconsistent-return-statements:102:explicit_implicit_returns:Either all return statements in a function should return an expression, or none of them should.
-inconsistent-return-statements:106:empty_explicit_returns:Either all return statements in a function should return an expression, or none of them should.
-inconsistent-return-statements:111:explicit_implicit_returns2:Either all return statements in a function should return an expression, or none of them should.
-inconsistent-return-statements:119:explicit_implicit_returns3:Either all return statements in a function should return an expression, or none of them should.
-inconsistent-return-statements:127:returns_missing_in_catched_exceptions:Either all return statements in a function should return an expression, or none of them should.
-inconsistent-return-statements:137:complex_func:Either all return statements in a function should return an expression, or none of them should.
-inconsistent-return-statements:145:inconsistent_returns_in_nested_function.not_consistent_returns_inner:Either all return statements in a function should return an expression, or none of them should.
+inconsistent-return-statements:110:explicit_implicit_returns:Either all return statements in a function should return an expression, or none of them should.
+inconsistent-return-statements:114:empty_explicit_returns:Either all return statements in a function should return an expression, or none of them should.
+inconsistent-return-statements:119:explicit_implicit_returns2:Either all return statements in a function should return an expression, or none of them should.
+inconsistent-return-statements:127:explicit_implicit_returns3:Either all return statements in a function should return an expression, or none of them should.
+inconsistent-return-statements:135:returns_missing_in_catched_exceptions:Either all return statements in a function should return an expression, or none of them should.
+inconsistent-return-statements:145:complex_func:Either all return statements in a function should return an expression, or none of them should.
+inconsistent-return-statements:153:inconsistent_returns_in_nested_function.not_consistent_returns_inner:Either all return statements in a function should return an expression, or none of them should.
+inconsistent-return-statements:174:bug_1772_counter_example:Either all return statements in a function should return an expression, or none of them should.