Skip to content

Commit 38f0b90

Browse files
Move file-reading into walker loop
1 parent c113a37 commit 38f0b90

File tree

8 files changed

+36
-67
lines changed

8 files changed

+36
-67
lines changed

Diff for: src/tools/tidy/src/bins.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ pub fn check(path: &Path, bad: &mut bool) {
2525
}
2626
}
2727

28-
super::walk(path,
28+
super::walk_no_read(path,
2929
&mut |path| super::filter_dirs(path) || path.ends_with("src/etc"),
30-
&mut |entry, _contents| {
30+
&mut |entry| {
3131
let file = entry.path();
3232
let filename = file.file_name().unwrap().to_string_lossy();
3333
let extensions = [".py", ".sh"];

Diff for: src/tools/tidy/src/errors.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,19 @@
44
//! statistics about the error codes.
55
66
use std::collections::HashMap;
7-
use std::fs::File;
8-
use std::io::prelude::*;
97
use std::path::Path;
108

119
pub fn check(path: &Path, bad: &mut bool) {
12-
let mut contents = String::new();
1310
let mut map: HashMap<_, Vec<_>> = HashMap::new();
1411
super::walk(path,
1512
&mut |path| super::filter_dirs(path) || path.ends_with("src/test"),
16-
&mut |entry, _contents| {
13+
&mut |entry, contents| {
1714
let file = entry.path();
1815
let filename = file.file_name().unwrap().to_string_lossy();
1916
if filename != "error_codes.rs" {
2017
return
2118
}
2219

23-
contents.truncate(0);
24-
t!(t!(File::open(file)).read_to_string(&mut contents));
25-
2620
// In the `register_long_diagnostics!` macro, entries look like this:
2721
//
2822
// ```

Diff for: src/tools/tidy/src/features.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
1212
use std::collections::HashMap;
1313
use std::fmt;
14-
use std::fs::{self, File};
15-
use std::io::prelude::*;
14+
use std::fs;
1615
use std::path::Path;
1716

1817
use regex::Regex;
@@ -58,13 +57,11 @@ pub fn check(path: &Path, bad: &mut bool, verbose: bool) {
5857
let lib_features = get_and_check_lib_features(path, bad, &features);
5958
assert!(!lib_features.is_empty());
6059

61-
let mut contents = String::new();
62-
6360
super::walk_many(&[&path.join("test/ui"),
6461
&path.join("test/ui-fulldeps"),
6562
&path.join("test/compile-fail")],
6663
&mut |path| super::filter_dirs(path),
67-
&mut |entry, _contents| {
64+
&mut |entry, contents| {
6865
let file = entry.path();
6966
let filename = file.file_name().unwrap().to_string_lossy();
7067
if !filename.ends_with(".rs") || filename == "features.rs" ||
@@ -75,9 +72,6 @@ pub fn check(path: &Path, bad: &mut bool, verbose: bool) {
7572
let filen_underscore = filename.replace('-',"_").replace(".rs","");
7673
let filename_is_gate_test = test_filen_gate(&filen_underscore, &mut features);
7774

78-
contents.truncate(0);
79-
t!(t!(File::open(&file), &file).read_to_string(&mut contents));
80-
8175
for (i, line) in contents.lines().enumerate() {
8276
let mut err = |msg: &str| {
8377
tidy_error!(bad, "{}:{}: {}", file.display(), i + 1, msg);
@@ -369,20 +363,16 @@ fn get_and_check_lib_features(base_src_path: &Path,
369363

370364
fn map_lib_features(base_src_path: &Path,
371365
mf: &mut dyn FnMut(Result<(&str, Feature), &str>, &Path, usize)) {
372-
let mut contents = String::new();
373366
super::walk(base_src_path,
374367
&mut |path| super::filter_dirs(path) || path.ends_with("src/test"),
375-
&mut |entry, _contents| {
368+
&mut |entry, contents| {
376369
let file = entry.path();
377370
let filename = file.file_name().unwrap().to_string_lossy();
378371
if !filename.ends_with(".rs") || filename == "features.rs" ||
379372
filename == "diagnostic_list.rs" {
380373
return;
381374
}
382375

383-
contents.truncate(0);
384-
t!(t!(File::open(&file), &file).read_to_string(&mut contents));
385-
386376
let mut becoming_feature: Option<(String, Feature)> = None;
387377
for (i, line) in contents.lines().enumerate() {
388378
macro_rules! err {

Diff for: src/tools/tidy/src/lib.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ fn filter_dirs(path: &Path) -> bool {
6767
skip.iter().any(|p| path.ends_with(p))
6868
}
6969

70+
7071
fn walk_many(
7172
paths: &[&Path], skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&DirEntry, &str)
7273
) {
@@ -76,19 +77,25 @@ fn walk_many(
7677
}
7778

7879
fn walk(path: &Path, skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&DirEntry, &str)) {
80+
let mut contents = String::new();
81+
walk_no_read(path, skip, &mut |entry| {
82+
contents.clear();
83+
if t!(File::open(entry.path()), entry.path()).read_to_string(&mut contents).is_err() {
84+
contents.clear();
85+
}
86+
f(&entry, &contents);
87+
});
88+
}
89+
90+
fn walk_no_read(path: &Path, skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&DirEntry)) {
7991
let walker = WalkDir::new(path).into_iter()
8092
.filter_entry(|e| !skip(e.path()));
81-
let mut contents = String::new();
8293
for entry in walker {
8394
if let Ok(entry) = entry {
8495
if entry.file_type().is_dir() {
8596
continue;
8697
}
87-
contents.clear();
88-
if t!(File::open(entry.path()), entry.path()).read_to_string(&mut contents).is_err() {
89-
contents.clear();
90-
}
91-
f(&entry, &contents);
98+
f(&entry);
9299
}
93100
}
94101
}

Diff for: src/tools/tidy/src/libcoretest.rs

+8-16
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,22 @@
44
//! item. All tests must be written externally in `libcore/tests`.
55
66
use std::path::Path;
7-
use std::fs::read_to_string;
87

98
pub fn check(path: &Path, bad: &mut bool) {
109
let libcore_path = path.join("libcore");
1110
super::walk(
1211
&libcore_path,
1312
&mut |subpath| t!(subpath.strip_prefix(&libcore_path)).starts_with("tests"),
14-
&mut |entry, _contents| {
13+
&mut |entry, contents| {
1514
let subpath = entry.path();
1615
if let Some("rs") = subpath.extension().and_then(|e| e.to_str()) {
17-
match read_to_string(subpath) {
18-
Ok(contents) => {
19-
if contents.contains("#[test]") {
20-
tidy_error!(
21-
bad,
22-
"{} contains #[test]; libcore tests must be placed inside \
23-
`src/libcore/tests/`",
24-
subpath.display()
25-
);
26-
}
27-
}
28-
Err(err) => {
29-
panic!("failed to read file {:?}: {}", subpath, err);
30-
}
16+
if contents.contains("#[test]") {
17+
tidy_error!(
18+
bad,
19+
"{} contains #[test]; libcore tests must be placed inside \
20+
`src/libcore/tests/`",
21+
subpath.display()
22+
);
3123
}
3224
}
3325
},

Diff for: src/tools/tidy/src/pal.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
//! platform-specific cfgs are allowed. Not sure yet how to deal with
3232
//! this in the long term.
3333
34-
use std::fs::File;
35-
use std::io::Read;
3634
use std::path::Path;
3735
use std::iter::Iterator;
3836

@@ -87,30 +85,26 @@ const EXCEPTION_PATHS: &[&str] = &[
8785
];
8886

8987
pub fn check(path: &Path, bad: &mut bool) {
90-
let mut contents = String::new();
9188
// Sanity check that the complex parsing here works.
9289
let mut saw_target_arch = false;
9390
let mut saw_cfg_bang = false;
94-
super::walk(path, &mut super::filter_dirs, &mut |entry, _contents| {
91+
super::walk(path, &mut super::filter_dirs, &mut |entry, contents| {
9592
let file = entry.path();
9693
let filestr = file.to_string_lossy().replace("\\", "/");
9794
if !filestr.ends_with(".rs") { return }
9895

9996
let is_exception_path = EXCEPTION_PATHS.iter().any(|s| filestr.contains(&**s));
10097
if is_exception_path { return }
10198

102-
check_cfgs(&mut contents, &file, bad, &mut saw_target_arch, &mut saw_cfg_bang);
99+
check_cfgs(contents, &file, bad, &mut saw_target_arch, &mut saw_cfg_bang);
103100
});
104101

105102
assert!(saw_target_arch);
106103
assert!(saw_cfg_bang);
107104
}
108105

109-
fn check_cfgs(contents: &mut String, file: &Path,
106+
fn check_cfgs(contents: &str, file: &Path,
110107
bad: &mut bool, saw_target_arch: &mut bool, saw_cfg_bang: &mut bool) {
111-
contents.truncate(0);
112-
t!(t!(File::open(file), file).read_to_string(contents));
113-
114108
// For now it's ok to have platform-specific code after 'mod tests'.
115109
let mod_tests_idx = find_test_mod(contents);
116110
let contents = &contents[..mod_tests_idx];

Diff for: src/tools/tidy/src/style.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
//! A number of these checks can be opted-out of with various directives of the form:
1414
//! `// ignore-tidy-CHECK-NAME`.
1515
16-
use std::fs::File;
17-
use std::io::prelude::*;
1816
use std::path::Path;
1917

2018
const COLS: usize = 100;
@@ -109,7 +107,7 @@ enum Directive {
109107
Ignore(bool),
110108
}
111109

112-
fn contains_ignore_directive(contents: &String, check: &str) -> Directive {
110+
fn contains_ignore_directive(contents: &str, check: &str) -> Directive {
113111
if contents.contains(&format!("// ignore-tidy-{}", check)) ||
114112
contents.contains(&format!("# ignore-tidy-{}", check)) {
115113
Directive::Ignore(false)
@@ -129,8 +127,7 @@ macro_rules! suppressible_tidy_err {
129127
}
130128

131129
pub fn check(path: &Path, bad: &mut bool) {
132-
let mut contents = String::new();
133-
super::walk(path, &mut super::filter_dirs, &mut |entry, _contents| {
130+
super::walk(path, &mut super::filter_dirs, &mut |entry, contents| {
134131
let file = entry.path();
135132
let filename = file.file_name().unwrap().to_string_lossy();
136133
let extensions = [".rs", ".py", ".js", ".sh", ".c", ".cpp", ".h"];
@@ -139,9 +136,6 @@ pub fn check(path: &Path, bad: &mut bool) {
139136
return
140137
}
141138

142-
contents.truncate(0);
143-
t!(t!(File::open(file), file).read_to_string(&mut contents));
144-
145139
if contents.is_empty() {
146140
tidy_error!(bad, "{}: empty file", file.display());
147141
}

Diff for: src/tools/tidy/src/ui_tests.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ use std::fs;
44
use std::path::Path;
55

66
pub fn check(path: &Path, bad: &mut bool) {
7-
super::walk_many(
8-
&[&path.join("test/ui"), &path.join("test/ui-fulldeps")],
9-
&mut |_| false,
10-
&mut |entry, _contents| {
7+
for path in &[&path.join("test/ui"), &path.join("test/ui-fulldeps")] {
8+
super::walk_no_read(path, &mut |_| false, &mut |entry| {
119
let file_path = entry.path();
1210
if let Some(ext) = file_path.extension() {
1311
if ext == "stderr" || ext == "stdout" {
@@ -46,6 +44,6 @@ pub fn check(path: &Path, bad: &mut bool) {
4644
}
4745
}
4846
}
49-
},
50-
);
47+
});
48+
}
5149
}

0 commit comments

Comments
 (0)