@@ -62,110 +62,89 @@ impl Lint {
6262 }
6363
6464 /// 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 ( )
6772 }
6873
6974 /// 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 ( )
7278 }
7379
74- /// Returns the lints in a `HashMap`, grouped by the different lint groups
80+ /// Returns all deprecated lints
7581 #[ 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 ( )
7884 }
7985
86+ /// Returns the lints in a `HashMap`, grouped by the different lint groups
8087 #[ 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 ( )
8390 }
8491}
8592
8693/// Generates the Vec items for `register_lint_group` calls in `clippy_lints/src/lib.rs`.
8794#[ 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 > {
8996 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( ) ) )
9898 . sorted ( )
9999 . collect :: < Vec < String > > ( )
100100}
101101
102102/// Generates the `pub mod module_name` list in `clippy_lints/src/lib.rs`.
103103#[ 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 > {
105105 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 )
114107 . unique ( )
115- . map ( |module| format ! ( "pub mod {};" , module) )
108+ . map ( |module| format ! ( "mod {};" , module) )
116109 . sorted ( )
117110 . collect :: < Vec < String > > ( )
118111}
119112
120113/// Generates the list of lint links at the bottom of the README
121114#[ 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) )
134119 . collect ( )
135120}
136121
137122/// Generates the `register_removed` code in `./clippy_lints/src/lib.rs`.
138123#[ 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 > {
140125 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" )
151138 } )
152- . flatten ( )
153139 . collect :: < Vec < String > > ( )
154140}
155141
156142#[ 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 > {
158144 let pre = " store.register_lints(&[" . to_string ( ) ;
159145 let post = " ]);" . to_string ( ) ;
160146 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( ) ) )
169148 . sorted ( )
170149 . collect :: < Vec < String > > ( ) ;
171150 inner. insert ( 0 , pre) ;
@@ -439,7 +418,7 @@ fn test_usable_lints() {
439418 None ,
440419 "module_name" ,
441420 ) ] ;
442- assert_eq ! ( expected, Lint :: usable_lints( lints. into_iter ( ) ) . collect :: < Vec < Lint >> ( ) ) ;
421+ assert_eq ! ( expected, Lint :: usable_lints( & lints) ) ;
443422}
444423
445424#[ test]
@@ -469,13 +448,12 @@ fn test_gen_changelog_lint_list() {
469448 let lints = vec ! [
470449 Lint :: new( "should_assert_eq" , "group1" , "abc" , None , "module_name" ) ,
471450 Lint :: new( "should_assert_eq2" , "group2" , "abc" , None , "module_name" ) ,
472- Lint :: new( "incorrect_internal" , "internal_style" , "abc" , None , "module_name" ) ,
473451 ] ;
474452 let expected = vec ! [
475453 format!( "[`should_assert_eq`]: {}#should_assert_eq" , DOCS_LINK . to_string( ) ) ,
476454 format!( "[`should_assert_eq2`]: {}#should_assert_eq2" , DOCS_LINK . to_string( ) ) ,
477455 ] ;
478- assert_eq ! ( expected, gen_changelog_lint_list( lints) ) ;
456+ assert_eq ! ( expected, gen_changelog_lint_list( lints. iter ( ) ) ) ;
479457}
480458
481459#[ test]
@@ -495,7 +473,6 @@ fn test_gen_deprecated() {
495473 Some ( "will be removed" ) ,
496474 "module_name" ,
497475 ) ,
498- Lint :: new( "should_assert_eq2" , "group2" , "abc" , None , "module_name" ) ,
499476 ] ;
500477 let expected: Vec < String > = vec ! [
501478 " store.register_removed(" ,
@@ -510,36 +487,37 @@ fn test_gen_deprecated() {
510487 . into_iter ( )
511488 . map ( String :: from)
512489 . 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 ( ) ) ;
514498}
515499
516500#[ test]
517501fn test_gen_modules_list ( ) {
518502 let lints = vec ! [
519503 Lint :: new( "should_assert_eq" , "group1" , "abc" , None , "module_name" ) ,
520- Lint :: new( "should_assert_eq2" , "group2" , "abc" , Some ( "abc" ) , "deprecated" ) ,
521504 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( ) ,
527505 ] ;
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( ) ) ) ;
529508}
530509
531510#[ test]
532511fn test_gen_lint_group_list ( ) {
533512 let lints = vec ! [
534513 Lint :: new( "abc" , "group1" , "abc" , None , "module_name" ) ,
535514 Lint :: new( "should_assert_eq" , "group1" , "abc" , None , "module_name" ) ,
536- Lint :: new( "should_assert_eq2" , "group2" , "abc" , Some ( "abc" ) , "deprecated" ) ,
537515 Lint :: new( "internal" , "internal_style" , "abc" , None , "module_name" ) ,
538516 ] ;
539517 let expected = vec ! [
540518 " LintId::of(&module_name::ABC)," . to_string( ) ,
541519 " LintId::of(&module_name::INTERNAL)," . to_string( ) ,
542520 " LintId::of(&module_name::SHOULD_ASSERT_EQ)," . to_string( ) ,
543521 ] ;
544- assert_eq ! ( expected, gen_lint_group_list( lints) ) ;
522+ assert_eq ! ( expected, gen_lint_group_list( lints. iter ( ) ) ) ;
545523}
0 commit comments