update script: also generate lint list in lib.rs

This commit is contained in:
Georg Brandl 2015-08-13 11:31:09 +02:00
parent 2c2716f045
commit ea0cf2a296
3 changed files with 146 additions and 115 deletions

View File

@ -64,39 +64,40 @@ pub fn plugin_registrar(reg: &mut Registry) {
reg.register_lint_pass(box loops::LoopsPass as LintPassObject);
reg.register_lint_pass(box lifetimes::LifetimePass as LintPassObject);
reg.register_lint_group("clippy", vec![types::BOX_VEC,
types::LINKEDLIST,
types::LET_UNIT_VALUE,
misc::SINGLE_MATCH,
misc::TOPLEVEL_REF_ARG,
misc::CMP_NAN,
misc::FLOAT_CMP,
misc::PRECEDENCE,
misc::CMP_OWNED,
eq_op::EQ_OP,
bit_mask::BAD_BIT_MASK,
bit_mask::INEFFECTIVE_BIT_MASK,
ptr_arg::PTR_ARG,
needless_bool::NEEDLESS_BOOL,
approx_const::APPROX_CONSTANT,
eta_reduction::REDUNDANT_CLOSURE,
identity_op::IDENTITY_OP,
mut_mut::MUT_MUT,
len_zero::LEN_ZERO,
len_zero::LEN_WITHOUT_IS_EMPTY,
attrs::INLINE_ALWAYS,
collapsible_if::COLLAPSIBLE_IF,
unicode::ZERO_WIDTH_SPACE,
unicode::NON_ASCII_LITERAL,
strings::STRING_ADD_ASSIGN,
returns::NEEDLESS_RETURN,
returns::LET_AND_RETURN,
misc::MODULO_ONE,
methods::OPTION_UNWRAP_USED,
methods::RESULT_UNWRAP_USED,
methods::STR_TO_STRING,
methods::STRING_TO_STRING,
lifetimes::NEEDLESS_LIFETIMES,
loops::NEEDLESS_RANGE_LOOP,
]);
reg.register_lint_group("clippy", vec![
approx_const::APPROX_CONSTANT,
attrs::INLINE_ALWAYS,
bit_mask::BAD_BIT_MASK,
bit_mask::INEFFECTIVE_BIT_MASK,
collapsible_if::COLLAPSIBLE_IF,
eq_op::EQ_OP,
eta_reduction::REDUNDANT_CLOSURE,
identity_op::IDENTITY_OP,
len_zero::LEN_WITHOUT_IS_EMPTY,
len_zero::LEN_ZERO,
lifetimes::NEEDLESS_LIFETIMES,
loops::NEEDLESS_RANGE_LOOP,
methods::OPTION_UNWRAP_USED,
methods::RESULT_UNWRAP_USED,
methods::STR_TO_STRING,
methods::STRING_TO_STRING,
misc::CMP_NAN,
misc::CMP_OWNED,
misc::FLOAT_CMP,
misc::MODULO_ONE,
misc::PRECEDENCE,
misc::SINGLE_MATCH,
misc::TOPLEVEL_REF_ARG,
mut_mut::MUT_MUT,
needless_bool::NEEDLESS_BOOL,
ptr_arg::PTR_ARG,
returns::LET_AND_RETURN,
returns::NEEDLESS_RETURN,
strings::STRING_ADD_ASSIGN,
types::BOX_VEC,
types::LET_UNIT_VALUE,
types::LINKEDLIST,
unicode::NON_ASCII_LITERAL,
unicode::ZERO_WIDTH_SPACE,
]);
}

110
util/update_lints.py Normal file
View File

