]> git.lizzy.rs Git - rust.git/blob - clippy_dev/src/stderr_length_check.rs
Auto merge of #7316 - lengyijun:rc_mutex, r=llogiq
[rust.git] / clippy_dev / src / stderr_length_check.rs
1 use crate::clippy_project_root;
2 use std::ffi::OsStr;
3 use std::fs;
4 use std::path::{Path, PathBuf};
5 use walkdir::WalkDir;
6
7 // The maximum length allowed for stderr files.
8 //
9 // We limit this because small files are easier to deal with than bigger files.
10 const LENGTH_LIMIT: usize = 200;
11
12 pub fn check() {
13     let exceeding_files: Vec<_> = exceeding_stderr_files();
14
15     if !exceeding_files.is_empty() {
16         eprintln!("Error: stderr files exceeding limit of {} lines:", LENGTH_LIMIT);
17         for (path, count) in exceeding_files {
18             println!("{}: {}", path.display(), count);
19         }
20         std::process::exit(1);
21     }
22 }
23
24 fn exceeding_stderr_files() -> Vec<(PathBuf, usize)> {
25     // We use `WalkDir` instead of `fs::read_dir` here in order to recurse into subdirectories.
26     WalkDir::new(clippy_project_root().join("tests/ui"))
27         .into_iter()
28         .filter_map(Result::ok)
29         .filter(|f| !f.file_type().is_dir())
30         .filter_map(|e| {
31             let p = e.into_path();
32             let count = count_linenumbers(&p);
33             if p.extension() == Some(OsStr::new("stderr")) && count > LENGTH_LIMIT {
34                 Some((p, count))
35             } else {
36                 None
37             }
38         })
39         .collect()
40 }
41
42 #[must_use]
43 fn count_linenumbers(filepath: &Path) -> usize {
44     match fs::read(filepath) {
45         Ok(content) => bytecount::count(&content, b'\n'),
46         Err(e) => {
47             eprintln!("Failed to read file: {}", e);
48             0
49         },
50     }
51 }