Skip to content

Commit ddc5b9e

Browse files
Add error codes duplicates check
1 parent 320ada6 commit ddc5b9e

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

src/tools/tidy/src/error_codes_check.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,42 @@ fn extract_error_codes_from_tests(f: &str, error_codes: &mut HashMap<String, boo
9393
}
9494
}
9595

96+
fn check_used_error_codes(
97+
file_path: &Path,
98+
f: &str,
99+
error_codes: &HashMap<String, bool>,
100+
errors: &mut usize,
101+
) {
102+
for (line_number, line) in f.lines().enumerate() {
103+
let s = line.trim();
104+
let c = if s.contains(" \"E0") {
105+
' '
106+
} else if s.contains("(\"E0") {
107+
'('
108+
} else {
109+
continue;
110+
};
111+
let parts = s.split(&format!("{}\"E0", c)).collect::<Vec<_>>();
112+
if let Some(err_code) = parts[1].split('"').next() {
113+
let err_code = format!("E0{}", err_code);
114+
if error_codes.get(&err_code).is_none() {
115+
eprintln!(
116+
"Error code `{}` used but hasn't been declared in `{}:{}`",
117+
err_code,
118+
file_path.display(),
119+
line_number + 1
120+
);
121+
*errors += 1;
122+
}
123+
}
124+
}
125+
}
126+
96127
pub fn check(path: &Path, bad: &mut bool) {
97128
println!("Checking which error codes lack tests...");
98129
let mut error_codes: HashMap<String, bool> = HashMap::new();
130+
let mut errors_count: usize = 0;
131+
99132
super::walk(path, &mut |path| super::filter_dirs(path), &mut |entry, contents| {
100133
let file_name = entry.file_name();
101134
if file_name == "error_codes.rs" {
@@ -106,6 +139,14 @@ pub fn check(path: &Path, bad: &mut bool) {
106139
});
107140
println!("Found {} error codes", error_codes.len());
108141

142+
super::walk(path, &mut |path| super::filter_dirs(path), &mut |entry, contents| {
143+
let file_name = entry.file_name();
144+
if entry.path().extension() == Some(OsStr::new("rs")) && file_name != "error_codes_check.rs"
145+
{
146+
check_used_error_codes(entry.path(), contents, &error_codes, &mut errors_count);
147+
}
148+
});
149+
109150
let mut errors = Vec::new();
110151
for (err_code, nb) in &error_codes {
111152
if !*nb && !WHITELIST.contains(&err_code.as_str()) {
@@ -117,7 +158,7 @@ pub fn check(path: &Path, bad: &mut bool) {
117158
eprintln!("{}", err);
118159
}
119160
println!("Found {} error codes with no tests", errors.len());
120-
if !errors.is_empty() {
161+
if !errors.is_empty() || errors_count != 0 {
121162
*bad = true;
122163
}
123164
println!("Done!");

0 commit comments

Comments
 (0)