]> git.lizzy.rs Git - rust.git/blob - src/tools/tidy/src/lib.rs
tidy: forbid since values for features that point to the current release or future...
[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         eprint!("tidy error: ");
32         eprintln!($fmt);
33     });
34     ($bad:expr, $fmt:expr, $($arg:tt)*) => ({
35         *$bad = true;
36         eprint!("tidy error: ");
37         eprintln!($fmt, $($arg)*);
38     });
39 }
40
41 pub mod bins;
42 pub mod debug_artifacts;
43 pub mod deps;
44 pub mod edition;
45 pub mod error_codes_check;
46 pub mod errors;
47 pub mod extdeps;
48 pub mod features;
49 pub mod pal;
50 pub mod primitive_docs;
51 pub mod style;
52 pub mod target_specific_tests;
53 pub mod ui_tests;
54 pub mod unit_tests;
55 pub mod unstable_book;
56
57 fn filter_dirs(path: &Path) -> bool {
58     let skip = [
59         "tidy-test-file",
60         "compiler/rustc_codegen_cranelift",
61         "compiler/rustc_codegen_gcc",
62         "src/llvm-project",
63         "library/backtrace",
64         "library/portable-simd",
65         "library/stdarch",
66         "src/tools/cargo",
67         "src/tools/clippy",
68         "src/tools/miri",
69         "src/tools/rls",
70         "src/tools/rust-analyzer",
71         "src/tools/rust-installer",
72         "src/tools/rustfmt",
73         "src/doc/book",
74         // Filter RLS output directories
75         "target/rls",
76     ];
77     skip.iter().any(|p| path.ends_with(p))
78 }
79
80 fn walk_many(
81     paths: &[&Path],
82     skip: &mut dyn FnMut(&Path) -> bool,
83     f: &mut dyn FnMut(&DirEntry, &str),
84 ) {
85     for path in paths {
86         walk(path, skip, f);
87     }
88 }
89
90 fn walk(path: &Path, skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&DirEntry, &str)) {
91     let mut contents = String::new();
92     walk_no_read(path, skip, &mut |entry| {
93         contents.clear();
94         if t!(File::open(entry.path()), entry.path()).read_to_string(&mut contents).is_err() {
95             contents.clear();
96         }
97         f(&entry, &contents);
98     });
99 }
100
101 fn walk_no_read(path: &Path, skip: &mut dyn FnMut(&Path) -> bool, f: &mut dyn FnMut(&DirEntry)) {
102     let walker = WalkDir::new(path).into_iter().filter_entry(|e| !skip(e.path()));
103     for entry in walker {
104         if let Ok(entry) = entry {
105             if entry.file_type().is_dir() {
106                 continue;
107             }
108             f(&entry);
109         }
110     }
111 }