Skip to content

Commit 7f9a4af

Browse files
committed
Make tidy error code parsing robust against comments
1 parent 958a022 commit 7f9a4af

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

src/tools/tidy/src/error_codes.rs

+30-13
Original file line numberDiff line numberDiff line change
@@ -71,50 +71,67 @@ fn extract_error_codes(root_path: &Path, errors: &mut Vec<String>) -> Vec<String
7171
let path = root_path.join(Path::new(ERROR_CODES_PATH));
7272
let file =
7373
fs::read_to_string(&path).unwrap_or_else(|e| panic!("failed to read `{path:?}`: {e}"));
74+
let path = path.display();
7475

7576
let mut error_codes = Vec::new();
7677

77-
for line in file.lines() {
78+
for (line_index, line) in file.lines().enumerate() {
79+
let line_index = line_index + 1;
7880
let line = line.trim();
7981

8082
if line.starts_with('E') {
8183
let split_line = line.split_once(':');
8284

8385
// Extract the error code from the line. Emit a fatal error if it is not in the correct
8486
// format.
85-
let err_code = if let Some(err_code) = split_line {
86-
err_code.0.to_owned()
87-
} else {
87+
let Some(split_line) = split_line else {
8888
errors.push(format!(
89-
"Expected a line with the format `Eabcd: abcd, \
89+
"{path}:{line_index}: Expected a line with the format `Eabcd: abcd, \
9090
but got \"{}\" without a `:` delimiter",
9191
line,
9292
));
9393
continue;
9494
};
9595

96+
let err_code = split_line.0.to_owned();
97+
9698
// If this is a duplicate of another error code, emit a fatal error.
9799
if error_codes.contains(&err_code) {
98-
errors.push(format!("Found duplicate error code: `{}`", err_code));
100+
errors.push(format!(
101+
"{path}:{line_index}: Found duplicate error code: `{}`",
102+
err_code
103+
));
99104
continue;
100105
}
101106

102107
let mut chars = err_code.chars();
103-
chars.next();
108+
assert_eq!(chars.next(), Some('E'));
104109
let error_num_as_str = chars.as_str();
105110

106111
// Ensure that the line references the correct markdown file.
107-
let expected_filename = format!(" {},", error_num_as_str);
108-
if expected_filename != split_line.unwrap().1 {
112+
let rest = split_line.1.split_once(',');
113+
let Some(rest) = rest else {
114+
errors.push(format!(
115+
"{path}:{line_index}: Expected a line with the format `Eabcd: abcd, \
116+
but got \"{}\" without a `,` delimiter",
117+
line,
118+
));
119+
continue;
120+
};
121+
if error_num_as_str != rest.0.trim() {
109122
errors.push(format!(
110-
"`{}:` should be followed by `{}` but instead found `{}` in \
123+
"{path}:{line_index}: `{}:` should be followed by `{},` but instead found `{}` in \
111124
`compiler/rustc_error_codes/src/lib.rs`",
112125
err_code,
113-
expected_filename,
114-
split_line.unwrap().1,
126+
error_num_as_str,
127+
split_line.1,
115128
));
116129
continue;
117130
}
131+
if !rest.1.trim().is_empty() && !rest.1.trim().starts_with("//") {
132+
errors.push(format!("{path}:{line_index}: should only have one error per line"));
133+
continue;
134+
}
118135

119136
error_codes.push(err_code);
120137
}
@@ -146,7 +163,7 @@ fn check_error_codes_docs(
146163
return;
147164
}
148165

149-
// Make sure that the file is referenced in `error_codes.rs`
166+
// Make sure that the file is referenced in `rustc_error_codes/src/lib.rs`
150167
let filename = path.file_name().unwrap().to_str().unwrap().split_once('.');
151168
let err_code = filename.unwrap().0; // `unwrap` is ok because we know the filename is in the correct format.
152169

0 commit comments

Comments
 (0)