about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2022-12-30 21:26:34 -0800
committerGitHub <noreply@github.com>2022-12-30 21:26:34 -0800
commit81808b7bafb3bf8991046de2bc38d783d92116a8 (patch)
treeae32a57d802cfd6f0a275984196709e01119925b
parent5b74a33b8d03da897553b42270cdab541d28b33f (diff)
parent75b3ee26cbeddcf2244e284d3c822d067cada2e2 (diff)
downloadrust-81808b7bafb3bf8991046de2bc38d783d92116a8.tar.gz
rust-81808b7bafb3bf8991046de2bc38d783d92116a8.zip
Rollup merge of #106286 - Nilstrieb:tidy-cowows, r=jyn514
Make tidy errors red

This makes it easier to see them (and makes people go owo).

I also changes the error codes check to not print too many things and use `tidy_error`.

r? ```@jyn514```
-rw-r--r--Cargo.lock9
-rw-r--r--src/tools/tidy/Cargo.toml1
-rw-r--r--src/tools/tidy/src/error_codes_check.rs37
-rw-r--r--src/tools/tidy/src/lib.rs31
4 files changed, 39 insertions, 39 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 9581099c210..f99e58e59b8 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2675,9 +2675,9 @@ dependencies = [
 
 [[package]]
 name = "owo-colors"
-version = "3.4.0"
+version = "3.5.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "decf7381921fea4dcb2549c5667eda59b3ec297ab7e2b5fc33eac69d2e7da87b"
+checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
 
 [[package]]
 name = "packed_simd_2"
@@ -5203,9 +5203,9 @@ dependencies = [
 
 [[package]]
 name = "termcolor"
-version = "1.1.2"
+version = "1.1.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
+checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
 dependencies = [
  "winapi-util",
 ]
@@ -5309,6 +5309,7 @@ dependencies = [
  "lazy_static",
  "miropt-test-tools",
  "regex",
+ "termcolor",
  "walkdir",
 ]
 
diff --git a/src/tools/tidy/Cargo.toml b/src/tools/tidy/Cargo.toml
index 97d038da702..fff83a1d097 100644
--- a/src/tools/tidy/Cargo.toml
+++ b/src/tools/tidy/Cargo.toml
@@ -11,6 +11,7 @@ miropt-test-tools = { path = "../miropt-test-tools" }
 lazy_static = "1"
 walkdir = "2"
 ignore = "0.4.18"
+termcolor = "1.1.3"
 
 [[bin]]
 name = "rust-tidy"
diff --git a/src/tools/tidy/src/error_codes_check.rs b/src/tools/tidy/src/error_codes_check.rs
index fd870b0997c..40a46c630d7 100644
--- a/src/tools/tidy/src/error_codes_check.rs
+++ b/src/tools/tidy/src/error_codes_check.rs
@@ -80,15 +80,6 @@ fn check_if_error_code_is_test_in_explanation(f: &str, err_code: &str) -> bool {
     ignore_found
 }
 
-macro_rules! some_or_continue {
-    ($e:expr) => {
-        match $e {
-            Some(e) => e,
-            None => continue,
-        }
-    };
-}
-
 fn extract_error_codes(
     f: &str,
     error_codes: &mut HashMap<String, ErrorCodeStatus>,
@@ -122,10 +113,16 @@ fn extract_error_codes(
                     Some((file_name, _)) => file_name,
                 },
             };
-            let path = some_or_continue!(path.parent())
+
+            let Some(parent) = path.parent() else {
+                continue;
+            };
+
+            let path = parent
                 .join(md_file_name)
                 .canonicalize()
                 .expect("failed to canonicalize error explanation file path");
+
             match read_to_string(&path) {
                 Ok(content) => {
                     let has_test = check_if_error_code_is_test_in_explanation(&content, &err_code);
@@ -215,8 +212,6 @@ pub fn check(paths: &[&Path], bad: &mut bool) {
     // * #[error = "E0111"]
     let regex = Regex::new(r#"[(,"\s](E\d{4})[,)"]"#).unwrap();
 
-    println!("Checking which error codes lack tests...");
-
     for path in paths {
         walk(path, &mut filter_dirs, &mut |entry, contents| {
             let file_name = entry.file_name();
@@ -245,20 +240,15 @@ pub fn check(paths: &[&Path], bad: &mut bool) {
         });
     }
     if found_explanations == 0 {
-        eprintln!("No error code explanation was tested!");
-        *bad = true;
+        tidy_error!(bad, "No error code explanation was tested!");
     }
     if found_tests == 0 {
-        eprintln!("No error code was found in compilation errors!");
-        *bad = true;
+        tidy_error!(bad, "No error code was found in compilation errors!");
     }
     if explanations.is_empty() {
-        eprintln!("No error code explanation was found!");
-        *bad = true;
+        tidy_error!(bad, "No error code explanation was found!");
     }
     if errors.is_empty() {
-        println!("Found {} error codes", error_codes.len());
-
         for (err_code, error_status) in &error_codes {
             if !error_status.has_test && !EXEMPTED_FROM_TEST.contains(&err_code.as_str()) {
                 errors.push(format!("Error code {err_code} needs to have at least one UI test!"));
@@ -310,11 +300,6 @@ pub fn check(paths: &[&Path], bad: &mut bool) {
     }
     errors.sort();
     for err in &errors {
-        eprintln!("{err}");
-    }
-    println!("Found {} error(s) in error codes", errors.len());
-    if !errors.is_empty() {
-        *bad = true;
+        tidy_error!(bad, "{err}");
     }
-    println!("Done!");
 }
diff --git a/src/tools/tidy/src/lib.rs b/src/tools/tidy/src/lib.rs
index 698e4850bea..ce7e7ac5cd4 100644
--- a/src/tools/tidy/src/lib.rs
+++ b/src/tools/tidy/src/lib.rs
@@ -3,6 +3,10 @@
 //! This library contains the tidy lints and exposes it
 //! to be used by tools.
 
+use std::fmt::Display;
+
+use termcolor::WriteColor;
+
 /// A helper macro to `unwrap` a result except also print out details like:
 ///
 /// * The expression that failed
@@ -26,18 +30,27 @@ macro_rules! t {
 }
 
 macro_rules! tidy_error {
-    ($bad:expr, $fmt:expr) => ({
-        *$bad = true;
-        eprint!("tidy error: ");
-        eprintln!($fmt);
-    });
-    ($bad:expr, $fmt:expr, $($arg:tt)*) => ({
-        *$bad = true;
-        eprint!("tidy error: ");
-        eprintln!($fmt, $($arg)*);
+    ($bad:expr, $($fmt:tt)*) => ({
+        $crate::tidy_error($bad, format_args!($($fmt)*)).expect("failed to output error");
     });
 }
 
+fn tidy_error(bad: &mut bool, args: impl Display) -> std::io::Result<()> {
+    use std::io::Write;
+    use termcolor::{Color, ColorChoice, ColorSpec, StandardStream};
+
+    *bad = true;
+
+    let mut stderr = StandardStream::stdout(ColorChoice::Auto);
+    stderr.set_color(ColorSpec::new().set_fg(Some(Color::Red)))?;
+
+    write!(&mut stderr, "tidy error")?;
+    stderr.set_color(&ColorSpec::new())?;
+
+    writeln!(&mut stderr, ": {args}")?;
+    Ok(())
+}
+
 pub mod alphabetical;
 pub mod bins;
 pub mod debug_artifacts;