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