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