]> git.lizzy.rs Git - rust.git/blob - src/tools/tidy/src/lib.rs
Auto merge of #93176 - danielhenrymantilla:stack-pinning-macro, r=m-ou-se
[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 std::fs::File;
7 use std::io::Read;
8 use walkdir::{DirEntry, WalkDir};
9
10 use std::path::Path;
11
12 macro_rules! t {
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     };
19
20     ($e:expr) => {
21         match $e {
22             Ok(e) => e,
23             Err(e) => panic!("{} failed with {}", stringify!($e), e),
24         }
25     };
26 }
27
28 macro_rules! tidy_error {
29     ($bad:expr, $fmt:expr) => ({
30         *$bad = true;
31         eprintln!("tidy error: {}", $fmt);
32     });
33     ($bad:expr, $fmt:expr, $($arg:tt)*) => ({
34         *$bad = true;
35         eprint!("tidy error: ");
36         eprintln!($fmt, $($arg)*);
37     });
38 }
39
40 pub mod bins;
41 pub mod debug_artifacts;
42 pub mod deps;
43 pub mod edition;
44 pub mod error_codes_check;
45 pub mod errors;
46 pub mod extdeps;
47 pub mod features;
48 pub mod pal;
49 pub mod primitive_docs;
50 pub mod style;
51 pub mod target_specific_tests;
52 pub mod ui_tests;
53 pub mod unit_tests;
54 pub mod unstable_book;
55
56 fn filter_dirs(path: &Path) -> bool {
57     let skip = [
58         "tidy-test-file",
59         "compiler/rustc_codegen_cranelift",
60         "compiler/rustc_codegen_gcc",
61         "src/llvm-project",
62         "library/backtrace",
63         "library/portable-simd",
64         "library/stdarch",
65         "src/tools/cargo",
66         "src/tools/clippy",
67         "src/tools/miri",
68         "src/tools/rls",
69         "src/tools/rust-analyzer",
70         "src/tools/rust-installer",
71         "src/tools/rustfmt",
72         "src/doc/book",
73         // Filter RLS output directories
74         "target/rls",
75     ];
76     skip.iter().any(|p| path.ends_with(p))
77 }
78
79 fn walk_many(
80     paths: &[&Path],
81     skip: &mut dyn FnMut(&Path) -> bool,
82     f: &mut dyn FnMut(&DirEntry, &str),
83 ) {
84     for path in paths {
85         walk(path, skip, f);
86     }
87 }
88
89 fn walk(path: &Path, skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&DirEntry, &str)) {
90     let mut contents = String::new();
91     walk_no_read(path, skip, &mut |entry| {
92         contents.clear();
93         if t!(File::open(entry.path()), entry.path()).read_to_string(&mut contents).is_err() {
94             contents.clear();
95         }
96         f(&entry, &contents);
97     });
98 }
99
100 fn walk_no_read(path: &Path, skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&DirEntry)) {
101     let walker = WalkDir::new(path).into_iter().filter_entry(|e| !skip(e.path()));
102     for entry in walker {
103         if let Ok(entry) = entry {
104             if entry.file_type().is_dir() {
105                 continue;
106             }
107             f(&entry);
108         }
109     }
110 }