summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFantix King <fantix@uchicago.edu>2019-02-20 14:42:13 -0600
committerClaudiu Popa <pcmanticore@gmail.com>2019-02-22 18:09:53 +0100
commit15e405f20c9432f50c677a4026adc76124716861 (patch)
tree57b996ff4d6c24c896e261eb610437e7bd42bd1a
parent8c1116851fadf3f46f9ad2ce7fcbfb5acdf36da3 (diff)
downloadpylint-git-15e405f20c9432f50c677a4026adc76124716861.tar.gz
Fix bug of W0120
When break is deep in the else of an inner loop, W0120 was raised.
-rw-r--r--CONTRIBUTORS.txt2
-rw-r--r--ChangeLog3
-rw-r--r--pylint/checkers/base.py1
-rw-r--r--pylint/test/functional/useless_else_on_loop.py3
-rw-r--r--pylint/test/functional/useless_else_on_loop.txt2
5 files changed, 9 insertions, 2 deletions
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt
index fc905aeaa..4a4336afd 100644
--- a/CONTRIBUTORS.txt
+++ b/CONTRIBUTORS.txt
@@ -269,3 +269,5 @@ contributors:
* Svetoslav Neykov: contributor
* Federico Bond: contributor
+
+* Fantix King (UChicago): contributor
diff --git a/ChangeLog b/ChangeLog
index dad70c637..54d3ec143 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -136,6 +136,9 @@ Release date: TBA
* Fix false positive ``not-callable`` for uninferable properties.
+* Fix false positive ``useless-else-on-loop`` if the break is deep in the else
+ of an inner loop.
+
What's New in Pylint 2.2.2?
===========================
diff --git a/pylint/checkers/base.py b/pylint/checkers/base.py
index 05de23ef8..043700f02 100644
--- a/pylint/checkers/base.py
+++ b/pylint/checkers/base.py
@@ -246,6 +246,7 @@ def _get_break_loop_node(break_node):
while not isinstance(parent, loop_nodes) or break_node in getattr(
parent, "orelse", []
):
+ break_node = parent
parent = parent.parent
if parent is None:
break
diff --git a/pylint/test/functional/useless_else_on_loop.py b/pylint/test/functional/useless_else_on_loop.py
index 465f659f9..8fd178798 100644
--- a/pylint/test/functional/useless_else_on_loop.py
+++ b/pylint/test/functional/useless_else_on_loop.py
@@ -67,7 +67,8 @@ def test_break_in_orelse_deep():
if 3 < 2:
break
else:
- break
+ if 1 < 2:
+ break
else:
return True
return False
diff --git a/pylint/test/functional/useless_else_on_loop.txt b/pylint/test/functional/useless_else_on_loop.txt
index efa1161ab..0d561011f 100644
--- a/pylint/test/functional/useless_else_on_loop.txt
+++ b/pylint/test/functional/useless_else_on_loop.txt
@@ -3,4 +3,4 @@ useless-else-on-loop:18:test_return_while:Else clause on loop without a break st
useless-else-on-loop:28::Else clause on loop without a break statement
useless-else-on-loop:35::Else clause on loop without a break statement
useless-else-on-loop:40::Else clause on loop without a break statement
-useless-else-on-loop:87:test_break_in_orelse_deep2:Else clause on loop without a break statement
+useless-else-on-loop:88:test_break_in_orelse_deep2:Else clause on loop without a break statement