1
1
use core:: fmt:: Write ;
2
2
use itertools:: Itertools ;
3
3
use rustc_lexer:: { tokenize, unescape, LiteralKind , TokenKind } ;
4
- use std:: collections:: HashMap ;
4
+ use std:: collections:: { HashMap , HashSet } ;
5
5
use std:: ffi:: OsStr ;
6
6
use std:: fs;
7
7
use std:: path:: Path ;
@@ -32,7 +32,7 @@ pub enum UpdateMode {
32
32
/// Panics if a file path could not read from or then written to
33
33
#[ allow( clippy:: too_many_lines) ]
34
34
pub fn run ( update_mode : UpdateMode ) {
35
- let ( lints, deprecated_lints) = gather_all ( ) ;
35
+ let ( lints, deprecated_lints, renamed_lints ) = gather_all ( ) ;
36
36
37
37
let internal_lints = Lint :: internal_lints ( & lints) ;
38
38
let usable_lints = Lint :: usable_lints ( & lints) ;
@@ -110,10 +110,13 @@ pub fn run(update_mode: UpdateMode) {
110
110
111
111
let content = gen_deprecated_lints_test ( & deprecated_lints) ;
112
112
process_file ( "tests/ui/deprecated.rs" , update_mode, & content) ;
113
+
114
+ let content = gen_renamed_lints_test ( & renamed_lints) ;
115
+ process_file ( "tests/ui/rename.rs" , update_mode, & content) ;
113
116
}
114
117
115
118
pub fn print_lints ( ) {
116
- let ( lint_list, _) = gather_all ( ) ;
119
+ let ( lint_list, _, _ ) = gather_all ( ) ;
117
120
let usable_lints = Lint :: usable_lints ( & lint_list) ;
118
121
let usable_lint_count = usable_lints. len ( ) ;
119
122
let grouped_by_lint_group = Lint :: by_lint_group ( usable_lints. into_iter ( ) ) ;
@@ -213,6 +216,19 @@ impl DeprecatedLint {
213
216
}
214
217
}
215
218
219
+ struct RenamedLint {
220
+ old_name : String ,
221
+ new_name : String ,
222
+ }
223
+ impl RenamedLint {
224
+ fn new ( old_name : & str , new_name : & str ) -> Self {
225
+ Self {
226
+ old_name : remove_line_splices ( old_name) ,
227
+ new_name : remove_line_splices ( new_name) ,
228
+ }
229
+ }
230
+ }
231
+
216
232
/// Generates the code for registering a group
217
233
fn gen_lint_group_list < ' a > ( group_name : & str , lints : impl Iterator < Item = & ' a Lint > ) -> String {
218
234
let mut details: Vec < _ > = lints. map ( |l| ( & l. module , l. name . to_uppercase ( ) ) ) . collect ( ) ;
@@ -288,10 +304,31 @@ fn gen_deprecated_lints_test(lints: &[DeprecatedLint]) -> String {
288
304
res
289
305
}
290
306
307
+ fn gen_renamed_lints_test ( lints : & [ RenamedLint ] ) -> String {
308
+ let mut seen_lints = HashSet :: new ( ) ;
309
+ let mut res: String = GENERATED_FILE_COMMENT . into ( ) ;
310
+ res. push_str ( "// run-rustfix\n \n " ) ;
311
+ for lint in lints {
312
+ if seen_lints. insert ( & lint. new_name ) {
313
+ writeln ! ( res, "#![allow({})]" , lint. new_name) . unwrap ( ) ;
314
+ }
315
+ }
316
+ seen_lints. clear ( ) ;
317
+ res. push ( '\n' ) ;
318
+ for lint in lints {
319
+ if seen_lints. insert ( & lint. old_name ) {
320
+ writeln ! ( res, "#![warn({})]" , lint. old_name) . unwrap ( ) ;
321
+ }
322
+ }
323
+ res. push_str ( "\n fn main() {}\n " ) ;
324
+ res
325
+ }
326
+
291
327
/// Gathers all lints defined in `clippy_lints/src`
292
- fn gather_all ( ) -> ( Vec < Lint > , Vec < DeprecatedLint > ) {
328
+ fn gather_all ( ) -> ( Vec < Lint > , Vec < DeprecatedLint > , Vec < RenamedLint > ) {
293
329
let mut lints = Vec :: with_capacity ( 1000 ) ;
294
330
let mut deprecated_lints = Vec :: with_capacity ( 50 ) ;
331
+ let mut renamed_lints = Vec :: with_capacity ( 50 ) ;
295
332
let root_path = clippy_project_root ( ) . join ( "clippy_lints/src" ) ;
296
333
297
334
for ( rel_path, file) in WalkDir :: new ( & root_path)
@@ -317,13 +354,13 @@ fn gather_all() -> (Vec<Lint>, Vec<DeprecatedLint>) {
317
354
module. strip_suffix ( ".rs" ) . unwrap_or ( & module)
318
355
} ;
319
356
320
- if module == "deprecated_lints" {
321
- parse_deprecated_contents ( & contents, & mut deprecated_lints) ;
322
- } else {
323
- parse_contents ( & contents, module, & mut lints) ;
357
+ match module {
358
+ "deprecated_lints" => parse_deprecated_contents ( & contents, & mut deprecated_lints) ,
359
+ "renamed_lints" => parse_renamed_contents ( & contents , & mut renamed_lints ) ,
360
+ _ => parse_contents ( & contents, module, & mut lints) ,
324
361
}
325
362
}
326
- ( lints, deprecated_lints)
363
+ ( lints, deprecated_lints, renamed_lints )
327
364
}
328
365
329
366
macro_rules! match_tokens {
@@ -406,6 +443,25 @@ fn parse_deprecated_contents(contents: &str, lints: &mut Vec<DeprecatedLint>) {
406
443
}
407
444
}
408
445
446
+ fn parse_renamed_contents ( contents : & str , lints : & mut Vec < RenamedLint > ) {
447
+ for line in contents. lines ( ) {
448
+ let mut offset = 0usize ;
449
+ let mut iter = tokenize ( line) . map ( |t| {
450
+ let range = offset..offset + t. len ;
451
+ offset = range. end ;
452
+ ( t. kind , & line[ range] )
453
+ } ) ;
454
+ let ( old_name, new_name) = match_tokens ! (
455
+ iter,
456
+ // ("old_name",
457
+ Whitespace OpenParen Literal { kind: LiteralKind :: Str { ..} , ..} ( old_name) Comma
458
+ // "new_name"),
459
+ Whitespace Literal { kind: LiteralKind :: Str { ..} , ..} ( new_name) CloseParen Comma
460
+ ) ;
461
+ lints. push ( RenamedLint :: new ( old_name, new_name) ) ;
462
+ }
463
+ }
464
+
409
465
/// Removes the line splices and surrounding quotes from a string literal
410
466
fn remove_line_splices ( s : & str ) -> String {
411
467
let s = s
0 commit comments