@@ -281,7 +281,7 @@ def maximum_line_length(physical_line, max_line_length, multiline, noqa):
281281 if ((len (chunks ) == 1 and multiline ) or
282282 (len (chunks ) == 2 and chunks [0 ] == '#' )) and \
283283 len (line ) - len (chunks [- 1 ]) < max_line_length - 7 :
284- return
284+ return None
285285 if hasattr (line , 'decode' ): # Python 2
286286 # The line could contain multi-byte characters
287287 try :
@@ -495,6 +495,58 @@ def indentation(logical_line, previous_logical, indent_char,
495495 yield 0 , tmpl % (3 + c , "unexpected indentation" )
496496
497497
498+ @register_check
499+ def returns (logical_line , indent_level , previous_logical , checker_state ):
500+ r"""Be consistent in return statements.
501+
502+ Either all return statements in a function should return an expression, or
503+ none of them should. If any return statement returns an expression, any
504+ return statements where no value is returned should explicitly state this
505+ as return None, [and an explicit return statement should be present at the
506+ end of the function (if reachable).]
507+
508+ The reachability constraint is not implemented due to its complexity.
509+
510+ Okay: def a():\n return 1
511+ Okay: def a():\n return 1\n return 2
512+ Okay: def a():\n return
513+ Okay: def a():\n return\n return
514+ Okay: def a():\n def b():\n return\n return b
515+ Okay: def a():\n def b():\n return 2\n return
516+
517+ E750: def a():\n return\n return 2
518+ E750: def a():\n return 4\n return
519+ """
520+ functions_stack = checker_state .setdefault ('functions_stack' , [])
521+ # a stack of functions, containing:
522+ # indent_level, return_without_value, return_with_value
523+ INDENT , RETURN_NO_VALUE , RETURN_VALUE = 0 , 1 , 2
524+ if STARTSWITH_DEF_REGEX .match (previous_logical ):
525+ functions_stack .append ([indent_level , False , False ])
526+
527+ if functions_stack and indent_level < functions_stack [- 1 ][INDENT ]:
528+ functions_stack .pop ()
529+
530+ if logical_line .startswith ('return' ):
531+ try :
532+ last_fun_record = functions_stack [- 1 ]
533+ except IndexError :
534+ # ignore return statements outside of functions (this happens in
535+ # pycodestyle unit tests only)
536+ return
537+
538+ if logical_line == 'return' :
539+ if last_fun_record [RETURN_VALUE ]:
540+ yield 0 , "E750 'return' without expression used in the same " \
541+ "method as 'return' with expression"
542+ last_fun_record [RETURN_NO_VALUE ] = True
543+ else :
544+ if last_fun_record [RETURN_NO_VALUE ]:
545+ yield 0 , "E750 'return' with expression used in the same " \
546+ "method as 'return' without expression"
547+ last_fun_record [RETURN_VALUE ] = True
548+
549+
498550@register_check
499551def continued_indentation (logical_line , tokens , indent_level , hang_closing ,
500552 indent_char , noqa , verbose ):
@@ -1910,15 +1962,15 @@ def error(self, line_number, offset, text, check):
19101962 """Report an error, according to options."""
19111963 code = text [:4 ]
19121964 if self ._ignore_code (code ):
1913- return
1965+ return None
19141966 if code in self .counters :
19151967 self .counters [code ] += 1
19161968 else :
19171969 self .counters [code ] = 1
19181970 self .messages [code ] = text [5 :]
19191971 # Don't care about expected errors or warnings
19201972 if code in self .expected :
1921- return
1973+ return None
19221974 if self .print_filename and not self .file_errors :
19231975 print (self .filename )
19241976 self .file_errors += 1
@@ -2030,7 +2082,7 @@ def __init__(self, options):
20302082
20312083 def error (self , line_number , offset , text , check ):
20322084 if line_number not in self ._selected [self .filename ]:
2033- return
2085+ return None
20342086 return super (DiffReport , self ).error (line_number , offset , text , check )
20352087
20362088
0 commit comments