@@ -281,7 +281,7 @@ def maximum_line_length(physical_line, max_line_length, multiline, noqa):
281
281
if ((len (chunks ) == 1 and multiline ) or
282
282
(len (chunks ) == 2 and chunks [0 ] == '#' )) and \
283
283
len (line ) - len (chunks [- 1 ]) < max_line_length - 7 :
284
- return
284
+ return None
285
285
if hasattr (line , 'decode' ): # Python 2
286
286
# The line could contain multi-byte characters
287
287
try :
@@ -495,6 +495,58 @@ def indentation(logical_line, previous_logical, indent_char,
495
495
yield 0 , tmpl % (3 + c , "unexpected indentation" )
496
496
497
497
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
+
498
550
@register_check
499
551
def continued_indentation (logical_line , tokens , indent_level , hang_closing ,
500
552
indent_char , noqa , verbose ):
@@ -1910,15 +1962,15 @@ def error(self, line_number, offset, text, check):
1910
1962
"""Report an error, according to options."""
1911
1963
code = text [:4 ]
1912
1964
if self ._ignore_code (code ):
1913
- return
1965
+ return None
1914
1966
if code in self .counters :
1915
1967
self .counters [code ] += 1
1916
1968
else :
1917
1969
self .counters [code ] = 1
1918
1970
self .messages [code ] = text [5 :]
1919
1971
# Don't care about expected errors or warnings
1920
1972
if code in self .expected :
1921
- return
1973
+ return None
1922
1974
if self .print_filename and not self .file_errors :
1923
1975
print (self .filename )
1924
1976
self .file_errors += 1
@@ -2030,7 +2082,7 @@ def __init__(self, options):
2030
2082
2031
2083
def error (self , line_number , offset , text , check ):
2032
2084
if line_number not in self ._selected [self .filename ]:
2033
- return
2085
+ return None
2034
2086
return super (DiffReport , self ).error (line_number , offset , text , check )
2035
2087
2036
2088
0 commit comments