Skip to content

Commit 19b272a

Browse files
committed
Use a single WalkBuilder for multiple paths
1 parent d26a155 commit 19b272a

File tree

3 files changed

+43
-42
lines changed

3 files changed

+43
-42
lines changed

src/tools/tidy/src/bins.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ mod os_impl {
103103

104104
// FIXME: we don't need to look at all binaries, only files that have been modified in this branch
105105
// (e.g. using `git ls-files`).
106-
walk_no_read(path, |path| filter_dirs(path) || path.ends_with("src/etc"), &mut |entry| {
106+
walk_no_read(&[path], |path| filter_dirs(path) || path.ends_with("src/etc"), &mut |entry| {
107107
let file = entry.path();
108108
let extension = file.extension();
109109
let scripts = ["py", "sh", "ps1"];

src/tools/tidy/src/ui_tests.rs

+29-29
Original file line numberDiff line numberDiff line change
@@ -49,37 +49,37 @@ fn check_entries(tests_path: &Path, bad: &mut bool) {
4949

5050
pub fn check(path: &Path, bad: &mut bool) {
5151
check_entries(&path, bad);
52-
for path in &[&path.join("ui"), &path.join("ui-fulldeps")] {
53-
crate::walk::walk_no_read(path, |_| false, &mut |entry| {
54-
let file_path = entry.path();
55-
if let Some(ext) = file_path.extension() {
56-
if ext == "stderr" || ext == "stdout" {
57-
// Test output filenames have one of the formats:
58-
// ```
59-
// $testname.stderr
60-
// $testname.$mode.stderr
61-
// $testname.$revision.stderr
62-
// $testname.$revision.$mode.stderr
63-
// ```
64-
//
65-
// For now, just make sure that there is a corresponding
66-
// `$testname.rs` file.
67-
//
68-
// NB: We do not use file_stem() as some file names have multiple `.`s and we
69-
// must strip all of them.
70-
let testname =
71-
file_path.file_name().unwrap().to_str().unwrap().split_once('.').unwrap().0;
72-
if !file_path.with_file_name(testname).with_extension("rs").exists() {
73-
tidy_error!(bad, "Stray file with UI testing output: {:?}", file_path);
74-
}
52+
let (ui, ui_fulldeps) = (path.join("ui"), path.join("ui-fulldeps"));
53+
let paths = [ui.as_path(), ui_fulldeps.as_path()];
54+
crate::walk::walk_no_read(&paths, |_| false, &mut |entry| {
55+
let file_path = entry.path();
56+
if let Some(ext) = file_path.extension() {
57+
if ext == "stderr" || ext == "stdout" {
58+
// Test output filenames have one of the formats:
59+
// ```
60+
// $testname.stderr
61+
// $testname.$mode.stderr
62+
// $testname.$revision.stderr
63+
// $testname.$revision.$mode.stderr
64+
// ```
65+
//
66+
// For now, just make sure that there is a corresponding
67+
// `$testname.rs` file.
68+
//
69+
// NB: We do not use file_stem() as some file names have multiple `.`s and we
70+
// must strip all of them.
71+
let testname =
72+
file_path.file_name().unwrap().to_str().unwrap().split_once('.').unwrap().0;
73+
if !file_path.with_file_name(testname).with_extension("rs").exists() {
74+
tidy_error!(bad, "Stray file with UI testing output: {:?}", file_path);
75+
}
7576

76-
if let Ok(metadata) = fs::metadata(file_path) {
77-
if metadata.len() == 0 {
78-
tidy_error!(bad, "Empty file with UI testing output: {:?}", file_path);
79-
}
77+
if let Ok(metadata) = fs::metadata(file_path) {
78+
if metadata.len() == 0 {
79+
tidy_error!(bad, "Empty file with UI testing output: {:?}", file_path);
8080
}
8181
}
8282
}
83-
});
84-
}
83+
}
84+
});
8585
}

src/tools/tidy/src/walk.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,24 @@ pub fn filter_dirs(path: &Path) -> bool {
3535

3636
/// Filter for only files that end in `.rs`.
3737
pub fn filter_not_rust(path: &Path) -> bool {
38-
!path.is_dir() && path.extension() != Some(OsStr::new("rs"))
38+
path.extension() != Some(OsStr::new("rs")) && !path.is_dir()
3939
}
4040

41-
pub fn walk_many(
42-
paths: &[&Path],
41+
pub fn walk(
42+
path: &Path,
4343
skip: impl Clone + Send + Sync + 'static + Fn(&Path) -> bool,
4444
f: &mut dyn FnMut(&DirEntry, &str),
4545
) {
46-
for path in paths {
47-
walk(path, skip.clone(), f);
48-
}
46+
walk_many(&[path], skip, f);
4947
}
5048

51-
pub fn walk(
52-
path: &Path,
53-
skip: impl Send + Sync + 'static + Fn(&Path) -> bool,
49+
pub fn walk_many(
50+
paths: &[&Path],
51+
skip: impl Clone + Send + Sync + 'static + Fn(&Path) -> bool,
5452
f: &mut dyn FnMut(&DirEntry, &str),
5553
) {
5654
let mut contents = Vec::new();
57-
walk_no_read(path, skip, &mut |entry| {
55+
walk_no_read(paths, skip, &mut |entry| {
5856
contents.clear();
5957
let mut file = t!(File::open(entry.path()), entry.path());
6058
t!(file.read_to_end(&mut contents), entry.path());
@@ -67,11 +65,14 @@ pub fn walk(
6765
}
6866

6967
pub(crate) fn walk_no_read(
70-
path: &Path,
68+
paths: &[&Path],
7169
skip: impl Send + Sync + 'static + Fn(&Path) -> bool,
7270
f: &mut dyn FnMut(&DirEntry),
7371
) {
74-
let mut walker = ignore::WalkBuilder::new(path);
72+
let mut walker = ignore::WalkBuilder::new(paths[0]);
73+
for path in &paths[1..] {
74+
walker.add(path);
75+
}
7576
let walker = walker.filter_entry(move |e| !skip(e.path()));
7677
for entry in walker.build() {
7778
if let Ok(entry) = entry {

0 commit comments

Comments
 (0)