Merge branch 'lint-followup' into 'main'

Improve maint/add_warning

Closes #494

See merge request tpo/core/arti!560
This commit is contained in:
Nick Mathewson 2022-06-01 10:04:50 +00:00
commit 43028b94a5
1 changed files with 34 additions and 16 deletions

View File

@ -1,5 +1,6 @@
#!/usr/bin/python3
import argparse
import sys
import os
import re
@ -66,15 +67,23 @@ DECIDED_NOT = """
PAT = re.compile(r'^#!\[(allow|deny|warn)')
opt_check = False
opts = None
deferred_errors = []
class ImproperFile(Exception):
def __init__(self, lno, message):
self.lno = lno
self.message = message
def filter_file(lints, inp, outp):
in_lint_list = False
found_lint_list = False
lno = 0
for line in inp.readlines():
lno += 1
if line.startswith("// @@ begin lint list"):
assert not in_lint_list
if in_lint_list:
raise ImproperFile(lno, 'found "@@ begin lint list" but inside lint list')
found_lint_list = True
in_lint_list = True
elif line.startswith("//! <!-- @@ end lint list"):
@ -82,27 +91,39 @@ def filter_file(lints, inp, outp):
# *really really* hates comments that come after things.
# Finishing the automaintained block with just a blank line is too much of a hazard.
# It does end up in the output HTML from Rustdoc, but it is harmless there.
assert in_lint_list
if not in_lint_list:
raise ImproperFile(lno, 'found "@@ end lint list" but not inside lint list')
in_lint_list = False
outp.write(WANT_LINTS.strip())
outp.write("\n")
elif in_lint_list:
assert PAT.match(line)
if not PAT.match(line):
raise ImproperFile(lno, 'entry in lint list does not look like a lint')
# do not send to output
continue
outp.write(line)
assert not in_lint_list
assert found_lint_list
if in_lint_list:
raise ImproperFile(lno, 'missing "@@ lint list" delimiter, still in lint list at EOF')
if not found_lint_list:
raise ImproperFile(lno, 'standard lint list block seems to be missing (wrong delimiters?)')
def process(lints, fn):
print("{}...".format(fn))
tmp_name = fn+".tmp~"
outp = open(tmp_name,'w')
inp = open(fn,'r')
filter_file(lints, inp, outp)
try:
filter_file(lints, inp, outp)
except ImproperFile as e:
print('%s:%d: %s' % (fn, e.lno, e.message), file=sys.stderr)
deferred_errors.append(fn)
os.remove(tmp_name) # this tmp file is probably partial
return
inp.close()
outp.close()
if opt_check:
if opts.check:
if subprocess.run(['diff', '-u', '--', fn, tmp_name]).returncode != 0:
deferred_errors.append(fn)
else:
@ -127,12 +148,9 @@ def main(lints,files):
sys.exit(1)
if __name__ == '__main__':
args = sys.argv[1:]
while len(args) > 0 and args[0].startswith('-'):
arg = args.pop(0)
if arg == '--check':
opt_check = True
else:
raise(Exception('unknown option '+arg))
parser = argparse.ArgumentParser('standardise Rust lint blocks')
parser.add_argument('--check')
parser.add_argument('file', nargs='+')
opts = parser.parse_args()
main(WANT_LINTS, args)
main(WANT_LINTS, opts.file)