]> git.lizzy.rs Git - rust.git/blob - src/tools/tidy/src/lib.rs
Rollup merge of #106175 - compiler-errors:bad-import-sugg, r=oli-obk
[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::fmt::Display;
7
8 use termcolor::WriteColor;
9
10 /// A helper macro to `unwrap` a result except also print out details like:
11 ///
12 /// * The expression that failed
13 /// * The error itself
14 /// * (optionally) a path connected to the error (e.g. failure to open a file)
15 #[macro_export]
16 macro_rules! t {
17     ($e:expr, $p:expr) => {
18         match $e {
19             Ok(e) => e,
20             Err(e) => panic!("{} failed on {} with {}", stringify!($e), ($p).display(), e),
21         }
22     };
23
24     ($e:expr) => {
25         match $e {
26             Ok(e) => e,
27             Err(e) => panic!("{} failed with {}", stringify!($e), e),
28         }
29     };
30 }
31
32 macro_rules! tidy_error {
33     ($bad:expr, $($fmt:tt)*) => ({
34         $crate::tidy_error($bad, format_args!($($fmt)*)).expect("failed to output error");
35     });
36 }
37
38 fn tidy_error(bad: &mut bool, args: impl Display) -> std::io::Result<()> {
39     use std::io::Write;
40     use termcolor::{Color, ColorChoice, ColorSpec, StandardStream};
41
42     *bad = true;
43
44     let mut stderr = StandardStream::stdout(ColorChoice::Auto);
45     stderr.set_color(ColorSpec::new().set_fg(Some(Color::Red)))?;
46
47     write!(&mut stderr, "tidy error")?;
48     stderr.set_color(&ColorSpec::new())?;
49
50     writeln!(&mut stderr, ": {args}")?;
51     Ok(())
52 }
53
54 pub mod alphabetical;
55 pub mod bins;
56 pub mod debug_artifacts;
57 pub mod deps;
58 pub mod edition;
59 pub mod error_codes;
60 pub mod extdeps;
61 pub mod features;
62 pub mod mir_opt_tests;
63 pub mod pal;
64 pub mod primitive_docs;
65 pub mod style;
66 pub mod target_specific_tests;
67 pub mod ui_tests;
68 pub mod unit_tests;
69 pub mod unstable_book;
70 pub mod walk;
71 pub mod x_version;