]> git.lizzy.rs Git - rust.git/blob - src/tools/tidy/src/errors.rs
Auto merge of #93176 - danielhenrymantilla:stack-pinning-macro, r=m-ou-se
[rust.git] / src / tools / tidy / src / errors.rs
1 //! Tidy check to verify the validity of long error diagnostic codes.
2 //!
3 //! This ensures that error codes are used at most once and also prints out some
4 //! statistics about the error codes.
5
6 use std::collections::HashMap;
7 use std::path::Path;
8
9 pub fn check(path: &Path, bad: &mut bool) {
10     let mut map: HashMap<_, Vec<_>> = HashMap::new();
11     super::walk(
12         path,
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" {
18                 return;
19             }
20
21             // In the `register_long_diagnostics!` macro, entries look like this:
22             //
23             // ```
24             // EXXXX: r##"
25             // <Long diagnostic message>
26             // "##,
27             // ```
28             //
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() {
35                 if inside_long_diag {
36                     inside_long_diag = !line.contains("\"##");
37                     continue;
38                 }
39
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 {
45                         Ok(n) => n,
46                         Err(..) => continue,
47                     };
48                     map.entry(code).or_default().push((file.to_owned(), num + 1, line.to_owned()));
49                     break;
50                 }
51
52                 inside_long_diag = line.contains("r##\"");
53             }
54         },
55     );
56
57     let mut max = 0;
58     for (&code, entries) in map.iter() {
59         if code > max {
60             max = code;
61         }
62         if entries.len() == 1 {
63             continue;
64         }
65
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);
69         }
70     }
71
72     if !*bad {
73         println!("* {} error codes", map.len());
74         println!("* highest error code: E{:04}", max);
75     }
76 }