2 # Generate a Markdown table of all lints, and put it in README.md.
3 # With -n option, only print the new table to stdout.
4 # With -c option, print a warning and set exit status to 1 if a file would be
11 declare_deprecated_lint_re = re.compile(r'''
12 declare_deprecated_lint! \s* [{(] \s*
13 pub \s+ (?P<name>[A-Z_][A-Z_0-9]*) \s*,\s*
14 " (?P<desc>(?:[^"\\]+|\\.)*) " \s* [})]
15 ''', re.VERBOSE | re.DOTALL)
17 declare_clippy_lint_re = re.compile(r'''
18 declare_clippy_lint! \s* [{(] \s*
19 pub \s+ (?P<name>[A-Z_][A-Z_0-9]*) \s*,\s*
20 (?P<cat>[a-z_]+) \s*,\s*
21 " (?P<desc>(?:[^"\\]+|\\.)*) " \s* [})]
22 ''', re.VERBOSE | re.DOTALL)
24 nl_escape_re = re.compile(r'\\\n\s*')
26 docs_link = 'https://rust-lang-nursery.github.io/rust-clippy/master/index.html'
29 def collect(lints, deprecated_lints, clippy_lints, fn):
30 """Collect all lints from a file.
32 Adds entries to the lints list as `(module, name, level, desc)`.
37 for match in declare_deprecated_lint_re.finditer(code):
38 # remove \-newline escapes from description string
39 desc = nl_escape_re.sub('', match.group('desc'))
40 deprecated_lints.append((os.path.splitext(os.path.basename(fn))[0],
41 match.group('name').lower(),
42 desc.replace('\\"', '"')))
44 for match in declare_clippy_lint_re.finditer(code):
45 # remove \-newline escapes from description string
46 desc = nl_escape_re.sub('', match.group('desc'))
47 cat = match.group('cat')
48 clippy_lints[cat].append((os.path.splitext(os.path.basename(fn))[0],
49 match.group('name').lower(),
51 desc.replace('\\"', '"')))
54 def gen_group(lints, levels=None):
55 """Write lint group (list of all lints in the form module::NAME)."""
57 lints = [tup for tup in lints if tup[2] in levels]
58 for (module, name, _, _) in sorted(lints):
59 yield ' %s::%s,\n' % (module, name.upper())
65 for module in sorted(set(lint[0] for lint in lints)):
66 yield 'pub mod %s;\n' % module
69 def gen_deprecated(lints):
70 """Declare deprecated lints"""
73 yield ' store.register_removed(\n'
74 yield ' "%s",\n' % lint[1]
75 yield ' "%s",\n' % lint[2]
79 def replace_region(fn, region_start, region_end, callback,
80 replace_start=True, write_back=True):
81 """Replace a region in a file delimited by two lines matching regexes.
83 A callback is called to write the new region. If `replace_start` is true,
84 the start delimiter line is replaced as well. The end delimiter line is
87 # read current content
91 # replace old region with new region
96 if re.search(region_end, line):
98 new_lines.extend(callback())
99 new_lines.append(line)
100 elif re.search(region_start, line):
101 if not replace_start:
102 new_lines.append(line)
103 # old region starts here
106 new_lines.append(line)
110 with open(fn, 'w') as fp:
111 fp.writelines(new_lines)
113 # if something changed, return true
114 return lines != new_lines
117 def main(print_only=False, check=False):
119 deprecated_lints = []
131 if not os.path.isfile('clippy_lints/src/lib.rs'):
132 print('Error: call this script from clippy checkout directory!')
135 # collect all lints from source files
136 for fn in os.listdir('clippy_lints/src'):
137 if fn.endswith('.rs'):
138 collect(lints, deprecated_lints, clippy_lints,
139 os.path.join('clippy_lints', 'src', fn))
142 with open('Cargo.toml') as fp:
144 if line.startswith('version ='):
145 clippy_version = line.split()[2].strip('"')
148 print('Error: version not found in Cargo.toml!')
152 for _, value in clippy_lints.iteritems():
156 sys.stdout.writelines(gen_table(all_lints))
159 # update the lint counter in README.md
160 changed = replace_region(
162 r'^\[There are \d+ lints included in this crate\]\(https://rust-lang-nursery.github.io/rust-clippy/master/index.html\)$', "",
163 lambda: ['[There are %d lints included in this crate](https://rust-lang-nursery.github.io/rust-clippy/master/index.html)\n' %
165 write_back=not check)
167 # update the links in the CHANGELOG
168 changed |= replace_region(
170 "<!-- begin autogenerated links to wiki -->",
171 "<!-- end autogenerated links to wiki -->",
172 lambda: ["[`{0}`]: {1}#{0}\n".format(l[1], docs_link) for l in
173 sorted(all_lints + deprecated_lints,
174 key=lambda l: l[1])],
175 replace_start=False, write_back=not check)
177 # update version of clippy_lints in Cargo.toml
178 changed |= replace_region(
179 'Cargo.toml', r'# begin automatic update', '# end automatic update',
180 lambda: ['clippy_lints = { version = "%s", path = "clippy_lints" }\n' %
182 replace_start=False, write_back=not check)
184 # update version of clippy_lints in Cargo.toml
185 changed |= replace_region(
186 'clippy_lints/Cargo.toml', r'# begin automatic update', '# end automatic update',
187 lambda: ['version = "%s"\n' % clippy_version],
188 replace_start=False, write_back=not check)
190 # update the `pub mod` list
191 changed |= replace_region(
192 'clippy_lints/src/lib.rs', r'begin lints modules', r'end lints modules',
193 lambda: gen_mods(all_lints),
194 replace_start=False, write_back=not check)
196 # same for "clippy" lint collection
197 changed |= replace_region(
198 'clippy_lints/src/lib.rs', r'reg.register_lint_group\("clippy"', r'\]\);',
199 lambda: gen_group(lints, levels=('warn', 'deny')),
200 replace_start=False, write_back=not check)
202 for key, value in clippy_lints.iteritems():
203 # same for "clippy_*" lint collections
204 changed |= replace_region(
205 'clippy_lints/src/lib.rs', r'reg.register_lint_group\("clippy_' + key + r'"', r'\]\);',
206 lambda: gen_group(value),
207 replace_start=False, write_back=not check)
209 # same for "deprecated" lint collection
210 changed |= replace_region(
211 'clippy_lints/src/lib.rs', r'let mut store', r'end deprecated lints',
212 lambda: gen_deprecated(deprecated_lints),
214 write_back=not check)
216 if check and changed:
217 print('Please run util/update_lints.py to regenerate lints lists.')
221 if __name__ == '__main__':
222 sys.exit(main(print_only='-n' in sys.argv, check='-c' in sys.argv))