Skip to content

Commit d6baf38

Browse files
committed
Dogfood 'str_split_once() with Tidy
1 parent 12db222 commit d6baf38

File tree

6 files changed

+63
-56
lines changed

6 files changed

+63
-56
lines changed

src/tools/tidy/src/cargo.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,10 @@ fn verify(tomlfile: &Path, libfile: &Path, bad: &mut bool) {
5959
break;
6060
}
6161

62-
let mut parts = line.splitn(2, '=');
63-
let krate = parts.next().unwrap().trim();
64-
if parts.next().is_none() {
65-
continue;
66-
}
62+
let krate = match line.split_once('=') {
63+
None => continue,
64+
Some((krate, _)) => krate.trim(),
65+
};
6766

6867
// Don't worry about depending on core/std while not writing `extern crate
6968
// core/std` -- that's intentional.

src/tools/tidy/src/error_codes_check.rs

+53-42
Original file line numberDiff line numberDiff line change
@@ -85,47 +85,55 @@ fn extract_error_codes(
8585
for line in f.lines() {
8686
let s = line.trim();
8787
if !reached_no_explanation && s.starts_with('E') && s.contains("include_str!(\"") {
88-
if let Some(err_code) = s.splitn(2, ':').next() {
89-
let err_code = err_code.to_owned();
90-
if !error_codes.contains_key(&err_code) {
91-
error_codes.insert(err_code.clone(), false);
92-
}
93-
// Now we extract the tests from the markdown file!
94-
let md = some_or_continue!(s.splitn(2, "include_str!(\"").nth(1));
95-
let md_file_name = some_or_continue!(md.splitn(2, "\")").next());
96-
let path = some_or_continue!(path.parent())
97-
.join(md_file_name)
98-
.canonicalize()
99-
.expect("failed to canonicalize error explanation file path");
100-
match read_to_string(&path) {
101-
Ok(content) => {
102-
if !IGNORE_EXPLANATION_CHECK.contains(&err_code.as_str())
103-
&& !check_if_error_code_is_test_in_explanation(&content, &err_code)
104-
{
105-
errors.push(format!(
106-
"`{}` doesn't use its own error code in compile_fail example",
107-
path.display(),
108-
));
109-
}
110-
if check_error_code_explanation(&content, error_codes, err_code) {
111-
errors.push(format!(
112-
"`{}` uses invalid tag `compile-fail` instead of `compile_fail`",
113-
path.display(),
114-
));
115-
}
88+
let err_code = match s.split_once(':') {
89+
None => continue,
90+
Some((err_code, _)) => err_code.to_owned(),
91+
};
92+
if !error_codes.contains_key(&err_code) {
93+
error_codes.insert(err_code.clone(), false);
94+
}
95+
// Now we extract the tests from the markdown file!
96+
let md_file_name = match s.split_once("include_str!(\"") {
97+
None => continue,
98+
Some((_, md)) => match md.split_once("\")") {
99+
None => continue,
100+
Some((file_name, _)) => file_name,
101+
},
102+
};
103+
let path = some_or_continue!(path.parent())
104+
.join(md_file_name)
105+
.canonicalize()
106+
.expect("failed to canonicalize error explanation file path");
107+
match read_to_string(&path) {
108+
Ok(content) => {
109+
if !IGNORE_EXPLANATION_CHECK.contains(&err_code.as_str())
110+
&& !check_if_error_code_is_test_in_explanation(&content, &err_code)
111+
{
112+
errors.push(format!(
113+
"`{}` doesn't use its own error code in compile_fail example",
114+
path.display(),
115+
));
116116
}
117-
Err(e) => {
118-
eprintln!("Couldn't read `{}`: {}", path.display(), e);
117+
if check_error_code_explanation(&content, error_codes, err_code) {
118+
errors.push(format!(
119+
"`{}` uses invalid tag `compile-fail` instead of `compile_fail`",
120+
path.display(),
121+
));
119122
}
120123
}
124+
Err(e) => {
125+
eprintln!("Couldn't read `{}`: {}", path.display(), e);
126+
}
121127
}
122128
} else if reached_no_explanation && s.starts_with('E') {
123-
if let Some(err_code) = s.splitn(2, ',').next() {
124-
let err_code = err_code.to_owned();
125-
if !error_codes.contains_key(&err_code) {
126-
// this check should *never* fail!
127-
error_codes.insert(err_code, false);
128-
}
129+
let err_code = match s.split_once(',') {
130+
None => s,
131+
Some((err_code, _)) => err_code,
132+
}
133+
.to_string();
134+
if !error_codes.contains_key(&err_code) {
135+
// this check should *never* fail!
136+
error_codes.insert(err_code, false);
129137
}
130138
} else if s == ";" {
131139
reached_no_explanation = true;
@@ -137,12 +145,15 @@ fn extract_error_codes_from_tests(f: &str, error_codes: &mut HashMap<String, boo
137145
for line in f.lines() {
138146
let s = line.trim();
139147
if s.starts_with("error[E") || s.starts_with("warning[E") {
140-
if let Some(err_code) = s.splitn(2, ']').next() {
141-
if let Some(err_code) = err_code.splitn(2, '[').nth(1) {
142-
let nb = error_codes.entry(err_code.to_owned()).or_insert(false);
143-
*nb = true;
144-
}
145-
}
148+
let err_code = match s.split_once(']') {
149+
None => continue,
150+
Some((err_code, _)) => match err_code.split_once('[') {
151+
None => continue,
152+
Some((_, err_code)) => err_code,
153+
},
154+
};
155+
let nb = error_codes.entry(err_code.to_owned()).or_insert(false);
156+
*nb = true;
146157
}
147158
}
148159
}

src/tools/tidy/src/extdeps.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn check(root: &Path, bad: &mut bool) {
2323
}
2424

2525
// Extract source value.
26-
let source = line.splitn(2, '=').nth(1).unwrap().trim();
26+
let source = line.split_once('=').unwrap().1.trim();
2727

2828
// Ensure source is allowed.
2929
if !ALLOWED_SOURCES.contains(&&*source) {

src/tools/tidy/src/features.rs

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ pub fn check(
112112
let gate_test_str = "gate-test-";
113113

114114
let feature_name = match line.find(gate_test_str) {
115+
// NB: the `splitn` always succeeds, even if the delimiter is not present.
115116
Some(i) => line[i + gate_test_str.len()..].splitn(2, ' ').next().unwrap(),
116117
None => continue,
117118
};

src/tools/tidy/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
//! This library contains the tidy lints and exposes it
44
//! to be used by tools.
55
6+
#![feature(str_split_once)]
7+
68
use std::fs::File;
79
use std::io::Read;
810
use walkdir::{DirEntry, WalkDir};

src/tools/tidy/src/ui_tests.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,8 @@ pub fn check(path: &Path, bad: &mut bool) {
1919
//
2020
// For now, just make sure that there is a corresponding
2121
// `$testname.rs` file.
22-
let testname = file_path
23-
.file_name()
24-
.unwrap()
25-
.to_str()
26-
.unwrap()
27-
.splitn(2, '.')
28-
.next()
29-
.unwrap();
22+
let testname =
23+
file_path.file_name().unwrap().to_str().unwrap().split_once('.').unwrap().0;
3024
if !file_path.with_file_name(testname).with_extension("rs").exists() {
3125
println!("Stray file with UI testing output: {:?}", file_path);
3226
*bad = true;

0 commit comments

Comments
 (0)