Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error codes duplicates check #68639

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/tools/tidy/src/error_codes_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,42 @@ fn extract_error_codes_from_tests(f: &str, error_codes: &mut HashMap<String, boo
}
}

fn check_used_error_codes(
file_path: &Path,
f: &str,
error_codes: &HashMap<String, bool>,
errors: &mut usize,
) {
for (line_number, line) in f.lines().enumerate() {
let s = line.trim();
let c = if s.contains(" \"E0") {
' '
} else if s.contains("(\"E0") {
'('
} else {
continue;
};
let parts = s.split(&format!("{}\"E0", c)).collect::<Vec<_>>();
if let Some(err_code) = parts[1].split('"').next() {
let err_code = format!("E0{}", err_code);
if error_codes.get(&err_code).is_none() {
eprintln!(
"Error code `{}` used but hasn't been declared in `{}:{}`",
err_code,
file_path.display(),
line_number + 1
);
*errors += 1;
}
}
}
}

pub fn check(path: &Path, bad: &mut bool) {
println!("Checking which error codes lack tests...");
let mut error_codes: HashMap<String, bool> = HashMap::new();
let mut errors_count: usize = 0;

super::walk(path, &mut |path| super::filter_dirs(path), &mut |entry, contents| {
let file_name = entry.file_name();
if file_name == "error_codes.rs" {
Expand All @@ -106,6 +139,14 @@ pub fn check(path: &Path, bad: &mut bool) {
});
println!("Found {} error codes", error_codes.len());

super::walk(path, &mut |path| super::filter_dirs(path), &mut |entry, contents| {
let file_name = entry.file_name();
if entry.path().extension() == Some(OsStr::new("rs")) && file_name != "error_codes_check.rs"
{
check_used_error_codes(entry.path(), contents, &error_codes, &mut errors_count);
}
});

let mut errors = Vec::new();
for (err_code, nb) in &error_codes {
if !*nb && !WHITELIST.contains(&err_code.as_str()) {
Expand All @@ -117,7 +158,7 @@ pub fn check(path: &Path, bad: &mut bool) {
eprintln!("{}", err);
}
println!("Found {} error codes with no tests", errors.len());
if !errors.is_empty() {
if !errors.is_empty() || errors_count != 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we instead push into the errors vec instead of having two separate modes?

*bad = true;
}
println!("Done!");
Expand Down