]>
Commit | Line | Data |
---|---|---|
9fa01778 | 1 | //! Library used by tidy and other tools. |
041b39d2 XL |
2 | //! |
3 | //! This library contains the tidy lints and exposes it | |
4 | //! to be used by tools. | |
5 | ||
dc9dc135 XL |
6 | use std::fs::File; |
7 | use std::io::Read; | |
dfeec247 | 8 | use walkdir::{DirEntry, WalkDir}; |
041b39d2 XL |
9 | |
10 | use std::path::Path; | |
11 | ||
12 | macro_rules! t { | |
dfeec247 XL |
13 | ($e:expr, $p:expr) => { |
14 | match $e { | |
15 | Ok(e) => e, | |
16 | Err(e) => panic!("{} failed on {} with {}", stringify!($e), ($p).display(), e), | |
17 | } | |
18 | }; | |
041b39d2 | 19 | |
dfeec247 XL |
20 | ($e:expr) => { |
21 | match $e { | |
22 | Ok(e) => e, | |
23 | Err(e) => panic!("{} failed with {}", stringify!($e), e), | |
24 | } | |
25 | }; | |
041b39d2 XL |
26 | } |
27 | ||
28 | macro_rules! tidy_error { | |
29 | ($bad:expr, $fmt:expr, $($arg:tt)*) => ({ | |
041b39d2 | 30 | *$bad = true; |
abe05a73 XL |
31 | eprint!("tidy error: "); |
32 | eprintln!($fmt, $($arg)*); | |
041b39d2 XL |
33 | }); |
34 | } | |
35 | ||
36 | pub mod bins; | |
dfeec247 | 37 | pub mod cargo; |
e74abb32 | 38 | pub mod debug_artifacts; |
dfeec247 XL |
39 | pub mod deps; |
40 | pub mod edition; | |
41 | pub mod error_codes_check; | |
041b39d2 | 42 | pub mod errors; |
dfeec247 | 43 | pub mod extdeps; |
041b39d2 | 44 | pub mod features; |
041b39d2 | 45 | pub mod pal; |
dfeec247 | 46 | pub mod style; |
0531ce1d | 47 | pub mod ui_tests; |
416331ca | 48 | pub mod unit_tests; |
041b39d2 XL |
49 | pub mod unstable_book; |
50 | ||
51 | fn filter_dirs(path: &Path) -> bool { | |
52 | let skip = [ | |
416331ca XL |
53 | "src/llvm-project", |
54 | "src/stdarch", | |
041b39d2 | 55 | "src/tools/cargo", |
ea8adc8c | 56 | "src/tools/clippy", |
416331ca XL |
57 | "src/tools/miri", |
58 | "src/tools/rls", | |
041b39d2 | 59 | "src/tools/rust-installer", |
ea8adc8c | 60 | "src/tools/rustfmt", |
74b04a01 | 61 | "src/doc/book", |
e74abb32 XL |
62 | // Filter RLS output directories |
63 | "target/rls", | |
041b39d2 XL |
64 | ]; |
65 | skip.iter().any(|p| path.ends_with(p)) | |
66 | } | |
67 | ||
dc9dc135 | 68 | fn walk_many( |
dfeec247 XL |
69 | paths: &[&Path], |
70 | skip: &mut dyn FnMut(&Path) -> bool, | |
71 | f: &mut dyn FnMut(&DirEntry, &str), | |
dc9dc135 | 72 | ) { |
041b39d2 XL |
73 | for path in paths { |
74 | walk(path, skip, f); | |
75 | } | |
76 | } | |
77 | ||
dc9dc135 XL |
78 | fn walk(path: &Path, skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&DirEntry, &str)) { |
79 | let mut contents = String::new(); | |
80 | walk_no_read(path, skip, &mut |entry| { | |
81 | contents.clear(); | |
82 | if t!(File::open(entry.path()), entry.path()).read_to_string(&mut contents).is_err() { | |
83 | contents.clear(); | |
84 | } | |
85 | f(&entry, &contents); | |
86 | }); | |
87 | } | |
88 | ||
89 | fn walk_no_read(path: &Path, skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&DirEntry)) { | |
dfeec247 | 90 | let walker = WalkDir::new(path).into_iter().filter_entry(|e| !skip(e.path())); |
dc9dc135 XL |
91 | for entry in walker { |
92 | if let Ok(entry) = entry { | |
93 | if entry.file_type().is_dir() { | |
94 | continue; | |
041b39d2 | 95 | } |
dc9dc135 | 96 | f(&entry); |
041b39d2 XL |
97 | } |
98 | } | |
99 | } |