]> git.lizzy.rs Git - rust.git/blob - src/tools/tidy/src/lib.rs
Auto merge of #47804 - retep007:recursive-requirements, r=pnkfelix
[rust.git] / src / tools / tidy / src / lib.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Library used by tidy and other tools
12 //!
13 //! This library contains the tidy lints and exposes it
14 //! to be used by tools.
15
16 #![deny(warnings)]
17
18 use std::fs;
19
20 use std::path::Path;
21
22 macro_rules! t {
23     ($e:expr, $p:expr) => (match $e {
24         Ok(e) => e,
25         Err(e) => panic!("{} failed on {} with {}", stringify!($e), ($p).display(), e),
26     });
27
28     ($e:expr) => (match $e {
29         Ok(e) => e,
30         Err(e) => panic!("{} failed with {}", stringify!($e), e),
31     })
32 }
33
34 macro_rules! tidy_error {
35     ($bad:expr, $fmt:expr, $($arg:tt)*) => ({
36         *$bad = true;
37         eprint!("tidy error: ");
38         eprintln!($fmt, $($arg)*);
39     });
40 }
41
42 pub mod bins;
43 pub mod style;
44 pub mod errors;
45 pub mod features;
46 pub mod cargo;
47 pub mod pal;
48 pub mod deps;
49 pub mod unstable_book;
50
51 fn filter_dirs(path: &Path) -> bool {
52     let skip = [
53         "src/binaryen",
54         "src/dlmalloc",
55         "src/jemalloc",
56         "src/llvm",
57         "src/llvm-emscripten",
58         "src/libbacktrace",
59         "src/libcompiler_builtins",
60         "src/librustc_data_structures/owning_ref",
61         "src/compiler-rt",
62         "src/liblibc",
63         "src/vendor",
64         "src/rt/hoedown",
65         "src/tools/cargo",
66         "src/tools/rls",
67         "src/tools/clippy",
68         "src/tools/rust-installer",
69         "src/tools/rustfmt",
70         "src/tools/miri",
71         "src/librustc/mir/interpret",
72         "src/librustc_mir/interpret",
73         "src/target",
74     ];
75     skip.iter().any(|p| path.ends_with(p))
76 }
77
78 fn walk_many(paths: &[&Path], skip: &mut FnMut(&Path) -> bool, f: &mut FnMut(&Path)) {
79     for path in paths {
80         walk(path, skip, f);
81     }
82 }
83
84 fn walk(path: &Path, skip: &mut FnMut(&Path) -> bool, f: &mut FnMut(&Path)) {
85     for entry in t!(fs::read_dir(path), path) {
86         let entry = t!(entry);
87         let kind = t!(entry.file_type());
88         let path = entry.path();
89         if kind.is_dir() {
90             if !skip(&path) {
91                 walk(&path, skip, f);
92             }
93         } else {
94             f(&path);
95         }
96     }
97 }