]> git.lizzy.rs Git - rust.git/blob - src/tools/tidy/src/lib.rs
Auto merge of #63124 - Centril:rollup-onohtqt, r=Centril
[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 edition;
38 pub mod pal;
39 pub mod deps;
40 pub mod extdeps;
41 pub mod ui_tests;
42 pub mod unit_tests;
43 pub mod unstable_book;
44
45 fn filter_dirs(path: &Path) -> bool {
46     let skip = [
47         "src/llvm-emscripten",
48         "src/llvm-project",
49         "src/stdarch",
50         "src/tools/cargo",
51         "src/tools/clippy",
52         "src/tools/miri",
53         "src/tools/rls",
54         "src/tools/rust-installer",
55         "src/tools/rustfmt",
56     ];
57     skip.iter().any(|p| path.ends_with(p))
58 }
59
60 fn walk_many(
61     paths: &[&Path], skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&DirEntry, &str)
62 ) {
63     for path in paths {
64         walk(path, skip, f);
65     }
66 }
67
68 fn walk(path: &Path, skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&DirEntry, &str)) {
69     let mut contents = String::new();
70     walk_no_read(path, skip, &mut |entry| {
71         contents.clear();
72         if t!(File::open(entry.path()), entry.path()).read_to_string(&mut contents).is_err() {
73             contents.clear();
74         }
75         f(&entry, &contents);
76     });
77 }
78
79 fn walk_no_read(path: &Path, skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&DirEntry)) {
80     let walker = WalkDir::new(path).into_iter()
81         .filter_entry(|e| !skip(e.path()));
82     for entry in walker {
83         if let Ok(entry) = entry {
84             if entry.file_type().is_dir() {
85                 continue;
86             }
87             f(&entry);
88         }
89     }
90 }