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