@@ -62,110 +62,89 @@ impl Lint {
62
62
}
63
63
64
64
/// Returns all non-deprecated lints and non-internal lints
65
- pub fn usable_lints ( lints : impl Iterator < Item = Self > ) -> impl Iterator < Item = Self > {
66
- lints. filter ( |l| l. deprecation . is_none ( ) && !l. is_internal ( ) )
65
+ #[ must_use]
66
+ pub fn usable_lints ( lints : & [ Self ] ) -> Vec < Self > {
67
+ lints
68
+ . iter ( )
69
+ . filter ( |l| l. deprecation . is_none ( ) && !l. group . starts_with ( "internal" ) )
70
+ . cloned ( )
71
+ . collect ( )
67
72
}
68
73
69
74
/// Returns all internal lints (not `internal_warn` lints)
70
- pub fn internal_lints ( lints : impl Iterator < Item = Self > ) -> impl Iterator < Item = Self > {
71
- lints. filter ( |l| l. group == "internal" )
75
+ #[ must_use]
76
+ pub fn internal_lints ( lints : & [ Self ] ) -> Vec < Self > {
77
+ lints. iter ( ) . filter ( |l| l. group == "internal" ) . cloned ( ) . collect ( )
72
78
}
73
79
74
- /// Returns the lints in a `HashMap`, grouped by the different lint groups
80
+ /// Returns all deprecated lints
75
81
#[ must_use]
76
- pub fn by_lint_group ( lints : impl Iterator < Item = Self > ) -> HashMap < String , Vec < Self > > {
77
- lints. map ( |lint| ( lint . group . to_string ( ) , lint ) ) . into_group_map ( )
82
+ pub fn deprecated_lints ( lints : & [ Self ] ) -> Vec < Self > {
83
+ lints. iter ( ) . filter ( |l| l . deprecation . is_some ( ) ) . cloned ( ) . collect ( )
78
84
}
79
85
86
+ /// Returns the lints in a `HashMap`, grouped by the different lint groups
80
87
#[ must_use]
81
- pub fn is_internal ( & self ) -> bool {
82
- self . group . starts_with ( "internal" )
88
+ pub fn by_lint_group ( lints : impl Iterator < Item = Self > ) -> HashMap < String , Vec < Self > > {
89
+ lints . map ( |lint| ( lint . group . to_string ( ) , lint ) ) . into_group_map ( )
83
90
}
84
91
}
85
92
86
93
/// Generates the Vec items for `register_lint_group` calls in `clippy_lints/src/lib.rs`.
87
94
#[ must_use]
88
- pub fn gen_lint_group_list ( lints : Vec < Lint > ) -> Vec < String > {
95
+ pub fn gen_lint_group_list < ' a > ( lints : impl Iterator < Item = & ' a Lint > ) -> Vec < String > {
89
96
lints
90
- . into_iter ( )
91
- . filter_map ( |l| {
92
- if l. deprecation . is_some ( ) {
93
- None
94
- } else {
95
- Some ( format ! ( " LintId::of(&{}::{})," , l. module, l. name. to_uppercase( ) ) )
96
- }
97
- } )
97
+ . map ( |l| format ! ( " LintId::of(&{}::{})," , l. module, l. name. to_uppercase( ) ) )
98
98
. sorted ( )
99
99
. collect :: < Vec < String > > ( )
100
100
}
101
101
102
102
/// Generates the `pub mod module_name` list in `clippy_lints/src/lib.rs`.
103
103
#[ must_use]
104
- pub fn gen_modules_list ( lints : Vec < Lint > ) -> Vec < String > {
104
+ pub fn gen_modules_list < ' a > ( lints : impl Iterator < Item = & ' a Lint > ) -> Vec < String > {
105
105
lints
106
- . into_iter ( )
107
- . filter_map ( |l| {
108
- if l. is_internal ( ) || l. deprecation . is_some ( ) {
109
- None
110
- } else {
111
- Some ( l. module )
112
- }
113
- } )
106
+ . map ( |l| & l. module )
114
107
. unique ( )
115
- . map ( |module| format ! ( "pub mod {};" , module) )
108
+ . map ( |module| format ! ( "mod {};" , module) )
116
109
. sorted ( )
117
110
. collect :: < Vec < String > > ( )
118
111
}
119
112
120
113
/// Generates the list of lint links at the bottom of the README
121
114
#[ must_use]
122
- pub fn gen_changelog_lint_list ( lints : Vec < Lint > ) -> Vec < String > {
123
- let mut lint_list_sorted: Vec < Lint > = lints;
124
- lint_list_sorted. sort_by_key ( |l| l. name . clone ( ) ) ;
125
- lint_list_sorted
126
- . iter ( )
127
- . filter_map ( |l| {
128
- if l. is_internal ( ) {
129
- None
130
- } else {
131
- Some ( format ! ( "[`{}`]: {}#{}" , l. name, DOCS_LINK , l. name) )
132
- }
133
- } )
115
+ pub fn gen_changelog_lint_list < ' a > ( lints : impl Iterator < Item = & ' a Lint > ) -> Vec < String > {
116
+ lints
117
+ . sorted_by_key ( |l| & l. name )
118
+ . map ( |l| format ! ( "[`{}`]: {}#{}" , l. name, DOCS_LINK , l. name) )
134
119
. collect ( )
135
120
}
136
121
137
122
/// Generates the `register_removed` code in `./clippy_lints/src/lib.rs`.
138
123
#[ must_use]
139
- pub fn gen_deprecated ( lints : & [ Lint ] ) -> Vec < String > {
124
+ pub fn gen_deprecated < ' a > ( lints : impl Iterator < Item = & ' a Lint > ) -> Vec < String > {
140
125
lints
141
- . iter ( )
142
- . filter_map ( |l| {
143
- l. clone ( ) . deprecation . map ( |depr_text| {
144
- vec ! [
145
- " store.register_removed(" . to_string( ) ,
146
- format!( " \" clippy::{}\" ," , l. name) ,
147
- format!( " \" {}\" ," , depr_text) ,
148
- " );" . to_string( ) ,
149
- ]
150
- } )
126
+ . flat_map ( |l| {
127
+ l. deprecation
128
+ . clone ( )
129
+ . map ( |depr_text| {
130
+ vec ! [
131
+ " store.register_removed(" . to_string( ) ,
132
+ format!( " \" clippy::{}\" ," , l. name) ,
133
+ format!( " \" {}\" ," , depr_text) ,
134
+ " );" . to_string( ) ,
135
+ ]
136
+ } )
137
+ . expect ( "only deprecated lints should be passed" )
151
138
} )
152
- . flatten ( )
153
139
. collect :: < Vec < String > > ( )
154
140
}
155
141
156
142
#[ must_use]
157
- pub fn gen_register_lint_list ( lints : & [ Lint ] ) -> Vec < String > {
143
+ pub fn gen_register_lint_list < ' a > ( lints : impl Iterator < Item = & ' a Lint > ) -> Vec < String > {
158
144
let pre = " store.register_lints(&[" . to_string ( ) ;
159
145
let post = " ]);" . to_string ( ) ;
160
146
let mut inner = lints
161
- . iter ( )
162
- . filter_map ( |l| {
163
- if !l. is_internal ( ) && l. deprecation . is_none ( ) {
164
- Some ( format ! ( " &{}::{}," , l. module, l. name. to_uppercase( ) ) )
165
- } else {
166
- None
167
- }
168
- } )
147
+ . map ( |l| format ! ( " &{}::{}," , l. module, l. name. to_uppercase( ) ) )
169
148
. sorted ( )
170
149
. collect :: < Vec < String > > ( ) ;
171
150
inner. insert ( 0 , pre) ;
@@ -439,7 +418,7 @@ fn test_usable_lints() {
439
418
None ,
440
419
"module_name" ,
441
420
) ] ;
442
- assert_eq ! ( expected, Lint :: usable_lints( lints. into_iter ( ) ) . collect :: < Vec < Lint >> ( ) ) ;
421
+ assert_eq ! ( expected, Lint :: usable_lints( & lints) ) ;
443
422
}
444
423
445
424
#[ test]
@@ -469,13 +448,12 @@ fn test_gen_changelog_lint_list() {
469
448
let lints = vec ! [
470
449
Lint :: new( "should_assert_eq" , "group1" , "abc" , None , "module_name" ) ,
471
450
Lint :: new( "should_assert_eq2" , "group2" , "abc" , None , "module_name" ) ,
472
- Lint :: new( "incorrect_internal" , "internal_style" , "abc" , None , "module_name" ) ,
473
451
] ;
474
452
let expected = vec ! [
475
453
format!( "[`should_assert_eq`]: {}#should_assert_eq" , DOCS_LINK . to_string( ) ) ,
476
454
format!( "[`should_assert_eq2`]: {}#should_assert_eq2" , DOCS_LINK . to_string( ) ) ,
477
455
] ;
478
- assert_eq ! ( expected, gen_changelog_lint_list( lints) ) ;
456
+ assert_eq ! ( expected, gen_changelog_lint_list( lints. iter ( ) ) ) ;
479
457
}
480
458
481
459
#[ test]
@@ -495,7 +473,6 @@ fn test_gen_deprecated() {
495
473
Some ( "will be removed" ) ,
496
474
"module_name" ,
497
475
) ,
498
- Lint :: new( "should_assert_eq2" , "group2" , "abc" , None , "module_name" ) ,
499
476
] ;
500
477
let expected: Vec < String > = vec ! [
501
478
" store.register_removed(" ,
@@ -510,36 +487,37 @@ fn test_gen_deprecated() {
510
487
. into_iter ( )
511
488
. map ( String :: from)
512
489
. collect ( ) ;
513
- assert_eq ! ( expected, gen_deprecated( & lints) ) ;
490
+ assert_eq ! ( expected, gen_deprecated( lints. iter( ) ) ) ;
491
+ }
492
+
493
+ #[ test]
494
+ #[ should_panic]
495
+ fn test_gen_deprecated_fail ( ) {
496
+ let lints = vec ! [ Lint :: new( "should_assert_eq2" , "group2" , "abc" , None , "module_name" ) ] ;
497
+ let _ = gen_deprecated ( lints. iter ( ) ) ;
514
498
}
515
499
516
500
#[ test]
517
501
fn test_gen_modules_list ( ) {
518
502
let lints = vec ! [
519
503
Lint :: new( "should_assert_eq" , "group1" , "abc" , None , "module_name" ) ,
520
- Lint :: new( "should_assert_eq2" , "group2" , "abc" , Some ( "abc" ) , "deprecated" ) ,
521
504
Lint :: new( "incorrect_stuff" , "group3" , "abc" , None , "another_module" ) ,
522
- Lint :: new( "incorrect_internal" , "internal_style" , "abc" , None , "module_name" ) ,
523
- ] ;
524
- let expected = vec ! [
525
- "pub mod another_module;" . to_string( ) ,
526
- "pub mod module_name;" . to_string( ) ,
527
505
] ;
528
- assert_eq ! ( expected, gen_modules_list( lints) ) ;
506
+ let expected = vec ! [ "mod another_module;" . to_string( ) , "mod module_name;" . to_string( ) ] ;
507
+ assert_eq ! ( expected, gen_modules_list( lints. iter( ) ) ) ;
529
508
}
530
509
531
510
#[ test]
532
511
fn test_gen_lint_group_list ( ) {
533
512
let lints = vec ! [
534
513
Lint :: new( "abc" , "group1" , "abc" , None , "module_name" ) ,
535
514
Lint :: new( "should_assert_eq" , "group1" , "abc" , None , "module_name" ) ,
536
- Lint :: new( "should_assert_eq2" , "group2" , "abc" , Some ( "abc" ) , "deprecated" ) ,
537
515
Lint :: new( "internal" , "internal_style" , "abc" , None , "module_name" ) ,
538
516
] ;
539
517
let expected = vec ! [
540
518
" LintId::of(&module_name::ABC)," . to_string( ) ,
541
519
" LintId::of(&module_name::INTERNAL)," . to_string( ) ,
542
520
" LintId::of(&module_name::SHOULD_ASSERT_EQ)," . to_string( ) ,
543
521
] ;
544
- assert_eq ! ( expected, gen_lint_group_list( lints) ) ;
522
+ assert_eq ! ( expected, gen_lint_group_list( lints. iter ( ) ) ) ;
545
523
}
0 commit comments