]> git.proxmox.com Git - rustc.git/blame - src/tools/tidy/src/lib.rs
New upstream version 1.53.0+dfsg1
[rustc.git] / src / tools / tidy / src / lib.rs
CommitLineData
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
6use std::fs::File;
7use std::io::Read;
dfeec247 8use walkdir::{DirEntry, WalkDir};
041b39d2
XL
9
10use std::path::Path;
11
12macro_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
28macro_rules! tidy_error {
fc512014
XL
29 ($bad:expr, $fmt:expr) => ({
30 *$bad = true;
31 eprintln!("tidy error: {}", $fmt);
32 });
041b39d2 33 ($bad:expr, $fmt:expr, $($arg:tt)*) => ({
041b39d2 34 *$bad = true;
abe05a73
XL
35 eprint!("tidy error: ");
36 eprintln!($fmt, $($arg)*);
041b39d2
XL
37 });
38}
39
40pub mod bins;
e74abb32 41pub mod debug_artifacts;
dfeec247
XL
42pub mod deps;
43pub mod edition;
44pub mod error_codes_check;
041b39d2 45pub mod errors;
dfeec247 46pub mod extdeps;
041b39d2 47pub mod features;
041b39d2 48pub mod pal;
dfeec247 49pub mod style;
0531ce1d 50pub mod ui_tests;
416331ca 51pub mod unit_tests;
041b39d2
XL
52pub mod unstable_book;
53
54fn filter_dirs(path: &Path) -> bool {
55 let skip = [
cdc7bbd5 56 "tidy-test-file",
29967ef6 57 "compiler/rustc_codegen_cranelift",
416331ca 58 "src/llvm-project",
3dfed10e
XL
59 "library/backtrace",
60 "library/stdarch",
041b39d2 61 "src/tools/cargo",
ea8adc8c 62 "src/tools/clippy",
416331ca
XL
63 "src/tools/miri",
64 "src/tools/rls",
f035d41b 65 "src/tools/rust-analyzer",
041b39d2 66 "src/tools/rust-installer",
ea8adc8c 67 "src/tools/rustfmt",
74b04a01 68 "src/doc/book",
e74abb32
XL
69 // Filter RLS output directories
70 "target/rls",
041b39d2
XL
71 ];
72 skip.iter().any(|p| path.ends_with(p))
73}
74
dc9dc135 75fn walk_many(
dfeec247
XL
76 paths: &[&Path],
77 skip: &mut dyn FnMut(&Path) -> bool,
78 f: &mut dyn FnMut(&DirEntry, &str),
dc9dc135 79) {
041b39d2
XL
80 for path in paths {
81 walk(path, skip, f);
82 }
83}
84
dc9dc135
XL
85fn walk(path: &Path, skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&DirEntry, &str)) {
86 let mut contents = String::new();
87 walk_no_read(path, skip, &mut |entry| {
88 contents.clear();
89 if t!(File::open(entry.path()), entry.path()).read_to_string(&mut contents).is_err() {
90 contents.clear();
91 }
92 f(&entry, &contents);
93 });
94}
95
96fn walk_no_read(path: &Path, skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&DirEntry)) {
dfeec247 97 let walker = WalkDir::new(path).into_iter().filter_entry(|e| !skip(e.path()));
dc9dc135
XL
98 for entry in walker {
99 if let Ok(entry) = entry {
100 if entry.file_type().is_dir() {
101 continue;
041b39d2 102 }
dc9dc135 103 f(&entry);
041b39d2
XL
104 }
105 }
106}