diff options
| author | bors <bors@rust-lang.org> | 2020-02-06 15:31:01 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2020-02-06 15:31:01 +0000 |
| commit | a6f310ed0e22a422e7001efbe7e4bf2a0c9a0eff (patch) | |
| tree | 8fdfab8eccf00d8bb32d5efe848344dda326c4ea | |
| parent | 75e983a5187bbce1ca54613620f432459a16e5d9 (diff) | |
| parent | 8794e41728756e2a284ebcb9c666c6efcda62f8b (diff) | |
| download | rust-a6f310ed0e22a422e7001efbe7e4bf2a0c9a0eff.tar.gz rust-a6f310ed0e22a422e7001efbe7e4bf2a0c9a0eff.zip | |
Auto merge of #5139 - lzutao:linecount, r=llogiq
dev: Use bytecount for faster line count changelog: none
| -rw-r--r-- | clippy_dev/Cargo.toml | 1 | ||||
| -rw-r--r-- | clippy_dev/src/stderr_length_check.rs | 55 | ||||
| -rw-r--r-- | clippy_lints/src/missing_doc.rs | 2 |
3 files changed, 27 insertions, 31 deletions
diff --git a/clippy_dev/Cargo.toml b/clippy_dev/Cargo.toml index b30ba61d299..c8aa1e79f34 100644 --- a/clippy_dev/Cargo.toml +++ b/clippy_dev/Cargo.toml @@ -5,6 +5,7 @@ authors = ["Philipp Hansch <dev@phansch.net>"] edition = "2018" [dependencies] +bytecount = "0.6" clap = "2.33" itertools = "0.8" regex = "1" diff --git a/clippy_dev/src/stderr_length_check.rs b/clippy_dev/src/stderr_length_check.rs index e36feb3a271..041ee691137 100644 --- a/clippy_dev/src/stderr_length_check.rs +++ b/clippy_dev/src/stderr_length_check.rs @@ -1,54 +1,49 @@ use std::ffi::OsStr; -use walkdir::WalkDir; +use std::fs; +use std::path::{Path, PathBuf}; -use std::fs::File; -use std::io::prelude::*; +use walkdir::WalkDir; // The maximum length allowed for stderr files. // // We limit this because small files are easier to deal with than bigger files. -const LIMIT: usize = 200; +const LENGTH_LIMIT: usize = 200; pub fn check() { - let stderr_files = stderr_files(); - let exceeding_files = exceeding_stderr_files(stderr_files).collect::<Vec<String>>(); + let exceeding_files: Vec<_> = exceeding_stderr_files(); if !exceeding_files.is_empty() { - eprintln!("Error: stderr files exceeding limit of {} lines:", LIMIT); + eprintln!("Error: stderr files exceeding limit of {} lines:", LENGTH_LIMIT); for path in exceeding_files { - println!("{}", path); + println!("{}", path.display()); } std::process::exit(1); } } -fn exceeding_stderr_files(files: impl Iterator<Item = walkdir::DirEntry>) -> impl Iterator<Item = String> { - files.filter_map(|file| { - let path = file.path().to_str().expect("Could not convert path to str").to_string(); - let linecount = count_linenumbers(&path); - if linecount > LIMIT { - Some(path) - } else { - None - } - }) -} - -fn stderr_files() -> impl Iterator<Item = walkdir::DirEntry> { +fn exceeding_stderr_files() -> Vec<PathBuf> { // We use `WalkDir` instead of `fs::read_dir` here in order to recurse into subdirectories. WalkDir::new("../tests/ui") .into_iter() - .filter_map(std::result::Result::ok) - .filter(|f| f.path().extension() == Some(OsStr::new("stderr"))) + .filter_map(Result::ok) + .filter_map(|e| { + let p = e.into_path(); + if p.extension() == Some(OsStr::new("stderr")) && count_linenumbers(&p) > LENGTH_LIMIT { + Some(p) + } else { + None + } + }) + .collect() } #[must_use] -fn count_linenumbers(filepath: &str) -> usize { - if let Ok(mut file) = File::open(filepath) { - let mut content = String::new(); - file.read_to_string(&mut content).expect("Failed to read file?"); - content.lines().count() - } else { - 0 +fn count_linenumbers(filepath: &Path) -> usize { + match fs::read(filepath) { + Ok(content) => bytecount::count(&content, b'\n'), + Err(e) => { + eprintln!("Failed to read file: {}", e); + 0 + }, } } diff --git a/clippy_lints/src/missing_doc.rs b/clippy_lints/src/missing_doc.rs index 5ebc8a4d647..e502db59e66 100644 --- a/clippy_lints/src/missing_doc.rs +++ b/clippy_lints/src/missing_doc.rs @@ -36,7 +36,7 @@ pub struct MissingDoc { doc_hidden_stack: Vec<bool>, } -impl ::std::default::Default for MissingDoc { +impl Default for MissingDoc { #[must_use] fn default() -> Self { Self::new() |
