summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhippo91 <guillaume.peillex@gmail.com>2018-01-25 20:30:45 +0100
committerGitHub <noreply@github.com>2018-01-25 20:30:45 +0100
commit9e17f0c3864feb800ab19d4513d1fc63927d8e4f (patch)
tree0332acb235a867c48df4dd29b0949f02f63fc80c
parent0d5faa90ac33adda76a373057e6aeb4b8e110afe (diff)
parentff41e7181f8e55c08d137246a0f2caf17a9840f4 (diff)
downloadpylint-git-9e17f0c3864feb800ab19d4513d1fc63927d8e4f.tar.gz
Merge pull request #1849 from hippo91/1.8
Backport of PR #1846
-rw-r--r--ChangeLog12
-rw-r--r--doc/whatsnew/1.8.rst3
-rw-r--r--pylint/checkers/refactoring.py4
-rw-r--r--pylint/test/functional/inconsistent_returns.py39
-rw-r--r--pylint/test/functional/inconsistent_returns.txt22
5 files changed, 69 insertions, 11 deletions
diff --git a/ChangeLog b/ChangeLog
index 2794a221e..54df5a277 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,10 +2,20 @@
Pylint's ChangeLog
------------------
+What's New in Pylint 1.8.3?
+==========================
+
+Release date: -
+
+ * Fix false positive ``inconsistent-return-statements`` message when a
+ function is defined under an if statement.
+
+ Close #1794
+
What's New in Pylint 1.8.2?
==========================
-Release data: 2018-01-23
+Release date: 2018-01-23
* Fixed a crash which occurred when `Uninferable` wasn't properly handled in `stop-iteration-return`
diff --git a/doc/whatsnew/1.8.rst b/doc/whatsnew/1.8.rst
index e92b9d143..a0cf7d3ac 100644
--- a/doc/whatsnew/1.8.rst
+++ b/doc/whatsnew/1.8.rst
@@ -378,3 +378,6 @@ Other Changes
* Fixing false positive ``inconsistent-return-statements`` when
never returning functions are used (i.e such as sys.exit). (backport from 2.0)
+
+* Fix false positive ``inconsistent-return-statements`` message when a function
+ is defined under an if statement. (backport from 2.0)
diff --git a/pylint/checkers/refactoring.py b/pylint/checkers/refactoring.py
index 4032b6230..1f437799d 100644
--- a/pylint/checkers/refactoring.py
+++ b/pylint/checkers/refactoring.py
@@ -583,7 +583,9 @@ class RefactoringChecker(checkers.BaseTokenChecker):
if isinstance(node, astroid.If):
# if statement is returning if there are exactly two return statements in its
# children : one for the body part, the other for the orelse part
- return_stmts = [self._is_node_return_ended(_child) for _child in node.get_children()]
+ # Do not check if inner function definition are return ended.
+ return_stmts = [self._is_node_return_ended(_child) for _child in node.get_children()
+ if not isinstance(_child, astroid.FunctionDef)]
return sum(return_stmts) == 2
# recurses on the children of the node except for those which are except handler
# because one cannot be sure that the handler will really be used
diff --git a/pylint/test/functional/inconsistent_returns.py b/pylint/test/functional/inconsistent_returns.py
index e8c04f0e9..a6ec2cd1f 100644
--- a/pylint/test/functional/inconsistent_returns.py
+++ b/pylint/test/functional/inconsistent_returns.py
@@ -121,6 +121,15 @@ def bug_1771_with_user_config(var):
else:
return var * 2
+def bug_1794_inner_func_in_if(var):
+ # pylint: disable = no-else-return
+ if var:
+ def _inner():
+ return None
+ return None
+ else:
+ return None
+
# Next ones are not consistent
def explicit_implicit_returns(var): # [inconsistent-return-statements]
if var >= 0:
@@ -199,3 +208,33 @@ def bug_1771_counter_example(var): # [inconsistent-return-statements]
inconsistent_returns_in_nested_function()
else:
return var * 2
+
+def bug_1794_inner_func_in_if_counter_example_1(var): # [inconsistent-return-statements]
+ # pylint: disable = no-else-return
+ if var:
+ def _inner():
+ return None
+ return None
+ else:
+ return
+
+def bug_1794_inner_func_in_if_counter_example_2(var): # [inconsistent-return-statements]
+ # pylint: disable = no-else-return
+ if var:
+ def _inner():
+ return
+ return None
+ else:
+ return
+
+def bug_1794_inner_func_in_if_counter_example_3(var): # [inconsistent-return-statements]
+ # pylint: disable = no-else-return
+ if var:
+ def _inner():
+ return None
+ return None
+ else:
+ def _inner2(var_bis): # [inconsistent-return-statements]
+ if var_bis:
+ return True
+ return
diff --git a/pylint/test/functional/inconsistent_returns.txt b/pylint/test/functional/inconsistent_returns.txt
index 6ce725665..08ce9d7d9 100644
--- a/pylint/test/functional/inconsistent_returns.txt
+++ b/pylint/test/functional/inconsistent_returns.txt
@@ -1,9 +1,13 @@
-inconsistent-return-statements:125:explicit_implicit_returns:Either all return statements in a function should return an expression, or none of them should.
-inconsistent-return-statements:129:empty_explicit_returns:Either all return statements in a function should return an expression, or none of them should.
-inconsistent-return-statements:134:explicit_implicit_returns2:Either all return statements in a function should return an expression, or none of them should.
-inconsistent-return-statements:142:explicit_implicit_returns3:Either all return statements in a function should return an expression, or none of them should.
-inconsistent-return-statements:150:returns_missing_in_catched_exceptions:Either all return statements in a function should return an expression, or none of them should.
-inconsistent-return-statements:160:complex_func:Either all return statements in a function should return an expression, or none of them should.
-inconsistent-return-statements:168: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:189:bug_1772_counter_example:Either all return statements in a function should return an expression, or none of them should.
-inconsistent-return-statements:197:bug_1771_counter_example:Either all return statements in a function should return an expression, or none of them should.
+inconsistent-return-statements:134:explicit_implicit_returns:Either all return statements in a function should return an expression, or none of them should.
+inconsistent-return-statements:138:empty_explicit_returns:Either all return statements in a function should return an expression, or none of them should.
+inconsistent-return-statements:143:explicit_implicit_returns2:Either all return statements in a function should return an expression, or none of them should.
+inconsistent-return-statements:151:explicit_implicit_returns3:Either all return statements in a function should return an expression, or none of them should.
+inconsistent-return-statements:159:returns_missing_in_catched_exceptions:Either all return statements in a function should return an expression, or none of them should.
+inconsistent-return-statements:169:complex_func:Either all return statements in a function should return an expression, or none of them should.
+inconsistent-return-statements:177: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:198:bug_1772_counter_example:Either all return statements in a function should return an expression, or none of them should.
+inconsistent-return-statements:206:bug_1771_counter_example:Either all return statements in a function should return an expression, or none of them should.
+inconsistent-return-statements:212:bug_1794_inner_func_in_if_counter_example_1:Either all return statements in a function should return an expression, or none of them should.
+inconsistent-return-statements:221:bug_1794_inner_func_in_if_counter_example_2:Either all return statements in a function should return an expression, or none of them should.
+inconsistent-return-statements:230:bug_1794_inner_func_in_if_counter_example_3:Either all return statements in a function should return an expression, or none of them should.
+inconsistent-return-statements:237:bug_1794_inner_func_in_if_counter_example_3._inner2:Either all return statements in a function should return an expression, or none of them should.