Commits

Georg Brandl  committed 9815c04

Check for "return SomeError(...)" instead of "raise ...".

  • Participants
  • Parent commits c6bf50f
  • Branches experimental

Comments (0)

Files changed (2)

File pyflakes/checker.py

         pass
 
     # "stmt" type nodes
-    RETURN = DELETE = PRINT = WHILE = IF = WITH = RAISE = TRYEXCEPT = \
+    DELETE = PRINT = WHILE = IF = WITH = RAISE = TRYEXCEPT = \
     TRYFINALLY = ASSERT = EXEC = EXPR = handleChildren
     CONTINUE = BREAK = PASS = ignore
     # "expr" type nodes
             if node.module == '__future__':
                 importation.used = (self.scope, node.lineno)
             self.addBinding(node.lineno, importation)
+
+    def RETURN(self, node):
+        if not node.value:
+            return
+        self.handleNode(node.value, node)
+        if isinstance(node.value, _ast.Name):
+            name = node.value.id
+        elif isinstance(node.value, _ast.Call) and \
+           isinstance(node.value.func, _ast.Name):
+            name = node.value.func.id
+        else:
+            return
+        if name.endswith('Error') or name.endswith('Exception'):
+            self.report(messages.ExceptionReturn, node.lineno, name)

File pyflakes/messages.py

     def __init__(self, filename, lineno, msg):
         Message.__init__(self, filename, lineno)
         self.message_args = (msg,)
+
+
+class ExceptionReturn(Message):
+    """
+    Indicates that an Error or Exception is returned instead of raised.
+    """
+
+    message = 'exception %r is returned'
+    def __init__(self, filename, lineno, name):
+        Message.__init__(self, filename, lineno)
+        self.message_args = (name,)