Dogfood 'str_split_once() with Tidy

This commit is contained in:
Eric Arellano 2020-12-07 12:54:55 -07:00
parent 12db2225b6
commit d6baf3875c
6 changed files with 64 additions and 57 deletions

View File

@ -59,11 +59,10 @@ fn verify(tomlfile: &Path, libfile: &Path, bad: &mut bool) {
break;
}
let mut parts = line.splitn(2, '=');
let krate = parts.next().unwrap().trim();
if parts.next().is_none() {
continue;
}
let krate = match line.split_once('=') {
None => continue,
Some((krate, _)) => krate.trim(),
};
// Don't worry about depending on core/std while not writing `extern crate
// core/std` -- that's intentional.

View File

@ -85,47 +85,55 @@ fn extract_error_codes(
for line in f.lines() {
let s = line.trim();
if !reached_no_explanation && s.starts_with('E') && s.contains("include_str!(\"") {
if let Some(err_code) = s.splitn(2, ':').next() {
let err_code = err_code.to_owned();
if !error_codes.contains_key(&err_code) {
error_codes.insert(err_code.clone(), false);
let err_code = match s.split_once(':') {
None => continue,
Some((err_code, _)) => err_code.to_owned(),
};
if !error_codes.contains_key(&err_code) {
error_codes.insert(err_code.clone(), false);
}
// Now we extract the tests from the markdown file!
let md_file_name = match s.split_once("include_str!(\"") {
None => continue,
Some((_, md)) => match md.split_once("\")") {
None => continue,
Some((file_name, _)) => file_name,
},
};
let path = some_or_continue!(path.parent())
.join(md_file_name)
.canonicalize()
.expect("failed to canonicalize error explanation file path");
match read_to_string(&path) {
Ok(content) => {
if !IGNORE_EXPLANATION_CHECK.contains(&err_code.as_str())
&& !check_if_error_code_is_test_in_explanation(&content, &err_code)
{
errors.push(format!(
"`{}` doesn't use its own error code in compile_fail example",
path.display(),
));
}
if check_error_code_explanation(&content, error_codes, err_code) {
errors.push(format!(
"`{}` uses invalid tag `compile-fail` instead of `compile_fail`",
path.display(),
));
}
}
// Now we extract the tests from the markdown file!
let md = some_or_continue!(s.splitn(2, "include_str!(\"").nth(1));
let md_file_name = some_or_continue!(md.splitn(2, "\")").next());
let path = some_or_continue!(path.parent())
.join(md_file_name)
.canonicalize()
.expect("failed to canonicalize error explanation file path");
match read_to_string(&path) {
Ok(content) => {
if !IGNORE_EXPLANATION_CHECK.contains(&err_code.as_str())
&& !check_if_error_code_is_test_in_explanation(&content, &err_code)
{
errors.push(format!(
"`{}` doesn't use its own error code in compile_fail example",
path.display(),
));
}
if check_error_code_explanation(&content, error_codes, err_code) {
errors.push(format!(
"`{}` uses invalid tag `compile-fail` instead of `compile_fail`",
path.display(),
));
}
}
Err(e) => {
eprintln!("Couldn't read `{}`: {}", path.display(), e);
}
Err(e) => {
eprintln!("Couldn't read `{}`: {}", path.display(), e);
}
}
} else if reached_no_explanation && s.starts_with('E') {
if let Some(err_code) = s.splitn(2, ',').next() {
let err_code = err_code.to_owned();
if !error_codes.contains_key(&err_code) {
// this check should *never* fail!
error_codes.insert(err_code, false);
}
let err_code = match s.split_once(',') {
None => s,
Some((err_code, _)) => err_code,
}
.to_string();
if !error_codes.contains_key(&err_code) {
// this check should *never* fail!
error_codes.insert(err_code, false);
}
} else if s == ";" {
reached_no_explanation = true;
@ -137,12 +145,15 @@ fn extract_error_codes_from_tests(f: &str, error_codes: &mut HashMap<String, boo
for line in f.lines() {
let s = line.trim();
if s.starts_with("error[E") || s.starts_with("warning[E") {
if let Some(err_code) = s.splitn(2, ']').next() {
if let Some(err_code) = err_code.splitn(2, '[').nth(1) {
let nb = error_codes.entry(err_code.to_owned()).or_insert(false);
*nb = true;
}
}
let err_code = match s.split_once(']') {
None => continue,
Some((err_code, _)) => match err_code.split_once('[') {
None => continue,
Some((_, err_code)) => err_code,
},
};
let nb = error_codes.entry(err_code.to_owned()).or_insert(false);
*nb = true;
}
}
}

View File

@ -23,7 +23,7 @@ pub fn check(root: &Path, bad: &mut bool) {
}
// Extract source value.
let source = line.splitn(2, '=').nth(1).unwrap().trim();
let source = line.split_once('=').unwrap().1.trim();
// Ensure source is allowed.
if !ALLOWED_SOURCES.contains(&&*source) {

View File

@ -112,6 +112,7 @@ pub fn check(
let gate_test_str = "gate-test-";
let feature_name = match line.find(gate_test_str) {
// NB: the `splitn` always succeeds, even if the delimiter is not present.
Some(i) => line[i + gate_test_str.len()..].splitn(2, ' ').next().unwrap(),
None => continue,
};

View File

@ -3,6 +3,8 @@
//! This library contains the tidy lints and exposes it
//! to be used by tools.
#![feature(str_split_once)]
use std::fs::File;
use std::io::Read;
use walkdir::{DirEntry, WalkDir};

View File

@ -19,14 +19,8 @@ pub fn check(path: &Path, bad: &mut bool) {
//
// For now, just make sure that there is a corresponding
// `$testname.rs` file.
let testname = file_path
.file_name()
.unwrap()
.to_str()
.unwrap()
.splitn(2, '.')
.next()
.unwrap();
let testname =
file_path.file_name().unwrap().to_str().unwrap().split_once('.').unwrap().0;
if !file_path.with_file_name(testname).with_extension("rs").exists() {
println!("Stray file with UI testing output: {:?}", file_path);
*bad = true;