]> git.lizzy.rs Git - rust.git/blob - src/tools/tidy/src/lib.rs
Simplify SaveHandler trait
[rust.git] / src / tools / tidy / src / lib.rs
1 //! Library used by tidy and other tools.
2 //!
3 //! This library contains the tidy lints and exposes it
4 //! to be used by tools.
5
6 use walkdir::{DirEntry, WalkDir};
7 use std::fs::File;
8 use std::io::Read;
9
10 use std::path::Path;
11
12 macro_rules! t {
13     ($e:expr, $p:expr) => (match $e {
14         Ok(e) => e,
15         Err(e) => panic!("{} failed on {} with {}", stringify!($e), ($p).display(), e),
16     });
17
18     ($e:expr) => (match $e {
19         Ok(e) => e,
20         Err(e) => panic!("{} failed with {}", stringify!($e), e),
21     })
22 }
23
24 macro_rules! tidy_error {
25     ($bad:expr, $fmt:expr, $($arg:tt)*) => ({
26         *$bad = true;
27         eprint!("tidy error: ");
28         eprintln!($fmt, $($arg)*);
29     });
30 }
31
32 pub mod bins;
33 pub mod style;
34 pub mod errors;
35 pub mod features;
36 pub mod cargo;
37 pub mod pal;
38 pub mod deps;
39 pub mod extdeps;
40 pub mod ui_tests;
41 pub mod unstable_book;
42 pub mod libcoretest;
43
44 fn filter_dirs(path: &Path) -> bool {
45     let skip = [
46         "src/llvm",
47         "src/llvm-project",
48         "src/llvm-emscripten",
49         "src/libbacktrace",
50         "src/librustc_data_structures/owning_ref",
51         "src/vendor",
52         "src/tools/cargo",
53         "src/tools/clang",
54         "src/tools/rls",
55         "src/tools/clippy",
56         "src/tools/rust-installer",
57         "src/tools/rustfmt",
58         "src/tools/miri",
59         "src/tools/lld",
60         "src/tools/lldb",
61         "src/target",
62         "src/stdarch",
63         "src/rust-sgx",
64         "target",
65         "vendor",
66     ];
67     skip.iter().any(|p| path.ends_with(p))
68 }
69
70
71 fn walk_many(
72     paths: &[&Path], skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&DirEntry, &str)
73 ) {
74     for path in paths {
75         walk(path, skip, f);
76     }
77 }
78
79 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)) {
91     let walker = WalkDir::new(path).into_iter()
92         .filter_entry(|e| !skip(e.path()));
93     for entry in walker {
94         if let Ok(entry) = entry {
95             if entry.file_type().is_dir() {
96                 continue;
97             }
98             f(&entry);
99         }
100     }
101 }