Skip to content

Commit 430c88f

Browse files
committed
use Result for error handling and use glob for walk
1 parent 73deb33 commit 430c88f

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

libc-test/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ libc = { path = "..", version = "1.0.0-alpha.1", default-features = false }
1818
[dev-dependencies]
1919
syn = { version = "2.0.91", features = ["full", "visit"] }
2020
proc-macro2 = { version = "1.0.92", features = ["span-locations"] }
21+
glob = "0.3.2"
2122

2223
[build-dependencies]
2324
cc = "1.0.83"

libc-test/test/style.rs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,8 @@ use syn::spanned::Spanned;
3232
use syn::visit::{self, Visit};
3333
use syn::Token;
3434

35-
macro_rules! t {
36-
($e:expr) => {
37-
match $e {
38-
Ok(e) => e,
39-
Err(e) => panic!("{} failed with {}", stringify!($e), e),
40-
}
41-
};
42-
}
35+
type Error = Box<dyn std::error::Error>;
36+
type Result<T> = std::result::Result<T, Error>;
4337

4438
#[test]
4539
fn check_style() {
@@ -50,7 +44,7 @@ fn check_style() {
5044

5145
let root_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../src");
5246
let mut errors = Errors { errs: false };
53-
walk(&root_dir, &mut errors);
47+
walk(&root_dir, &mut errors).expect("root dir should be walked successfully");
5448

5549
if errors.errs {
5650
panic!("found some lint errors");
@@ -59,29 +53,31 @@ fn check_style() {
5953
}
6054
}
6155

62-
fn walk(path: &Path, err: &mut Errors) {
63-
for entry in t!(path.read_dir()).map(|e| t!(e)) {
64-
let path = entry.path();
65-
if t!(entry.file_type()).is_dir() {
66-
walk(&path, err);
56+
fn walk(root_dir: &Path, err: &mut Errors) -> Result<()> {
57+
for entry in glob::glob(&format!(
58+
"{}/**/*.rs",
59+
root_dir.to_str().expect("dir should be valid UTF-8")
60+
))? {
61+
let entry = entry?;
62+
63+
let name = entry
64+
.file_name()
65+
.expect("file name should not end in ..")
66+
.to_str()
67+
.expect("file name should be valid UTF-8");
68+
if let "lib.rs" | "macros.rs" = &name[..] {
6769
continue;
6870
}
6971

70-
let name = entry.file_name().into_string().unwrap();
71-
match &name[..] {
72-
n if !n.ends_with(".rs") => continue,
73-
74-
"lib.rs" | "macros.rs" => continue,
75-
76-
_ => {}
77-
}
78-
7972
let mut contents = String::new();
80-
t!(t!(fs::File::open(&path)).read_to_string(&mut contents));
73+
let path = entry.as_path();
74+
fs::File::open(path)?.read_to_string(&mut contents)?;
8175

82-
let file = t!(syn::parse_file(&contents));
76+
let file = syn::parse_file(&contents)?;
8377
StyleChecker::new(|line, msg| err.error(&path, line, msg)).visit_file(&file);
8478
}
79+
80+
Ok(())
8581
}
8682

8783
struct Errors {

0 commit comments

Comments
 (0)