Pass contents and DirEntry to walkers

This commit is contained in:
Mark Rousskov 2019-06-21 12:23:20 -04:00
parent 5c33c3e308
commit c113a3769d
8 changed files with 29 additions and 12 deletions

View File

@ -27,7 +27,8 @@ pub fn check(path: &Path, bad: &mut bool) {
super::walk(path,
&mut |path| super::filter_dirs(path) || path.ends_with("src/etc"),
&mut |file| {
&mut |entry, _contents| {
let file = entry.path();
let filename = file.file_name().unwrap().to_string_lossy();
let extensions = [".py", ".sh"];
if extensions.iter().any(|e| filename.ends_with(e)) {

View File

@ -13,7 +13,8 @@ pub fn check(path: &Path, bad: &mut bool) {
let mut map: HashMap<_, Vec<_>> = HashMap::new();
super::walk(path,
&mut |path| super::filter_dirs(path) || path.ends_with("src/test"),
&mut |file| {
&mut |entry, _contents| {
let file = entry.path();
let filename = file.file_name().unwrap().to_string_lossy();
if filename != "error_codes.rs" {
return

View File

@ -64,7 +64,8 @@ pub fn check(path: &Path, bad: &mut bool, verbose: bool) {
&path.join("test/ui-fulldeps"),
&path.join("test/compile-fail")],
&mut |path| super::filter_dirs(path),
&mut |file| {
&mut |entry, _contents| {
let file = entry.path();
let filename = file.file_name().unwrap().to_string_lossy();
if !filename.ends_with(".rs") || filename == "features.rs" ||
filename == "diagnostic_list.rs" {
@ -371,7 +372,8 @@ fn map_lib_features(base_src_path: &Path,
let mut contents = String::new();
super::walk(base_src_path,
&mut |path| super::filter_dirs(path) || path.ends_with("src/test"),
&mut |file| {
&mut |entry, _contents| {
let file = entry.path();
let filename = file.file_name().unwrap().to_string_lossy();
if !filename.ends_with(".rs") || filename == "features.rs" ||
filename == "diagnostic_list.rs" {

View File

@ -3,7 +3,9 @@
//! This library contains the tidy lints and exposes it
//! to be used by tools.
use walkdir::WalkDir;
use walkdir::{DirEntry, WalkDir};
use std::fs::File;
use std::io::Read;
use std::path::Path;
@ -65,21 +67,28 @@ fn filter_dirs(path: &Path) -> bool {
skip.iter().any(|p| path.ends_with(p))
}
fn walk_many(paths: &[&Path], skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&Path)) {
fn walk_many(
paths: &[&Path], skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&DirEntry, &str)
) {
for path in paths {
walk(path, skip, f);
}
}
fn walk(path: &Path, skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&Path)) {
fn walk(path: &Path, skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&DirEntry, &str)) {
let walker = WalkDir::new(path).into_iter()
.filter_entry(|e| !skip(e.path()));
let mut contents = String::new();
for entry in walker {
if let Ok(entry) = entry {
if entry.file_type().is_dir() {
continue;
}
f(&entry.path());
contents.clear();
if t!(File::open(entry.path()), entry.path()).read_to_string(&mut contents).is_err() {
contents.clear();
}
f(&entry, &contents);
}
}
}

View File

@ -11,7 +11,8 @@ pub fn check(path: &Path, bad: &mut bool) {
super::walk(
&libcore_path,
&mut |subpath| t!(subpath.strip_prefix(&libcore_path)).starts_with("tests"),
&mut |subpath| {
&mut |entry, _contents| {
let subpath = entry.path();
if let Some("rs") = subpath.extension().and_then(|e| e.to_str()) {
match read_to_string(subpath) {
Ok(contents) => {

View File

@ -91,7 +91,8 @@ pub fn check(path: &Path, bad: &mut bool) {
// Sanity check that the complex parsing here works.
let mut saw_target_arch = false;
let mut saw_cfg_bang = false;
super::walk(path, &mut super::filter_dirs, &mut |file| {
super::walk(path, &mut super::filter_dirs, &mut |entry, _contents| {
let file = entry.path();
let filestr = file.to_string_lossy().replace("\\", "/");
if !filestr.ends_with(".rs") { return }

View File

@ -130,7 +130,8 @@ macro_rules! suppressible_tidy_err {
pub fn check(path: &Path, bad: &mut bool) {
let mut contents = String::new();
super::walk(path, &mut super::filter_dirs, &mut |file| {
super::walk(path, &mut super::filter_dirs, &mut |entry, _contents| {
let file = entry.path();
let filename = file.file_name().unwrap().to_string_lossy();
let extensions = [".rs", ".py", ".js", ".sh", ".c", ".cpp", ".h"];
if extensions.iter().all(|e| !filename.ends_with(e)) ||

View File

@ -7,7 +7,8 @@ pub fn check(path: &Path, bad: &mut bool) {
super::walk_many(
&[&path.join("test/ui"), &path.join("test/ui-fulldeps")],
&mut |_| false,
&mut |file_path| {
&mut |entry, _contents| {
let file_path = entry.path();
if let Some(ext) = file_path.extension() {
if ext == "stderr" || ext == "stdout" {
// Test output filenames have one of the formats: