1 //! Tidy check to verify the validity of long error diagnostic codes.
3 //! This ensures that error codes are used at most once and also prints out some
4 //! statistics about the error codes.
6 use std::collections::HashMap;
9 pub fn check(path: &Path, bad: &mut bool) {
10 let mut map: HashMap<_, Vec<_>> = HashMap::new();
13 &mut |path| super::filter_dirs(path) || path.ends_with("src/test"),
14 &mut |entry, contents| {
15 let file = entry.path();
16 let filename = file.file_name().unwrap().to_string_lossy();
17 if filename != "error_codes.rs" {
21 // In the `register_long_diagnostics!` macro, entries look like this:
25 // <Long diagnostic message>
29 // and these long messages often have error codes themselves inside
30 // them, but we don't want to report duplicates in these cases. This
31 // variable keeps track of whether we're currently inside one of these
32 // long diagnostic messages.
33 let mut inside_long_diag = false;
34 for (num, line) in contents.lines().enumerate() {
36 inside_long_diag = !line.contains("\"##");
40 let mut search = line;
41 while let Some(i) = search.find('E') {
42 search = &search[i + 1..];
43 let code = if search.len() > 4 { search[..4].parse::<u32>() } else { continue };
44 let code = match code {
48 map.entry(code).or_default().push((file.to_owned(), num + 1, line.to_owned()));
52 inside_long_diag = line.contains("r##\"");
58 for (&code, entries) in map.iter() {
62 if entries.len() == 1 {
66 tidy_error!(bad, "duplicate error code: {}", code);
67 for &(ref file, line_num, ref line) in entries.iter() {
68 tidy_error!(bad, "{}:{}: {}", file.display(), line_num, line);
73 println!("* {} error codes", map.len());
74 println!("* highest error code: E{:04}", max);