2015-08-13 10:32:35 +02:00
|
|
|
#!/usr/bin/env python
|
2015-08-13 11:31:09 +02:00
|
|
|
# Generate a Markdown table of all lints, and put it in README.md.
|
|
|
|
# With -n option, only print the new table to stdout.
|
2016-01-29 22:24:08 +01:00
|
|
|
# With -c option, print a warning and set exit status to 1 if a file would be
|
|
|
|
# changed.
|
2015-08-13 11:31:09 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
2016-03-24 19:25:59 +01:00
|
|
|
declare_deprecated_lint_re = re.compile(r'''
|
|
|
|
declare_deprecated_lint! \s* [{(] \s*
|
|
|
|
pub \s+ (?P<name>[A-Z_][A-Z_0-9]*) \s*,\s*
|
|
|
|
" (?P<desc>(?:[^"\\]+|\\.)*) " \s* [})]
|
|
|
|
''', re.VERBOSE | re.DOTALL)
|
2015-08-13 11:31:09 +02:00
|
|
|
|
2018-03-27 17:13:55 +02:00
|
|
|
declare_clippy_lint_re = re.compile(r'''
|
|
|
|
declare_clippy_lint! \s* [{(] \s*
|
|
|
|
pub \s+ (?P<name>[A-Z_][A-Z_0-9]*) \s*,\s*
|
|
|
|
(?P<cat>[a-z_]+) \s*,\s*
|
|
|
|
" (?P<desc>(?:[^"\\]+|\\.)*) " \s* [})]
|
|
|
|
''', re.VERBOSE | re.DOTALL)
|
|
|
|
|
2015-08-13 11:31:09 +02:00
|
|
|
nl_escape_re = re.compile(r'\\\n\s*')
|
|
|
|
|
2017-09-01 10:29:49 +02:00
|
|
|
docs_link = 'https://rust-lang-nursery.github.io/rust-clippy/master/index.html'
|
2015-08-13 11:31:09 +02:00
|
|
|
|
2016-01-29 22:24:08 +01:00
|
|
|
|
2018-03-28 23:49:32 +02:00
|
|
|
def collect(deprecated_lints, clippy_lints, fn):
|
2015-08-13 11:31:09 +02:00
|
|
|
"""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()
|
|
|
|
|
2016-03-24 19:25:59 +01:00
|
|
|
for match in declare_deprecated_lint_re.finditer(code):
|
|
|
|
# remove \-newline escapes from description string
|
|
|
|
desc = nl_escape_re.sub('', match.group('desc'))
|
|
|
|
deprecated_lints.append((os.path.splitext(os.path.basename(fn))[0],
|
|
|
|
match.group('name').lower(),
|
|
|
|
desc.replace('\\"', '"')))
|
2018-06-14 22:50:07 +02:00
|
|
|
|
2018-03-27 17:13:55 +02:00
|
|
|
for match in declare_clippy_lint_re.finditer(code):
|
|
|
|
# remove \-newline escapes from description string
|
|
|
|
desc = nl_escape_re.sub('', match.group('desc'))
|
|
|
|
cat = match.group('cat')
|
|
|
|
clippy_lints[cat].append((os.path.splitext(os.path.basename(fn))[0],
|
|
|
|
match.group('name').lower(),
|
|
|
|
"allow",
|
|
|
|
desc.replace('\\"', '"')))
|
2016-05-24 18:25:25 +02:00
|
|
|
|
2015-08-13 11:31:09 +02:00
|
|
|
|
2018-06-14 22:50:07 +02:00
|
|
|
def gen_group(lints):
|
2015-08-13 11:31:09 +02:00
|
|
|
"""Write lint group (list of all lints in the form module::NAME)."""
|
|
|
|
for (module, name, _, _) in sorted(lints):
|
2015-08-13 10:32:35 +02:00
|
|
|
yield ' %s::%s,\n' % (module, name.upper())
|
2015-08-13 11:31:09 +02:00
|
|
|
|
|
|
|
|
2016-02-20 17:00:36 +01:00
|
|
|
def gen_mods(lints):
|
|
|
|
"""Declare modules"""
|
|
|
|
|
|
|
|
for module in sorted(set(lint[0] for lint in lints)):
|
|
|
|
yield 'pub mod %s;\n' % module
|
|
|
|
|
|
|
|
|
2016-03-24 19:25:59 +01:00
|
|
|
def gen_deprecated(lints):
|
|
|
|
"""Declare deprecated lints"""
|
|
|
|
|
|
|
|
for lint in lints:
|
2016-12-21 13:12:17 +01:00
|
|
|
yield ' store.register_removed(\n'
|
|
|
|
yield ' "%s",\n' % lint[1]
|
|
|
|
yield ' "%s",\n' % lint[2]
|
|
|
|
yield ' );\n'
|
2016-03-24 19:25:59 +01:00
|
|
|
|
2016-05-27 15:57:03 +02:00
|
|
|
|
2015-08-13 11:31:09 +02:00
|
|
|
def replace_region(fn, region_start, region_end, callback,
|
2015-08-13 10:32:35 +02:00
|
|
|
replace_start=True, write_back=True):
|
2015-08-13 11:31:09 +02:00
|
|
|
"""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)
|
|
|
|
|
2018-03-28 23:49:32 +02:00
|
|
|
found = False
|
|
|
|
|
2015-08-13 11:31:09 +02:00
|
|
|
# replace old region with new region
|
2015-08-13 10:32:35 +02:00
|
|
|
new_lines = []
|
|
|
|
in_old_region = False
|
|
|
|
for line in lines:
|
|
|
|
if in_old_region:
|
|
|
|
if re.search(region_end, line):
|
|
|
|
in_old_region = False
|
|
|
|
new_lines.extend(callback())
|
|
|
|
new_lines.append(line)
|
|
|
|
elif re.search(region_start, line):
|
|
|
|
if not replace_start:
|
|
|
|
new_lines.append(line)
|
|
|
|
# old region starts here
|
|
|
|
in_old_region = True
|
2018-03-28 23:49:32 +02:00
|
|
|
found = True
|
2015-08-13 10:32:35 +02:00
|
|
|
else:
|
|
|
|
new_lines.append(line)
|
|
|
|
|
2018-03-28 23:49:32 +02:00
|
|
|
if not found:
|
2018-08-12 11:33:44 +02:00
|
|
|
print("regex " + region_start + " not found")
|
2018-03-28 23:49:32 +02:00
|
|
|
|
2015-08-13 10:32:35 +02:00
|
|
|
# write back to file
|
|
|
|
if write_back:
|
|
|
|
with open(fn, 'w') as fp:
|
|
|
|
fp.writelines(new_lines)
|
|
|
|
|
|
|
|
# if something changed, return true
|
|
|
|
return lines != new_lines
|
|
|
|
|
|
|
|
|
|
|
|
def main(print_only=False, check=False):
|
2016-03-24 19:25:59 +01:00
|
|
|
deprecated_lints = []
|
2018-03-27 17:13:55 +02:00
|
|
|
clippy_lints = {
|
|
|
|
"correctness": [],
|
|
|
|
"style": [],
|
|
|
|
"complexity": [],
|
|
|
|
"perf": [],
|
2018-03-28 15:24:26 +02:00
|
|
|
"restriction": [],
|
|
|
|
"pedantic": [],
|
2018-04-30 01:20:39 +02:00
|
|
|
"cargo": [],
|
2018-03-28 15:24:26 +02:00
|
|
|
"nursery": [],
|
2018-03-27 17:13:55 +02:00
|
|
|
}
|
2015-08-13 11:31:09 +02:00
|
|
|
|
|
|
|
# check directory
|
2016-05-24 18:25:25 +02:00
|
|
|
if not os.path.isfile('clippy_lints/src/lib.rs'):
|
2015-08-13 11:31:09 +02:00
|
|
|
print('Error: call this script from clippy checkout directory!')
|
|
|
|
return
|
|
|
|
|
|
|
|
# collect all lints from source files
|
2016-07-18 11:19:33 +02:00
|
|
|
for fn in os.listdir('clippy_lints/src'):
|
|
|
|
if fn.endswith('.rs'):
|
2018-03-28 23:49:32 +02:00
|
|
|
collect(deprecated_lints, clippy_lints,
|
2016-07-18 11:19:33 +02:00
|
|
|
os.path.join('clippy_lints', 'src', fn))
|
2015-08-13 11:31:09 +02:00
|
|
|
|
2016-05-27 15:57:44 +02:00
|
|
|
# determine version
|
|
|
|
with open('Cargo.toml') as fp:
|
|
|
|
for line in fp:
|
|
|
|
if line.startswith('version ='):
|
|
|
|
clippy_version = line.split()[2].strip('"')
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
print('Error: version not found in Cargo.toml!')
|
|
|
|
return
|
|
|
|
|
2018-03-28 23:49:32 +02:00
|
|
|
all_lints = []
|
|
|
|
clippy_lint_groups = [
|
|
|
|
"correctness",
|
|
|
|
"style",
|
|
|
|
"complexity",
|
|
|
|
"perf",
|
|
|
|
]
|
|
|
|
clippy_lint_list = []
|
|
|
|
for x in clippy_lint_groups:
|
|
|
|
clippy_lint_list += clippy_lints[x]
|
2018-03-28 15:24:26 +02:00
|
|
|
for _, value in clippy_lints.iteritems():
|
|
|
|
all_lints += value
|
2018-03-27 17:13:55 +02:00
|
|
|
|
2015-08-13 11:31:09 +02:00
|
|
|
if print_only:
|
2018-06-14 22:50:07 +02:00
|
|
|
print_clippy_lint_groups = [
|
|
|
|
"correctness",
|
|
|
|
"style",
|
|
|
|
"complexity",
|
|
|
|
"perf",
|
|
|
|
"pedantic",
|
|
|
|
"nursery",
|
|
|
|
"restriction"
|
|
|
|
]
|
|
|
|
for group in print_clippy_lint_groups:
|
|
|
|
sys.stdout.write('\n## ' + group + '\n')
|
2018-06-15 17:45:41 +02:00
|
|
|
for (_, name, _, descr) in sorted(clippy_lints[group]):
|
2018-06-14 22:50:07 +02:00
|
|
|
sys.stdout.write('* [' + name + '](https://rust-lang-nursery.github.io/rust-clippy/master/index.html#' + name + ') (' + descr + ')\n')
|
2015-08-13 11:31:09 +02:00
|
|
|
return
|
|
|
|
|
2017-08-31 15:38:24 +02:00
|
|
|
# update the lint counter in README.md
|
2015-09-01 17:53:56 +02:00
|
|
|
changed = replace_region(
|
|
|
|
'README.md',
|
2018-03-28 23:49:32 +02:00
|
|
|
r'^\[There are \d+ lints included in this crate!\]\(https://rust-lang-nursery.github.io/rust-clippy/master/index.html\)$', "",
|
|
|
|
lambda: ['[There are %d lints included in this crate!](https://rust-lang-nursery.github.io/rust-clippy/master/index.html)\n' %
|
2018-03-27 17:13:55 +02:00
|
|
|
(len(all_lints))],
|
2015-08-24 16:23:05 +02:00
|
|
|
write_back=not check)
|
|
|
|
|
2016-04-14 17:56:46 +02:00
|
|
|
# update the links in the CHANGELOG
|
|
|
|
changed |= replace_region(
|
|
|
|
'CHANGELOG.md',
|
2018-07-23 07:29:53 +02:00
|
|
|
"<!-- begin autogenerated links to lint list -->",
|
|
|
|
"<!-- end autogenerated links to lint list -->",
|
2017-09-01 10:29:49 +02:00
|
|
|
lambda: ["[`{0}`]: {1}#{0}\n".format(l[1], docs_link) for l in
|
2018-03-27 17:13:55 +02:00
|
|
|
sorted(all_lints + deprecated_lints,
|
2016-04-30 23:54:10 +02:00
|
|
|
key=lambda l: l[1])],
|
2016-04-14 17:56:46 +02:00
|
|
|
replace_start=False, write_back=not check)
|
|
|
|
|
2016-05-27 15:57:44 +02:00
|
|
|
# update version of clippy_lints in Cargo.toml
|
|
|
|
changed |= replace_region(
|
|
|
|
'Cargo.toml', r'# begin automatic update', '# end automatic update',
|
|
|
|
lambda: ['clippy_lints = { version = "%s", path = "clippy_lints" }\n' %
|
|
|
|
clippy_version],
|
|
|
|
replace_start=False, write_back=not check)
|
|
|
|
|
|
|
|
# update version of clippy_lints in Cargo.toml
|
|
|
|
changed |= replace_region(
|
|
|
|
'clippy_lints/Cargo.toml', r'# begin automatic update', '# end automatic update',
|
|
|
|
lambda: ['version = "%s"\n' % clippy_version],
|
|
|
|
replace_start=False, write_back=not check)
|
|
|
|
|
2016-02-20 17:00:36 +01:00
|
|
|
# update the `pub mod` list
|
|
|
|
changed |= replace_region(
|
2016-05-24 18:25:25 +02:00
|
|
|
'clippy_lints/src/lib.rs', r'begin lints modules', r'end lints modules',
|
2018-03-27 17:13:55 +02:00
|
|
|
lambda: gen_mods(all_lints),
|
2016-02-20 17:00:36 +01:00
|
|
|
replace_start=False, write_back=not check)
|
|
|
|
|
2018-07-28 17:35:41 +02:00
|
|
|
# same for "clippy::*" lint collections
|
2015-09-01 17:53:56 +02:00
|
|
|
changed |= replace_region(
|
2018-07-28 17:35:41 +02:00
|
|
|
'clippy_lints/src/lib.rs', r'reg.register_lint_group\("clippy::all"', r'\]\);',
|
2018-03-28 23:49:32 +02:00
|
|
|
lambda: gen_group(clippy_lint_list),
|
2015-09-01 17:53:56 +02:00
|
|
|
replace_start=False, write_back=not check)
|
|
|
|
|
2018-03-28 15:24:26 +02:00
|
|
|
for key, value in clippy_lints.iteritems():
|
2018-07-28 17:35:41 +02:00
|
|
|
# same for "clippy::*" lint collections
|
2018-03-28 15:24:26 +02:00
|
|
|
changed |= replace_region(
|
2018-07-28 17:35:41 +02:00
|
|
|
'clippy_lints/src/lib.rs', r'reg.register_lint_group\("clippy::' + key + r'"', r'\]\);',
|
2018-03-28 15:24:26 +02:00
|
|
|
lambda: gen_group(value),
|
|
|
|
replace_start=False, write_back=not check)
|
2018-03-27 17:13:55 +02:00
|
|
|
|
2016-03-24 19:25:59 +01:00
|
|
|
# same for "deprecated" lint collection
|
|
|
|
changed |= replace_region(
|
2016-05-27 15:57:03 +02:00
|
|
|
'clippy_lints/src/lib.rs', r'let mut store', r'end deprecated lints',
|
|
|
|
lambda: gen_deprecated(deprecated_lints),
|
|
|
|
replace_start=False,
|
|
|
|
write_back=not check)
|
2016-03-24 19:25:59 +01:00
|
|
|
|
2015-08-13 10:32:35 +02:00
|
|
|
if check and changed:
|
|
|
|
print('Please run util/update_lints.py to regenerate lints lists.')
|
|
|
|
return 1
|
2015-08-13 11:31:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-08-13 10:32:35 +02:00
|
|
|
sys.exit(main(print_only='-n' in sys.argv, check='-c' in sys.argv))
|