summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <cpopa@cloudbasesolutions.com>2015-08-02 21:06:16 +0300
committerClaudiu Popa <cpopa@cloudbasesolutions.com>2015-08-02 21:06:16 +0300
commit1b32113dc0ed20312b4bf0ac9cc6d00009579748 (patch)
treecc5fcb6ca6bdb9a265005de734b50cbb1786f4fa
parent2057d563aa42052cb77febe0fd4a8a9dd871379d (diff)
downloadpylint-1b32113dc0ed20312b4bf0ac9cc6d00009579748.tar.gz
Improved the not-in-loop checker to properly detect more cases.
-rw-r--r--ChangeLog2
-rw-r--r--pylint/checkers/base.py9
-rw-r--r--pylint/test/functional/not_in_loop.py60
-rw-r--r--pylint/test/functional/not_in_loop.txt8
-rw-r--r--pylint/test/input/func_continue_not_in_loop.py14
-rw-r--r--pylint/test/messages/func_continue_not_in_loop.txt2
6 files changed, 77 insertions, 18 deletions
diff --git a/ChangeLog b/ChangeLog
index 17f34dd..6ec70e2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -231,6 +231,8 @@ ChangeLog for Pylint
* --generate-rcfile generates by default human readable symbols
for the --disable option. Closes issue #608.
+
+ * Improved the not-in-loop checker to properly detect more cases.
diff --git a/pylint/checkers/base.py b/pylint/checkers/base.py
index 13fc261..81d5801 100644
--- a/pylint/checkers/base.py
+++ b/pylint/checkers/base.py
@@ -490,10 +490,15 @@ class BasicErrorChecker(_BasicChecker):
_node = node.parent
while _node:
if isinstance(_node, (astroid.For, astroid.While)):
+ if node not in _node.orelse:
+ return
+
+ if isinstance(_node, (astroid.Class, astroid.Function)):
break
+
_node = _node.parent
- else:
- self.add_message('not-in-loop', node=node, args=node_name)
+
+ self.add_message('not-in-loop', node=node, args=node_name)
def _check_redefinition(self, redeftype, node):
"""check for redefinition of a function / method / class name"""
diff --git a/pylint/test/functional/not_in_loop.py b/pylint/test/functional/not_in_loop.py
new file mode 100644
index 0000000..708d5d2
--- /dev/null
+++ b/pylint/test/functional/not_in_loop.py
@@ -0,0 +1,60 @@
+"""Test that not-in-loop is detected properly."""
+# pylint: disable=missing-docstring, invalid-name, too-few-public-methods
+# pylint: disable=useless-else-on-loop, using-constant-test
+
+while True:
+ def ala():
+ continue # [not-in-loop]
+
+while True:
+ pass
+else:
+ continue # [not-in-loop]
+
+while True:
+ try:
+ pass
+ finally:
+ continue
+
+def lala():
+ continue # [not-in-loop]
+
+while True:
+ class A(object):
+ continue # [not-in-loop]
+
+for _ in range(10):
+ pass
+else:
+ continue # [not-in-loop]
+
+for _ in range(42):
+ pass
+else:
+ break # [not-in-loop]
+
+if True:
+ continue # [not-in-loop]
+else:
+ break # [not-in-loop]
+
+for _ in range(10):
+ for _ in range(20):
+ pass
+ else:
+ continue
+
+while True:
+ while True:
+ break
+ else:
+ break
+ break
+else:
+ pass
+
+for _ in range(1):
+ continue
+for _ in range(42):
+ break
diff --git a/pylint/test/functional/not_in_loop.txt b/pylint/test/functional/not_in_loop.txt
new file mode 100644
index 0000000..a762f07
--- /dev/null
+++ b/pylint/test/functional/not_in_loop.txt
@@ -0,0 +1,8 @@
+not-in-loop:7:ala:'continue' not properly in loop
+not-in-loop:12::'continue' not properly in loop
+not-in-loop:21:lala:'continue' not properly in loop
+not-in-loop:25:A:'continue' not properly in loop
+not-in-loop:30::'continue' not properly in loop
+not-in-loop:35::'break' not properly in loop
+not-in-loop:38::'continue' not properly in loop
+not-in-loop:40::'break' not properly in loop \ No newline at end of file
diff --git a/pylint/test/input/func_continue_not_in_loop.py b/pylint/test/input/func_continue_not_in_loop.py
deleted file mode 100644
index bdde320..0000000
--- a/pylint/test/input/func_continue_not_in_loop.py
+++ /dev/null
@@ -1,14 +0,0 @@
-"""this module produces a SyntaxError at execution time"""
-# pylint: disable=using-constant-test
-__revision__ = None
-
-def run():
- """simple function"""
- if True:
- continue
- else:
- break
-
-if __name__ == '__main__':
- run()
-
diff --git a/pylint/test/messages/func_continue_not_in_loop.txt b/pylint/test/messages/func_continue_not_in_loop.txt
deleted file mode 100644
index d3a3183..0000000
--- a/pylint/test/messages/func_continue_not_in_loop.txt
+++ /dev/null
@@ -1,2 +0,0 @@
-E: 8:run: 'continue' not properly in loop
-E: 10:run: 'break' not properly in loop