@ -0,0 +1,110 @@
# Generate a Markdown table of all lints, and put it in README.md.
# With -n option, only print the new table to stdout.
import os
import re
import sys
declare_lint_re = re.compile(r'''
declare_lint! \s* [{(] \s*
pub \s+ (?P<name>[A-Z_]+) \s*,\s*
(?P<level>Forbid|Deny|Warn|Allow) \s*,\s*
" (?P<desc>(?:[^"\\]+|\\.)*) " \s* [})]
''', re.X | re.S)
nl_escape_re = re.compile(r'\\\n\s*')
def collect(lints, fn):
"""Collect all lints from a file.
Adds entries to the lints list as `(module, name, level, desc)`.
"""
with open(fn) as fp:
code = fp.read()
for match in declare_lint_re.finditer(code):
# remove \-newline escapes from description string
desc = nl_escape_re.sub('', match.group('desc'))
lints.append((os.path.splitext(os.path.basename(fn))[0],
match.group('name').lower(),
match.group('level').lower(),
desc.replace('\\"', '"')))
def write_tbl(lints, fp):
"""Write lint table in Markdown format."""
# first and third column widths
w_name = max(len(l[1]) for l in lints)
w_desc = max(len(l[3]) for l in lints)
# header and underline
fp.write('%-*s | default | meaning\n' % (w_name, 'name'))
fp.write('%s-|-%s-|-%s\n' % ('-' * w_name, '-' * 7, '-' * w_desc))
# one table row per lint
for (_, name, default, meaning) in sorted(lints, key=lambda l: l[1]):
fp.write('%-*s | %-7s | %s\n' % (w_name, name, default, meaning))
def write_group(lints, fp):
"""Write lint group (list of all lints in the form module::NAME)."""
for (module, name, _, _) in sorted(lints):
fp.write(' %s::%s,\n' % (module, name.upper()))
def replace_region(fn, region_start, region_end, callback,
replace_start=True):
"""Replace a region in a file delimited by two lines matching regexes.
A callback is called to write the new region. If `replace_start` is true,
the start delimiter line is replaced as well. The end delimiter line is
never replaced.
"""
# read current content
with open(fn) as fp:
lines = list(fp)
# replace old region with new region
with open(fn, 'w') as fp:
in_old_region = False
for line in lines:
if in_old_region:
if re.search(region_end, line):
in_old_region = False
fp.write(line)
elif re.search(region_start, line):
if not replace_start:
fp.write(line)
# old region starts here
in_old_region = True
callback(fp)
else:
fp.write(line)
def main(print_only=False):
lints = []
# check directory
if not os.path.isfile('src/lib.rs'):
print('Error: call this script from clippy checkout directory!')
return
# collect all lints from source files
for root, dirs, files in os.walk('src'):
for fn in files:
if fn.endswith('.rs'):
collect(lints, os.path.join(root, fn))
if print_only:
write_tbl(lints, sys.stdout)
return
# replace table in README.md
replace_region('README.md', r'^name +\|', '^$', lambda fp: write_tbl(lints, fp))
# same for "clippy" lint collection
replace_region('src/lib.rs', r'reg.register_lint_group\("clippy"', r'\]\);',
lambda fp: write_group(lints, fp), replace_start=False)
if __name__ == '__main__':
main(print_only='-n' in sys.argv)

View File

@ -1,80 +0,0 @@
# Generate a Markdown table of all lints, and put it in README.md.
# With -n option, only print the new table to stdout.
import os
import re
import sys
declare_lint_re = re.compile(r'''
declare_lint! \s* [{(] \s*
pub \s+ (?P<name>[A-Z_]+) \s*,\s*
(?P<level>Forbid|Deny|Warn|Allow) \s*,\s*
" (?P<desc>(?:[^"\\]+|\\.)*) " \s* [})]
''', re.X | re.S)
nl_escape_re = re.compile(r'\\\n\s*')
def collect(lints, fp):
code = fp.read()
for match in declare_lint_re.finditer(code):
# remove \-newline escapes from description string
desc = nl_escape_re.sub('', match.group('desc'))
lints.append((match.group('name').lower(),
match.group('level').lower(),
desc.replace('\\"', '"')))
def write_tbl(lints, fp):
# first and third column widths
w_name = max(len(l[0]) for l in lints)
w_desc = max(len(l[2]) for l in lints)
# header and underline
fp.write('%-*s | default | meaning\n' % (w_name, 'name'))
fp.write('%s-|-%s-|-%s\n' % ('-' * w_name, '-' * 7, '-' * w_desc))
# one table row per lint
for (name, default, meaning) in sorted(lints):
fp.write('%-*s | %-7s | %s\n' % (w_name, name, default, meaning))
def main(print_only=False):
lints = []
# check directory
if not os.path.isfile('src/lib.rs'):
print('Error: call this script from clippy checkout directory!')
return
# collect all lints from source files
for root, dirs, files in os.walk('src'):
for fn in files:
if fn.endswith('.rs'):
with open(os.path.join(root, fn)) as fp:
collect(lints, fp)
if print_only:
write_tbl(lints, sys.stdout)
return
# read current README.md content
with open('README.md') as fp:
lines = list(fp)
# replace old table with new table
with open('README.md', 'w') as fp:
in_old_tbl = False
for line in lines:
if line.replace(' ', '').strip() == 'name|default|meaning':
# old table starts here
write_tbl(lints, fp)
in_old_tbl = True
if in_old_tbl:
# the old table is finished by an empty line
if line.strip():
continue
in_old_tbl = False
fp.write(line)
if __name__ == '__main__':
main(print_only='-n' in sys.argv)