1
1
//! Checks that all error codes have at least one test to prevent having error
2
2
//! codes that are silently not thrown by the compiler anymore.
3
3
4
- use std:: collections:: HashMap ;
4
+ use std:: collections:: { HashMap , HashSet } ;
5
5
use std:: ffi:: OsStr ;
6
6
use std:: fs:: read_to_string;
7
7
use std:: path:: Path ;
@@ -205,6 +205,7 @@ pub fn check(paths: &[&Path], bad: &mut bool) {
205
205
let mut found_explanations = 0 ;
206
206
let mut found_tests = 0 ;
207
207
let mut error_codes: HashMap < String , ErrorCodeStatus > = HashMap :: new ( ) ;
208
+ let mut explanations: HashSet < String > = HashSet :: new ( ) ;
208
209
// We want error codes which match the following cases:
209
210
//
210
211
// * foo(a, E0111, a)
@@ -218,17 +219,27 @@ pub fn check(paths: &[&Path], bad: &mut bool) {
218
219
for path in paths {
219
220
super :: walk ( path, & mut |path| super :: filter_dirs ( path) , & mut |entry, contents| {
220
221
let file_name = entry. file_name ( ) ;
222
+ let entry_path = entry. path ( ) ;
223
+
221
224
if file_name == "error_codes.rs" {
222
225
extract_error_codes ( contents, & mut error_codes, entry. path ( ) , & mut errors) ;
223
226
found_explanations += 1 ;
224
- } else if entry . path ( ) . extension ( ) == Some ( OsStr :: new ( "stderr" ) ) {
227
+ } else if entry_path . extension ( ) == Some ( OsStr :: new ( "stderr" ) ) {
225
228
extract_error_codes_from_tests ( contents, & mut error_codes) ;
226
229
found_tests += 1 ;
227
- } else if entry . path ( ) . extension ( ) == Some ( OsStr :: new ( "rs" ) ) {
230
+ } else if entry_path . extension ( ) == Some ( OsStr :: new ( "rs" ) ) {
228
231
let path = entry. path ( ) . to_string_lossy ( ) ;
229
232
if PATHS_TO_IGNORE_FOR_EXTRACTION . iter ( ) . all ( |c| !path. contains ( c) ) {
230
233
extract_error_codes_from_source ( contents, & mut error_codes, & regex) ;
231
234
}
235
+ } else if entry_path
236
+ . parent ( )
237
+ . and_then ( |p| p. file_name ( ) )
238
+ . map ( |p| p == "error_codes" )
239
+ . unwrap_or ( false )
240
+ && entry_path. extension ( ) == Some ( OsStr :: new ( "md" ) )
241
+ {
242
+ explanations. insert ( file_name. to_str ( ) . unwrap ( ) . replace ( ".md" , "" ) ) ;
232
243
}
233
244
} ) ;
234
245
}
@@ -240,6 +251,10 @@ pub fn check(paths: &[&Path], bad: &mut bool) {
240
251
eprintln ! ( "No error code was found in compilation errors!" ) ;
241
252
* bad = true ;
242
253
}
254
+ if explanations. is_empty ( ) {
255
+ eprintln ! ( "No error code explanation was found!" ) ;
256
+ * bad = true ;
257
+ }
243
258
if errors. is_empty ( ) {
244
259
println ! ( "Found {} error codes" , error_codes. len( ) ) ;
245
260
@@ -282,11 +297,21 @@ pub fn check(paths: &[&Path], bad: &mut bool) {
282
297
}
283
298
}
284
299
}
300
+ if errors. is_empty ( ) {
301
+ for explanation in explanations {
302
+ if !error_codes. contains_key ( & explanation) {
303
+ errors. push ( format ! (
304
+ "{} error code explanation should be listed in `error_codes.rs`" ,
305
+ explanation
306
+ ) ) ;
307
+ }
308
+ }
309
+ }
285
310
errors. sort ( ) ;
286
311
for err in & errors {
287
312
eprintln ! ( "{}" , err) ;
288
313
}
289
- println ! ( "Found {} error codes with no tests " , errors. len( ) ) ;
314
+ println ! ( "Found {} error(s) in error codes " , errors. len( ) ) ;
290
315
if !errors. is_empty ( ) {
291
316
* bad = true ;
292
317
}
0 commit comments