summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdi Roiban <adi.roiban@chevah.com>2015-02-14 11:40:18 +0000
committerAdi Roiban <adi.roiban@chevah.com>2015-02-14 11:40:18 +0000
commit4620866430463084421e3b9e7b858b894c6734ac (patch)
tree79a507936df9a53a01a49f5ea9468f27684e47af
parent8dd58c955cda639ae9155fd42b3c69eca47f5a6c (diff)
downloadpyflakes-4620866430463084421e3b9e7b858b894c6734ac.tar.gz
Add an error for return outside of function.
-rw-r--r--pyflakes/checker.py4
-rw-r--r--pyflakes/messages.py7
-rw-r--r--pyflakes/test/test_other.py9
3 files changed, 20 insertions, 0 deletions
diff --git a/pyflakes/checker.py b/pyflakes/checker.py
index 6d99310..2c192af 100644
--- a/pyflakes/checker.py
+++ b/pyflakes/checker.py
@@ -693,6 +693,10 @@ class Checker(object):
raise RuntimeError("Got impossible expression context: %r" % (node.ctx,))
def RETURN(self, node):
+ if isinstance(self.scope, ClassScope):
+ self.report(messages.ReturnOutsideFunction, node)
+ return
+
if (
node.value and
hasattr(self.scope, 'returnValue') and
diff --git a/pyflakes/messages.py b/pyflakes/messages.py
index 1f799ec..71663a1 100644
--- a/pyflakes/messages.py
+++ b/pyflakes/messages.py
@@ -133,3 +133,10 @@ class ReturnWithArgsInsideGenerator(Message):
Indicates a return statement with arguments inside a generator.
"""
message = '\'return\' with argument inside generator'
+
+
+class ReturnOutsideFunction(Message):
+ """
+ Indicates a return statement outside of a function/method.
+ """
+ message = '\'return\' outside function'
diff --git a/pyflakes/test/test_other.py b/pyflakes/test/test_other.py
index f24a721..966c723 100644
--- a/pyflakes/test/test_other.py
+++ b/pyflakes/test/test_other.py
@@ -340,6 +340,15 @@ class Test(TestCase):
pass
''', m.RedefinedWhileUnused)
+ def test_classWithReturn(self):
+ """
+ If a return is used inside a class, a warning is emitted.
+ """
+ self.flakes('''
+ class Foo(object):
+ return
+ ''', m.ReturnOutsideFunction)
+
@skip("todo: Too hard to make this warn but other cases stay silent")
def test_doubleAssignment(self):
"""