# HG changeset patch # User Augie Fackler # Date 2017-09-29 15:55:44 # Node ID b52f22d9afa5a8abd4acd6aa27f8c2e413d087c1 # Parent ab687e06fe022784643064c163fbf2038961c7ce contrib: add a check to check-code to ban superfluous pass statements These have annoyed me for a long time, and I'm tired of commenting on them in reviews. I'm sorry for how complicated the regular expression is, but I was too lazy to go crack open pylint's code and add the check there. diff --git a/contrib/check-code.py b/contrib/check-code.py --- a/contrib/check-code.py +++ b/contrib/check-code.py @@ -257,6 +257,16 @@ pypats = [ (r'(\w|\))[+/*\-<>]\w', "missing whitespace in expression"), (r'^\s+(\w|\.)+=\w[^,()\n]*$', "missing whitespace in assignment"), (r'\w\s=\s\s+\w', "gratuitous whitespace after ="), + (( + # a line ending with a colon, potentially with trailing comments + r':([ \t]*#[^\n]*)?\n' + # one that is not a pass and not only a comment + r'(?P[ \t]+)[^#][^\n]+\n' + # more lines at the same indent level + r'((?P=indent)[^\n]+\n)*' + # a pass at the same indent level, which is bogus + r'(?P=indent)pass[ \t\n#]' + ), 'omit superfluous pass'), (r'.{81}', "line too long"), (r'[^\n]\Z', "no trailing newline"), (r'(\S[ \t]+|^[ \t]+)\n', "trailing whitespace"), diff --git a/tests/test-contrib-check-code.t b/tests/test-contrib-check-code.t --- a/tests/test-contrib-check-code.t +++ b/tests/test-contrib-check-code.t @@ -285,11 +285,43 @@ web templates > ''' "%-6d \n 123456 .:*+-= foobar") > EOF +superfluous pass + + $ cat > superfluous_pass.py < # correct examples + > if foo: + > pass + > else: + > # comment-only line means still need pass + > pass + > def nothing(): + > pass + > class empty(object): + > pass + > if whatever: + > passvalue(value) + > # bad examples + > if foo: + > "foo" + > pass + > else: # trailing comment doesn't fool checker + > wat() + > pass + > def nothing(): + > "docstring means no pass" + > pass + > class empty(object): + > """multiline + > docstring also + > means no pass""" + > pass + > EOF + (Checking multiple invalid files at once examines whether caching translation table for repquote() works as expected or not. All files should break rules depending on result of repquote(), in this case) - $ "$check_code" stringjoin.py uigettext.py + $ "$check_code" stringjoin.py uigettext.py superfluous_pass.py stringjoin.py:1: > foo = (' foo' string join across lines with no space @@ -317,4 +349,16 @@ should break rules depending on result o uigettext.py:1: > ui.status("% 10s %05d % -3.2f %*s %%" missing _() in ui message (use () to hide false-positives) + superfluous_pass.py:14: + > if foo: + omit superfluous pass + superfluous_pass.py:17: + > else: # trailing comment doesn't fool checker + omit superfluous pass + superfluous_pass.py:20: + > def nothing(): + omit superfluous pass + superfluous_pass.py:23: + > class empty(object): + omit superfluous pass [1]