1 //! Checks that all error codes have at least one test to prevent having error
2 //! codes that are silently not thrown by the compiler anymore.
4 use crate::walk::{filter_dirs, walk};
5 use std::collections::{HashMap, HashSet};
7 use std::fs::read_to_string;
12 // A few of those error codes can't be tested but all the others can and *should* be tested!
13 const EXEMPTED_FROM_TEST: &[&str] = &[
14 "E0313", "E0377", "E0461", "E0462", "E0465", "E0476", "E0490", "E0514", "E0519", "E0523",
15 "E0554", "E0640", "E0717", "E0729", "E0789",
18 // Some error codes don't have any tests apparently...
19 const IGNORE_EXPLANATION_CHECK: &[&str] = &["E0464", "E0570", "E0601", "E0602", "E0729"];
21 // If the file path contains any of these, we don't want to try to extract error codes from it.
23 // We need to declare each path in the windows version (with backslash).
24 const PATHS_TO_IGNORE_FOR_EXTRACTION: &[&str] =
25 &["src/test/", "src\\test\\", "src/doc/", "src\\doc\\", "src/tools/", "src\\tools\\"];
27 #[derive(Default, Debug)]
28 struct ErrorCodeStatus {
30 has_explanation: bool,
34 fn check_error_code_explanation(
36 error_codes: &mut HashMap<String, ErrorCodeStatus>,
39 let mut invalid_compile_fail_format = false;
40 let mut found_error_code = false;
42 for line in f.lines() {
44 if s.starts_with("```") {
45 if s.contains("compile_fail") && s.contains('E') {
46 if !found_error_code {
47 error_codes.get_mut(&err_code).map(|x| x.has_test = true);
48 found_error_code = true;
50 } else if s.contains("compile-fail") {
51 invalid_compile_fail_format = true;
53 } else if s.starts_with("#### Note: this error code is no longer emitted by the compiler") {
54 if !found_error_code {
55 error_codes.get_mut(&err_code).map(|x| x.has_test = true);
56 found_error_code = true;
60 invalid_compile_fail_format
63 fn check_if_error_code_is_test_in_explanation(f: &str, err_code: &str) -> bool {
64 let mut ignore_found = false;
66 for line in f.lines() {
68 if s.starts_with("#### Note: this error code is no longer emitted by the compiler") {
71 if s.starts_with("```") {
72 if s.contains("compile_fail") && s.contains(err_code) {
74 } else if s.contains("ignore") {
75 // It's very likely that we can't actually make it fail compilation...
83 macro_rules! some_or_continue {
92 fn extract_error_codes(
94 error_codes: &mut HashMap<String, ErrorCodeStatus>,
96 errors: &mut Vec<String>,
98 let mut reached_no_explanation = false;
100 for line in f.lines() {
102 if !reached_no_explanation && s.starts_with('E') && s.contains("include_str!(\"") {
107 "Expected a line with the format `E0xxx: include_str!(\"..\")`, but got {} \
108 without a `:` delimiter",
115 error_codes.entry(err_code.clone()).or_default().has_explanation = true;
117 // Now we extract the tests from the markdown file!
118 let md_file_name = match s.split_once("include_str!(\"") {
120 Some((_, md)) => match md.split_once("\")") {
122 Some((file_name, _)) => file_name,
125 let path = some_or_continue!(path.parent())
128 .expect("failed to canonicalize error explanation file path");
129 match read_to_string(&path) {
131 let has_test = check_if_error_code_is_test_in_explanation(&content, &err_code);
132 if !has_test && !IGNORE_EXPLANATION_CHECK.contains(&err_code.as_str()) {
134 "`{}` doesn't use its own error code in compile_fail example",
137 } else if has_test && IGNORE_EXPLANATION_CHECK.contains(&err_code.as_str()) {
139 "`{}` has a compile_fail example with its own error code, it shouldn't \
140 be listed in IGNORE_EXPLANATION_CHECK!",
144 if check_error_code_explanation(&content, error_codes, err_code) {
146 "`{}` uses invalid tag `compile-fail` instead of `compile_fail`",
152 eprintln!("Couldn't read `{}`: {}", path.display(), e);
155 } else if reached_no_explanation && s.starts_with('E') {
156 let err_code = match s.split_once(',') {
158 Some((err_code, _)) => err_code,
161 if !error_codes.contains_key(&err_code) {
162 // this check should *never* fail!
163 error_codes.insert(err_code, ErrorCodeStatus::default());
166 reached_no_explanation = true;
171 fn extract_error_codes_from_tests(f: &str, error_codes: &mut HashMap<String, ErrorCodeStatus>) {
172 for line in f.lines() {
174 if s.starts_with("error[E") || s.starts_with("warning[E") {
175 let err_code = match s.split_once(']') {
177 Some((err_code, _)) => match err_code.split_once('[') {
179 Some((_, err_code)) => err_code,
182 error_codes.entry(err_code.to_owned()).or_default().has_test = true;
187 fn extract_error_codes_from_source(
189 error_codes: &mut HashMap<String, ErrorCodeStatus>,
192 for line in f.lines() {
193 if line.trim_start().starts_with("//") {
196 for cap in regex.captures_iter(line) {
197 if let Some(error_code) = cap.get(1) {
198 error_codes.entry(error_code.as_str().to_owned()).or_default().is_used = true;
204 pub fn check(paths: &[&Path], bad: &mut bool) {
205 let mut errors = Vec::new();
206 let mut found_explanations = 0;
207 let mut found_tests = 0;
208 let mut error_codes: HashMap<String, ErrorCodeStatus> = HashMap::new();
209 let mut explanations: HashSet<String> = HashSet::new();
210 // We want error codes which match the following cases:
212 // * foo(a, E0111, a)
215 // * #[error = "E0111"]
216 let regex = Regex::new(r#"[(,"\s](E\d{4})[,)"]"#).unwrap();
218 println!("Checking which error codes lack tests...");
221 walk(path, &mut filter_dirs, &mut |entry, contents| {
222 let file_name = entry.file_name();
223 let entry_path = entry.path();
225 if file_name == "error_codes.rs" {
226 extract_error_codes(contents, &mut error_codes, entry.path(), &mut errors);
227 found_explanations += 1;
228 } else if entry_path.extension() == Some(OsStr::new("stderr")) {
229 extract_error_codes_from_tests(contents, &mut error_codes);
231 } else if entry_path.extension() == Some(OsStr::new("rs")) {
232 let path = entry.path().to_string_lossy();
233 if PATHS_TO_IGNORE_FOR_EXTRACTION.iter().all(|c| !path.contains(c)) {
234 extract_error_codes_from_source(contents, &mut error_codes, ®ex);
238 .and_then(|p| p.file_name())
239 .map(|p| p == "error_codes")
241 && entry_path.extension() == Some(OsStr::new("md"))
243 explanations.insert(file_name.to_str().unwrap().replace(".md", ""));
247 if found_explanations == 0 {
248 eprintln!("No error code explanation was tested!");
251 if found_tests == 0 {
252 eprintln!("No error code was found in compilation errors!");
255 if explanations.is_empty() {
256 eprintln!("No error code explanation was found!");
259 if errors.is_empty() {
260 println!("Found {} error codes", error_codes.len());
262 for (err_code, error_status) in &error_codes {
263 if !error_status.has_test && !EXEMPTED_FROM_TEST.contains(&err_code.as_str()) {
264 errors.push(format!("Error code {err_code} needs to have at least one UI test!"));
265 } else if error_status.has_test && EXEMPTED_FROM_TEST.contains(&err_code.as_str()) {
267 "Error code {} has a UI test, it shouldn't be listed into EXEMPTED_FROM_TEST!",
271 if !error_status.is_used && !error_status.has_explanation {
273 "Error code {} isn't used and doesn't have an error explanation, it should be \
274 commented in error_codes.rs file",
280 if errors.is_empty() {
281 // Checking if local constants need to be cleaned.
282 for err_code in EXEMPTED_FROM_TEST {
283 match error_codes.get(err_code.to_owned()) {
287 "{} error code has a test and therefore should be \
288 removed from the `EXEMPTED_FROM_TEST` constant",
293 None => errors.push(format!(
294 "{} error code isn't used anymore and therefore should be removed \
295 from `EXEMPTED_FROM_TEST` constant",
301 if errors.is_empty() {
302 for explanation in explanations {
303 if !error_codes.contains_key(&explanation) {
305 "{} error code explanation should be listed in `error_codes.rs`",
315 println!("Found {} error(s) in error codes", errors.len());
316 if !errors.is_empty() {