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