21
21
" (?P<desc>(?:[^"\\]+|\\.)*) " \s* [})]
22
22
''' , re .VERBOSE | re .DOTALL )
23
23
24
+ declare_restriction_lint_re = re .compile (r'''
25
+ declare_restriction_lint! \s* [{(] \s*
26
+ pub \s+ (?P<name>[A-Z_][A-Z_0-9]*) \s*,\s*
27
+ " (?P<desc>(?:[^"\\]+|\\.)*) " \s* [})]
28
+ ''' , re .VERBOSE | re .DOTALL )
29
+
24
30
nl_escape_re = re .compile (r'\\\n\s*' )
25
31
26
32
wiki_link = 'https://github.com/Manishearth/rust-clippy/wiki'
27
33
28
34
29
- def collect (lints , deprecated_lints , fn ):
35
+ def collect (lints , deprecated_lints , restriction_lints , fn ):
30
36
"""Collect all lints from a file.
31
37
32
38
Adds entries to the lints list as `(module, name, level, desc)`.
@@ -48,6 +54,14 @@ def collect(lints, deprecated_lints, fn):
48
54
match .group ('name' ).lower (),
49
55
desc .replace ('\\ "' , '"' )))
50
56
57
+ for match in declare_restriction_lint_re .finditer (code ):
58
+ # remove \-newline escapes from description string
59
+ desc = nl_escape_re .sub ('' , match .group ('desc' ))
60
+ restriction_lints .append ((os .path .splitext (os .path .basename (fn ))[0 ],
61
+ match .group ('name' ).lower (),
62
+ "allow" ,
63
+ desc .replace ('\\ "' , '"' )))
64
+
51
65
52
66
def gen_table (lints , link = None ):
53
67
"""Write lint table in Markdown format."""
@@ -86,7 +100,6 @@ def gen_deprecated(lints):
86
100
for lint in lints :
87
101
yield ' store.register_removed("%s", "%s");\n ' % (lint [1 ], lint [2 ])
88
102
89
-
90
103
def replace_region (fn , region_start , region_end , callback ,
91
104
replace_start = True , write_back = True ):
92
105
"""Replace a region in a file delimited by two lines matching regexes.
@@ -128,6 +141,7 @@ def replace_region(fn, region_start, region_end, callback,
128
141
def main (print_only = False , check = False ):
129
142
lints = []
130
143
deprecated_lints = []
144
+ restriction_lints = []
131
145
132
146
# check directory
133
147
if not os .path .isfile ('src/lib.rs' ):
@@ -138,22 +152,24 @@ def main(print_only=False, check=False):
138
152
for root , _ , files in os .walk ('src' ):
139
153
for fn in files :
140
154
if fn .endswith ('.rs' ):
141
- collect (lints , deprecated_lints , os .path .join (root , fn ))
155
+ collect (lints , deprecated_lints , restriction_lints ,
156
+ os .path .join (root , fn ))
142
157
143
158
if print_only :
144
- sys .stdout .writelines (gen_table (lints ))
159
+ sys .stdout .writelines (gen_table (lints + restriction_lints ))
145
160
return
146
161
147
162
# replace table in README.md
148
163
changed = replace_region (
149
164
'README.md' , r'^name +\|' , '^$' ,
150
- lambda : gen_table (lints , link = wiki_link ),
165
+ lambda : gen_table (lints + restriction_lints , link = wiki_link ),
151
166
write_back = not check )
152
167
153
168
changed |= replace_region (
154
169
'README.md' ,
155
170
r'^There are \d+ lints included in this crate:' , "" ,
156
- lambda : ['There are %d lints included in this crate:\n ' % len (lints )],
171
+ lambda : ['There are %d lints included in this crate:\n ' % (len (lints )
172
+ + len (restriction_lints ))],
157
173
write_back = not check )
158
174
159
175
# update the links in the CHANGELOG
@@ -162,13 +178,14 @@ def main(print_only=False, check=False):
162
178
"<!-- begin autogenerated links to wiki -->" ,
163
179
"<!-- end autogenerated links to wiki -->" ,
164
180
lambda : ["[`{0}`]: {1}#{0}\n " .format (l [1 ], wiki_link ) for l in
165
- sorted (lints + deprecated_lints , key = lambda l : l [1 ])],
181
+ sorted (lints + restriction_lints + deprecated_lints ,
182
+ key = lambda l : l [1 ])],
166
183
replace_start = False , write_back = not check )
167
184
168
185
# update the `pub mod` list
169
186
changed |= replace_region (
170
187
'src/lib.rs' , r'begin lints modules' , r'end lints modules' ,
171
- lambda : gen_mods (lints ),
188
+ lambda : gen_mods (lints + restriction_lints ),
172
189
replace_start = False , write_back = not check )
173
190
174
191
# same for "clippy" lint collection
@@ -190,6 +207,12 @@ def main(print_only=False, check=False):
190
207
lambda : gen_group (lints , levels = ('allow' ,)),
191
208
replace_start = False , write_back = not check )
192
209
210
+ # same for "clippy_restrictions" lint collection
211
+ changed |= replace_region (
212
+ 'src/lib.rs' , r'reg.register_lint_group\("clippy_restrictions"' ,
213
+ r'\]\);' , lambda : gen_group (restriction_lints ),
214
+ replace_start = False , write_back = not check )
215
+
193
216
if check and changed :
194
217
print ('Please run util/update_lints.py to regenerate lints lists.' )
195
218
return 1
0 commit comments