]> git.lizzy.rs Git - rust.git/blob - util/update_lints.py
Merge pull request #2743 from phansch/rustup20180510
[rust.git] / util / update_lints.py
1 #!/usr/bin/env python
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
5 # changed.
6
7 import os
8 import re
9 import sys
10
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)
16
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)
23
24 nl_escape_re = re.compile(r'\\\n\s*')
25
26 docs_link = 'https://rust-lang-nursery.github.io/rust-clippy/master/index.html'
27
28
29 def collect(deprecated_lints, clippy_lints, fn):
30     """Collect all lints from a file.
31
32     Adds entries to the lints list as `(module, name, level, desc)`.
33     """
34     with open(fn) as fp:
35         code = fp.read()
36
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('\\"', '"')))
43     
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(),
50                                   "allow",
51                                   desc.replace('\\"', '"')))
52
53
54 def gen_group(lints, levels=None):
55     """Write lint group (list of all lints in the form module::NAME)."""
56     if levels:
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())
60
61
62 def gen_mods(lints):
63     """Declare modules"""
64
65     for module in sorted(set(lint[0] for lint in lints)):
66         yield 'pub mod %s;\n' % module
67
68
69 def gen_deprecated(lints):
70     """Declare deprecated lints"""
71
72     for lint in lints:
73         yield '    store.register_removed(\n'
74         yield '        "%s",\n' % lint[1]
75         yield '        "%s",\n' % lint[2]
76         yield '    );\n'
77
78
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.
82
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
85     never replaced.
86     """
87     # read current content
88     with open(fn) as fp:
89         lines = list(fp)
90
91     found = False
92
93     # replace old region with new region
94     new_lines = []
95     in_old_region = False
96     for line in lines:
97         if in_old_region:
98             if re.search(region_end, line):
99                 in_old_region = False
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
106             in_old_region = True
107             found = True
108         else:
109             new_lines.append(line)
110
111     if not found:
112         print "regex " + region_start + " not found"
113
114     # write back to file
115     if write_back:
116         with open(fn, 'w') as fp:
117             fp.writelines(new_lines)
118
119     # if something changed, return true
120     return lines != new_lines
121
122
123 def main(print_only=False, check=False):
124     deprecated_lints = []
125     clippy_lints = {
126         "correctness": [],
127         "style": [],
128         "complexity": [],
129         "perf": [],
130         "restriction": [],
131         "pedantic": [],
132         "cargo": [],
133         "nursery": [],
134     }
135
136     # check directory
137     if not os.path.isfile('clippy_lints/src/lib.rs'):
138         print('Error: call this script from clippy checkout directory!')
139         return
140
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))
146
147     # determine version
148     with open('Cargo.toml') as fp:
149         for line in fp:
150             if line.startswith('version ='):
151                 clippy_version = line.split()[2].strip('"')
152                 break
153         else:
154             print('Error: version not found in Cargo.toml!')
155             return
156
157     all_lints = []
158     clippy_lint_groups = [
159         "correctness",
160         "style",
161         "complexity",
162         "perf",
163     ]
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():
168         all_lints += value
169
170     if print_only:
171         sys.stdout.writelines(gen_table(all_lints))
172         return
173
174     # update the lint counter in README.md
175     changed = replace_region(
176         'README.md',
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' %
179                  (len(all_lints))],
180         write_back=not check)
181
182     # update the links in the CHANGELOG
183     changed |= replace_region(
184         'CHANGELOG.md',
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)
191
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' %
196                  clippy_version],
197         replace_start=False, write_back=not check)
198
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)
204
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)
210
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)
216
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)
223
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),
228         replace_start=False,
229         write_back=not check)
230
231     if check and changed:
232         print('Please run util/update_lints.py to regenerate lints lists.')
233         return 1
234
235
236 if __name__ == '__main__':
237     sys.exit(main(print_only='-n' in sys.argv, check='-c' in sys.argv))