@@ -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,53 @@ 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 functions_stack :
531
+ if logical_line == 'return' :
532
+ last_fun_record = functions_stack [- 1 ]
533
+ if last_fun_record [RETURN_VALUE ]:
534
+ yield 0 , "E750 'return' without expression used in the same " \
535
+ "method as 'return' with expression"
536
+ last_fun_record [RETURN_NO_VALUE ] = True
537
+ elif logical_line .startswith ('return' ):
538
+ last_fun_record = functions_stack [- 1 ]
539
+ if last_fun_record [RETURN_NO_VALUE ]:
540
+ yield 0 , "E750 'return' with expression used in the same " \
541
+ "method as 'return' without expression"
542
+ last_fun_record [RETURN_VALUE ] = True
543
+
544
+
498
545
@register_check
499
546
def continued_indentation (logical_line , tokens , indent_level , hang_closing ,
500
547
indent_char , noqa , verbose ):
@@ -1910,15 +1957,15 @@ def error(self, line_number, offset, text, check):
1910
1957
"""Report an error, according to options."""
1911
1958
code = text [:4 ]
1912
1959
if self ._ignore_code (code ):
1913
- return
1960
+ return None
1914
1961
if code in self .counters :
1915
1962
self .counters [code ] += 1
1916
1963
else :
1917
1964
self .counters [code ] = 1
1918
1965
self .messages [code ] = text [5 :]
1919
1966
# Don't care about expected errors or warnings
1920
1967
if code in self .expected :
1921
- return
1968
+ return None
1922
1969
if self .print_filename and not self .file_errors :
1923
1970
print (self .filename )
1924
1971
self .file_errors += 1
@@ -2030,7 +2077,7 @@ def __init__(self, options):
2030
2077
2031
2078
def error (self , line_number , offset , text , check ):
2032
2079
if line_number not in self ._selected [self .filename ]:
2033
- return
2080
+ return None
2034
2081
return super (DiffReport , self ).error (line_number , offset , text , check )
2035
2082
2036
2083
0 commit comments