@@ -5,7 +5,6 @@ use itertools::Itertools;
5
5
use rustc_lexer:: unescape;
6
6
use std:: collections:: HashSet ;
7
7
use std:: fmt:: Write ;
8
- use std:: fs:: OpenOptions ;
9
8
use std:: ops:: Range ;
10
9
use std:: path:: { Path , PathBuf } ;
11
10
use walkdir:: { DirEntry , WalkDir } ;
@@ -27,12 +26,11 @@ const DOCS_LINK: &str = "https://rust-lang.github.io/rust-clippy/master/index.ht
27
26
/// Panics if a file path could not read from or then written to
28
27
pub fn update ( update_mode : UpdateMode ) {
29
28
let lints = find_lint_decls ( ) ;
30
- let DeprecatedLints {
31
- renamed, deprecated, ..
32
- } = read_deprecated_lints ( ) ;
29
+ let ( deprecated, renamed) = read_deprecated_lints ( ) ;
33
30
generate_lint_files ( update_mode, & lints, & deprecated, & renamed) ;
34
31
}
35
32
33
+ #[ expect( clippy:: too_many_lines) ]
36
34
pub fn generate_lint_files (
37
35
update_mode : UpdateMode ,
38
36
lints : & [ Lint ] ,
@@ -94,6 +92,40 @@ pub fn generate_lint_files(
94
92
dst. push_str ( "];\n " ) ;
95
93
UpdateStatus :: from_changed ( src != dst)
96
94
} ) ,
95
+ ( "clippy_lints/src/deprecated_lints.rs" , & mut |_, src, dst| {
96
+ let mut searcher = RustSearcher :: new ( src) ;
97
+ assert ! (
98
+ searcher. find_token( Token :: Ident ( "declare_with_version" ) )
99
+ && searcher. find_token( Token :: Ident ( "declare_with_version" ) ) ,
100
+ "error reading deprecated lints"
101
+ ) ;
102
+ dst. push_str ( & src[ ..searcher. pos ( ) as usize ] ) ;
103
+ dst. push_str ( "! { DEPRECATED(DEPRECATED_VERSION) = [\n " ) ;
104
+ for lint in deprecated {
105
+ write ! (
106
+ dst,
107
+ " #[clippy::version = \" {}\" ]\n (\" {}\" , \" {}\" ),\n " ,
108
+ lint. version, lint. name, lint. reason,
109
+ )
110
+ . unwrap ( ) ;
111
+ }
112
+ dst. push_str (
113
+ "]}\n \n \
114
+ #[rustfmt::skip]\n \
115
+ declare_with_version! { RENAMED(RENAMED_VERSION) = [\n \
116
+ ",
117
+ ) ;
118
+ for lint in renamed {
119
+ write ! (
120
+ dst,
121
+ " #[clippy::version = \" {}\" ]\n (\" {}\" , \" {}\" ),\n " ,
122
+ lint. version, lint. old_name, lint. new_name,
123
+ )
124
+ . unwrap ( ) ;
125
+ }
126
+ dst. push_str ( "]}\n " ) ;
127
+ UpdateStatus :: from_changed ( src != dst)
128
+ } ) ,
97
129
( "tests/ui/deprecated.rs" , & mut |_, src, dst| {
98
130
dst. push_str ( GENERATED_FILE_COMMENT ) ;
99
131
for lint in deprecated {
@@ -129,7 +161,7 @@ fn round_to_fifty(count: usize) -> usize {
129
161
}
130
162
131
163
/// Lint data parsed from the Clippy source code.
132
- #[ derive( Clone , PartialEq , Eq , Debug ) ]
164
+ #[ derive( PartialEq , Eq , Debug ) ]
133
165
pub struct Lint {
134
166
pub name : String ,
135
167
pub group : String ,
@@ -138,15 +170,16 @@ pub struct Lint {
138
170
pub declaration_range : Range < usize > ,
139
171
}
140
172
141
- #[ derive( Clone , PartialEq , Eq , Debug ) ]
142
173
pub struct DeprecatedLint {
143
174
pub name : String ,
144
175
pub reason : String ,
176
+ pub version : String ,
145
177
}
146
178
147
179
pub struct RenamedLint {
148
180
pub old_name : String ,
149
181
pub new_name : String ,
182
+ pub version : String ,
150
183
}
151
184
152
185
/// Finds all lint declarations (`declare_clippy_lint!`)
@@ -230,23 +263,14 @@ fn parse_clippy_lint_decls(path: &Path, contents: &str, module: &str, lints: &mu
230
263
}
231
264
}
232
265
233
- pub struct DeprecatedLints {
234
- pub file : File < ' static > ,
235
- pub contents : String ,
236
- pub deprecated : Vec < DeprecatedLint > ,
237
- pub renamed : Vec < RenamedLint > ,
238
- pub deprecated_end : u32 ,
239
- pub renamed_end : u32 ,
240
- }
241
-
242
266
#[ must_use]
243
- pub fn read_deprecated_lints ( ) -> DeprecatedLints {
267
+ pub fn read_deprecated_lints ( ) -> ( Vec < DeprecatedLint > , Vec < RenamedLint > ) {
244
268
#[ allow( clippy:: enum_glob_use) ]
245
269
use Token :: * ;
246
270
#[ rustfmt:: skip]
247
271
static DECL_TOKENS : & [ Token < ' _ > ] = & [
248
272
// #[clippy::version = "version"]
249
- Pound , OpenBracket , Ident ( "clippy" ) , DoubleColon , Ident ( "version" ) , Eq , LitStr , CloseBracket ,
273
+ Pound , OpenBracket , Ident ( "clippy" ) , DoubleColon , Ident ( "version" ) , Eq , CaptureLitStr , CloseBracket ,
250
274
// ("first", "second"),
251
275
OpenParen , CaptureLitStr , Comma , CaptureLitStr , CloseParen , Comma ,
252
276
] ;
@@ -262,17 +286,12 @@ pub fn read_deprecated_lints() -> DeprecatedLints {
262
286
] ;
263
287
264
288
let path = "clippy_lints/src/deprecated_lints.rs" ;
265
- let mut res = DeprecatedLints {
266
- file : File :: open ( path, OpenOptions :: new ( ) . read ( true ) . write ( true ) ) ,
267
- contents : String :: new ( ) ,
268
- deprecated : Vec :: with_capacity ( 30 ) ,
269
- renamed : Vec :: with_capacity ( 80 ) ,
270
- deprecated_end : 0 ,
271
- renamed_end : 0 ,
272
- } ;
289
+ let mut deprecated = Vec :: with_capacity ( 30 ) ;
290
+ let mut renamed = Vec :: with_capacity ( 80 ) ;
291
+ let mut contents = String :: new ( ) ;
292
+ File :: open_read_to_cleared_string ( path, & mut contents) ;
273
293
274
- res. file . read_append_to_string ( & mut res. contents ) ;
275
- let mut searcher = RustSearcher :: new ( & res. contents ) ;
294
+ let mut searcher = RustSearcher :: new ( & contents) ;
276
295
277
296
// First instance is the macro definition.
278
297
assert ! (
@@ -281,36 +300,38 @@ pub fn read_deprecated_lints() -> DeprecatedLints {
281
300
) ;
282
301
283
302
if searcher. find_token ( Ident ( "declare_with_version" ) ) && searcher. match_tokens ( DEPRECATED_TOKENS , & mut [ ] ) {
303
+ let mut version = "" ;
284
304
let mut name = "" ;
285
305
let mut reason = "" ;
286
- while searcher. match_tokens ( DECL_TOKENS , & mut [ & mut name, & mut reason] ) {
287
- res . deprecated . push ( DeprecatedLint {
306
+ while searcher. match_tokens ( DECL_TOKENS , & mut [ & mut version , & mut name, & mut reason] ) {
307
+ deprecated. push ( DeprecatedLint {
288
308
name : parse_str_single_line ( path. as_ref ( ) , name) ,
289
309
reason : parse_str_single_line ( path. as_ref ( ) , reason) ,
310
+ version : parse_str_single_line ( path. as_ref ( ) , version) ,
290
311
} ) ;
291
312
}
292
313
} else {
293
314
panic ! ( "error reading deprecated lints" ) ;
294
315
}
295
- // position of the closing `]}` of `declare_with_version`
296
- res. deprecated_end = searcher. pos ( ) ;
297
316
298
317
if searcher. find_token ( Ident ( "declare_with_version" ) ) && searcher. match_tokens ( RENAMED_TOKENS , & mut [ ] ) {
318
+ let mut version = "" ;
299
319
let mut old_name = "" ;
300
320
let mut new_name = "" ;
301
- while searcher. match_tokens ( DECL_TOKENS , & mut [ & mut old_name, & mut new_name] ) {
302
- res . renamed . push ( RenamedLint {
321
+ while searcher. match_tokens ( DECL_TOKENS , & mut [ & mut version , & mut old_name, & mut new_name] ) {
322
+ renamed. push ( RenamedLint {
303
323
old_name : parse_str_single_line ( path. as_ref ( ) , old_name) ,
304
324
new_name : parse_str_single_line ( path. as_ref ( ) , new_name) ,
325
+ version : parse_str_single_line ( path. as_ref ( ) , version) ,
305
326
} ) ;
306
327
}
307
328
} else {
308
329
panic ! ( "error reading renamed lints" ) ;
309
330
}
310
- // position of the closing `]}` of `declare_with_version`
311
- res. renamed_end = searcher. pos ( ) ;
312
331
313
- res
332
+ deprecated. sort_by ( |lhs, rhs| lhs. name . cmp ( & rhs. name ) ) ;
333
+ renamed. sort_by ( |lhs, rhs| lhs. old_name . cmp ( & rhs. old_name ) ) ;
334
+ ( deprecated, renamed)
314
335
}
315
336
316
337
/// Removes the line splices and surrounding quotes from a string literal
0 commit comments