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(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
93 # replace old region with new region
98 if re.search(region_end, line):
100 new_lines.extend(callback())
101 new_lines.append(line)
102 elif re.search(region_start, line):
103 if not replace_start:
104 new_lines.append(line)
105 # old region starts here
109 new_lines.append(line)
112 print "regex " + region_start + " not found"
116 with open(fn, 'w') as fp:
117 fp.writelines(new_lines)
119 # if something changed, return true
120 return lines != new_lines
123 def main(print_only=False, check=False):
124 deprecated_lints = []
137 if not os.path.isfile('clippy_lints/src/lib.rs'):
138 print('Error: call this script from clippy checkout directory!')
141 # collect all lints from source files
142 for fn in os.listdir('clippy_lints/src'):
143 if fn.endswith('.rs'):
144 collect(deprecated_lints, clippy_lints,
145 os.path.join('clippy_lints', 'src', fn))
148 with open('Cargo.toml') as fp:
150 if line.startswith('version ='):
151 clippy_version = line.split()[2].strip('"')
154 print('Error: version not found in Cargo.toml!')
158 clippy_lint_groups = [
164 clippy_lint_list = []
165 for x in clippy_lint_groups:
166 clippy_lint_list += clippy_lints[x]
167 for _, value in clippy_lints.iteritems():
171 sys.stdout.writelines(gen_table(all_lints))
174 # update the lint counter in README.md
175 changed = replace_region(
177 r'^\[There are \d+ lints included in this crate!\]\(https://rust-lang-nursery.github.io/rust-clippy/master/index.html\)$', "",
178 lambda: ['[There are %d lints included in this crate!](https://rust-lang-nursery.github.io/rust-clippy/master/index.html)\n' %
180 write_back=not check)
182 # update the links in the CHANGELOG
183 changed |= replace_region(
185 "<!-- begin autogenerated links to wiki -->",
186 "<!-- end autogenerated links to wiki -->",
187 lambda: ["[`{0}`]: {1}#{0}\n".format(l[1], docs_link) for l in
188 sorted(all_lints + deprecated_lints,
189 key=lambda l: l[1])],
190 replace_start=False, write_back=not check)
192 # update version of clippy_lints in Cargo.toml
193 changed |= replace_region(
194 'Cargo.toml', r'# begin automatic update', '# end automatic update',
195 lambda: ['clippy_lints = { version = "%s", path = "clippy_lints" }\n' %
197 replace_start=False, write_back=not check)
199 # update version of clippy_lints in Cargo.toml
200 changed |= replace_region(
201 'clippy_lints/Cargo.toml', r'# begin automatic update', '# end automatic update',
202 lambda: ['version = "%s"\n' % clippy_version],
203 replace_start=False, write_back=not check)
205 # update the `pub mod` list
206 changed |= replace_region(
207 'clippy_lints/src/lib.rs', r'begin lints modules', r'end lints modules',
208 lambda: gen_mods(all_lints),
209 replace_start=False, write_back=not check)
211 # same for "clippy_*" lint collections
212 changed |= replace_region(
213 'clippy_lints/src/lib.rs', r'reg.register_lint_group\("clippy"', r'\]\);',
214 lambda: gen_group(clippy_lint_list),
215 replace_start=False, write_back=not check)
217 for key, value in clippy_lints.iteritems():
218 # same for "clippy_*" lint collections
219 changed |= replace_region(
220 'clippy_lints/src/lib.rs', r'reg.register_lint_group\("clippy_' + key + r'"', r'\]\);',
221 lambda: gen_group(value),
222 replace_start=False, write_back=not check)
224 # same for "deprecated" lint collection
225 changed |= replace_region(
226 'clippy_lints/src/lib.rs', r'let mut store', r'end deprecated lints',
227 lambda: gen_deprecated(deprecated_lints),
229 write_back=not check)
231 if check and changed:
232 print('Please run util/update_lints.py to regenerate lints lists.')
236 if __name__ == '__main__':
237 sys.exit(main(print_only='-n' in sys.argv, check='-c' in sys.argv))