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
10 from subprocess import call
12 declare_deprecated_lint_re = re.compile(r'''
13 declare_deprecated_lint! \s* [{(] \s*
14 pub \s+ (?P<name>[A-Z_][A-Z_0-9]*) \s*,\s*
15 " (?P<desc>(?:[^"\\]+|\\.)*) " \s* [})]
16 ''', re.VERBOSE | re.DOTALL)
18 declare_clippy_lint_re = re.compile(r'''
19 declare_clippy_lint! \s* [{(] \s*
20 pub \s+ (?P<name>[A-Z_][A-Z_0-9]*) \s*,\s*
21 (?P<cat>[a-z_]+) \s*,\s*
22 " (?P<desc>(?:[^"\\]+|\\.)*) " \s* [})]
23 ''', re.VERBOSE | re.DOTALL)
25 nl_escape_re = re.compile(r'\\\n\s*')
27 docs_link = 'https://rust-lang-nursery.github.io/rust-clippy/master/index.html'
30 def collect(deprecated_lints, clippy_lints, fn):
31 """Collect all lints from a file.
33 Adds entries to the lints list as `(module, name, level, desc)`.
38 for match in declare_deprecated_lint_re.finditer(code):
39 # remove \-newline escapes from description string
40 desc = nl_escape_re.sub('', match.group('desc'))
41 deprecated_lints.append((os.path.splitext(os.path.basename(fn))[0],
42 match.group('name').lower(),
43 desc.replace('\\"', '"')))
45 for match in declare_clippy_lint_re.finditer(code):
46 # remove \-newline escapes from description string
47 desc = nl_escape_re.sub('', match.group('desc'))
48 cat = match.group('cat')
49 if cat in ('internal', 'internal_warn'):
51 module_name = os.path.splitext(os.path.basename(fn))[0]
52 if module_name == 'mod':
53 module_name = os.path.basename(os.path.dirname(fn))
54 clippy_lints[cat].append((module_name,
55 match.group('name').lower(),
57 desc.replace('\\"', '"')))
61 """Write lint group (list of all lints in the form module::NAME)."""
62 for (module, name, _, _) in sorted(lints):
63 yield ' %s::%s,\n' % (module, name.upper())
69 for module in sorted(set(lint[0] for lint in lints)):
70 yield 'pub mod %s;\n' % module
73 def gen_deprecated(lints):
74 """Declare deprecated lints"""
77 yield ' store.register_removed(\n'
78 yield ' "%s",\n' % lint[1]
79 yield ' "%s",\n' % lint[2]
83 def replace_region(fn, region_start, region_end, callback,
84 replace_start=True, write_back=True):
85 """Replace a region in a file delimited by two lines matching regexes.
87 A callback is called to write the new region. If `replace_start` is true,
88 the start delimiter line is replaced as well. The end delimiter line is
91 # read current content
97 # replace old region with new region
102 if re.search(region_end, line):
103 in_old_region = False
104 new_lines.extend(callback())
105 new_lines.append(line)
106 elif re.search(region_start, line):
107 if not replace_start:
108 new_lines.append(line)
109 # old region starts here
113 new_lines.append(line)
116 print("regex " + region_start + " not found")
120 with open(fn, 'w') as fp:
121 fp.writelines(new_lines)
123 # if something changed, return true
124 return lines != new_lines
127 def main(print_only=False, check=False):
128 deprecated_lints = []
141 if not os.path.isfile('clippy_lints/src/lib.rs'):
142 print('Error: call this script from clippy checkout directory!')
145 # collect all lints from source files
146 for root, dirs, files in os.walk('clippy_lints/src'):
148 if fn.endswith('.rs'):
149 collect(deprecated_lints, clippy_lints,
150 os.path.join(root, fn))
153 with open('Cargo.toml') as fp:
155 if line.startswith('version ='):
156 clippy_version = line.split()[2].strip('"')
159 print('Error: version not found in Cargo.toml!')
163 clippy_lint_groups = [
169 clippy_lint_list = []
170 for x in clippy_lint_groups:
171 clippy_lint_list += clippy_lints[x]
172 for _, value in clippy_lints.iteritems():
176 call(["./util/dev", "update_lints", "--print-only"])
179 # update the lint counter in README.md
180 changed = replace_region(
182 r'^\[There are \d+ lints included in this crate!\]\(https://rust-lang-nursery.github.io/rust-clippy/master/index.html\)$', "",
183 lambda: ['[There are %d lints included in this crate!](https://rust-lang-nursery.github.io/rust-clippy/master/index.html)\n' %
185 write_back=not check)
187 # update the links in the CHANGELOG
188 changed |= replace_region(
190 "<!-- begin autogenerated links to lint list -->",
191 "<!-- end autogenerated links to lint list -->",
192 lambda: ["[`{0}`]: {1}#{0}\n".format(l[1], docs_link) for l in
193 sorted(all_lints + deprecated_lints,
194 key=lambda l: l[1])],
195 replace_start=False, write_back=not check)
197 # update version of clippy_lints in Cargo.toml
198 changed |= replace_region(
199 'Cargo.toml', r'# begin automatic update', '# end automatic update',
200 lambda: ['clippy_lints = { version = "%s", path = "clippy_lints" }\n' %
202 replace_start=False, write_back=not check)
204 # update version of clippy_lints in Cargo.toml
205 changed |= replace_region(
206 'clippy_lints/Cargo.toml', r'# begin automatic update', '# end automatic update',
207 lambda: ['version = "%s"\n' % clippy_version],
208 replace_start=False, write_back=not check)
210 # update the `pub mod` list
211 changed |= replace_region(
212 'clippy_lints/src/lib.rs', r'begin lints modules', r'end lints modules',
213 lambda: gen_mods(all_lints),
214 replace_start=False, write_back=not check)
216 # same for "clippy::*" lint collections
217 changed |= replace_region(
218 'clippy_lints/src/lib.rs', r'reg.register_lint_group\("clippy::all"', r'\]\);',
219 lambda: gen_group(clippy_lint_list),
220 replace_start=False, write_back=not check)
222 for key, value in clippy_lints.iteritems():
223 # same for "clippy::*" lint collections
224 changed |= replace_region(
225 'clippy_lints/src/lib.rs', r'reg.register_lint_group\("clippy::' + key + r'"', r'\]\);',
226 lambda: gen_group(value),
227 replace_start=False, write_back=not check)
229 # same for "deprecated" lint collection
230 changed |= replace_region(
231 'clippy_lints/src/lib.rs', r'let mut store', r'end deprecated lints',
232 lambda: gen_deprecated(deprecated_lints),
234 write_back=not check)
236 if check and changed:
237 print('Please run util/update_lints.py to regenerate lints lists.')
241 if __name__ == '__main__':
242 sys.exit(main(print_only='-n' in sys.argv, check='-c' in sys.argv))