@@ -93,10 +93,41 @@ fn extract_error_codes_from_tests(f: &str, error_codes: &mut HashMap<String, boo
9393 }
9494}
9595
96+ fn check_used_error_codes (
97+ file_path : & Path ,
98+ f : & str ,
99+ error_codes : & HashMap < String , bool > ,
100+ errors : & mut usize ,
101+ ) {
102+ for ( line_number, line) in f. lines ( ) . enumerate ( ) {
103+ let s = line. trim ( ) ;
104+ let c = if s. contains ( " \" E0" ) {
105+ ' '
106+ } else if s. contains ( "(\" E0" ) {
107+ '('
108+ } else {
109+ continue
110+ } ;
111+ let parts = s. split ( & format ! ( "{}\" E0" , c) ) . collect :: < Vec < _ > > ( ) ;
112+ if let Some ( err_code) = parts[ 1 ] . split ( '"' ) . next ( ) {
113+ let err_code = format ! ( "E0{}" , err_code) ;
114+ if error_codes. get ( & err_code) . is_none ( ) {
115+ eprintln ! ( "Error code `{}` used but hasn't been declared in `{}:{}`" ,
116+ err_code, file_path. display( ) , line_number + 1 ) ;
117+ * errors += 1 ;
118+ }
119+ }
120+ }
121+ }
122+
96123pub fn check ( path : & Path , bad : & mut bool ) {
97124 println ! ( "Checking which error codes lack tests..." ) ;
98125 let mut error_codes: HashMap < String , bool > = HashMap :: new ( ) ;
99- super :: walk ( path, & mut |path| super :: filter_dirs ( path) , & mut |entry, contents| {
126+ let mut errors_count: usize = 0 ;
127+
128+ super :: walk ( path,
129+ & mut |path| super :: filter_dirs ( path) ,
130+ & mut |entry, contents| {
100131 let file_name = entry. file_name ( ) ;
101132 if file_name == "error_codes.rs" {
102133 extract_error_codes ( contents, & mut error_codes, entry. path ( ) ) ;
@@ -106,6 +137,16 @@ pub fn check(path: &Path, bad: &mut bool) {
106137 } ) ;
107138 println ! ( "Found {} error codes" , error_codes. len( ) ) ;
108139
140+ super :: walk ( path,
141+ & mut |path| super :: filter_dirs ( path) ,
142+ & mut |entry, contents| {
143+ let file_name = entry. file_name ( ) ;
144+ if entry. path ( ) . extension ( ) == Some ( OsStr :: new ( "rs" ) )
145+ && file_name != "error_codes_check.rs" {
146+ check_used_error_codes ( entry. path ( ) , contents, & error_codes, & mut errors_count) ;
147+ }
148+ } ) ;
149+
109150 let mut errors = Vec :: new ( ) ;
110151 for ( err_code, nb) in & error_codes {
111152 if !* nb && !WHITELIST . contains ( & err_code. as_str ( ) ) {
@@ -117,7 +158,7 @@ pub fn check(path: &Path, bad: &mut bool) {
117158 eprintln ! ( "{}" , err) ;
118159 }
119160 println ! ( "Found {} error codes with no tests" , errors. len( ) ) ;
120- if !errors. is_empty ( ) {
161+ if !errors. is_empty ( ) || errors_count != 0 {
121162 * bad = true ;
122163 }
123164 println ! ( "Done!" ) ;
0 commit comments