]> git.lizzy.rs Git - rust.git/blob - clippy_dev/src/stderr_length_check.rs
Auto merge of #4099 - flip1995:ul_4094, r=oli-obk
[rust.git] / clippy_dev / src / stderr_length_check.rs
1 use std::ffi::OsStr;
2 use walkdir::WalkDir;
3
4 use std::fs::File;
5 use std::io::prelude::*;
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 LIMIT: usize = 320;
11
12 pub fn check() {
13     let stderr_files = stderr_files();
14     let exceeding_files = exceeding_stderr_files(stderr_files).collect::<Vec<String>>();
15
16     if !exceeding_files.is_empty() {
17         eprintln!("Error: stderr files exceeding limit of {} lines:", LIMIT);
18         for path in exceeding_files {
19             println!("{}", path);
20         }
21         std::process::exit(1);
22     }
23 }
24
25 fn exceeding_stderr_files(files: impl Iterator<Item = walkdir::DirEntry>) -> impl Iterator<Item = String> {
26     files
27         .filter_map(|file| {
28             let path = file.path().to_str().expect("Could not convert path to str").to_string();
29             let linecount = count_linenumbers(&path);
30             if linecount > LIMIT {
31                 Some(path)
32             } else {
33                 None
34             }
35         })
36 }
37
38 fn stderr_files() -> impl Iterator<Item = walkdir::DirEntry> {
39     // We use `WalkDir` instead of `fs::read_dir` here in order to recurse into subdirectories.
40     WalkDir::new("../tests/ui")
41         .into_iter()
42         .filter_map(std::result::Result::ok)
43         .filter(|f| f.path().extension() == Some(OsStr::new("stderr")))
44 }
45
46 fn count_linenumbers(filepath: &str) -> usize {
47     if let Ok(mut file) = File::open(filepath) {
48         let mut content = String::new();
49         file.read_to_string(&mut content).expect("Failed to read file?");
50         content.lines().count()
51     } else {
52         0
53     }
54 }