Skip to content

Commit a6f310e

Browse files
committed
Auto merge of #5139 - lzutao:linecount, r=llogiq
dev: Use bytecount for faster line count changelog: none
2 parents 75e983a + 8794e41 commit a6f310e

File tree

3 files changed

+27
-31
lines changed

3 files changed

+27
-31
lines changed

clippy_dev/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ authors = ["Philipp Hansch <[email protected]>"]
55
edition = "2018"
66

77
[dependencies]
8+
bytecount = "0.6"
89
clap = "2.33"
910
itertools = "0.8"
1011
regex = "1"

clippy_dev/src/stderr_length_check.rs

+25-30
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,49 @@
11
use std::ffi::OsStr;
2-
use walkdir::WalkDir;
2+
use std::fs;
3+
use std::path::{Path, PathBuf};
34

4-
use std::fs::File;
5-
use std::io::prelude::*;
5+
use walkdir::WalkDir;
66

77
// The maximum length allowed for stderr files.
88
//
99
// We limit this because small files are easier to deal with than bigger files.
10-
const LIMIT: usize = 200;
10+
const LENGTH_LIMIT: usize = 200;
1111

1212
pub fn check() {
13-
let stderr_files = stderr_files();
14-
let exceeding_files = exceeding_stderr_files(stderr_files).collect::<Vec<String>>();
13+
let exceeding_files: Vec<_> = exceeding_stderr_files();
1514

1615
if !exceeding_files.is_empty() {
17-
eprintln!("Error: stderr files exceeding limit of {} lines:", LIMIT);
16+
eprintln!("Error: stderr files exceeding limit of {} lines:", LENGTH_LIMIT);
1817
for path in exceeding_files {
19-
println!("{}", path);
18+
println!("{}", path.display());
2019
}
2120
std::process::exit(1);
2221
}
2322
}
2423

25-
fn exceeding_stderr_files(files: impl Iterator<Item = walkdir::DirEntry>) -> impl Iterator<Item = String> {
26-
files.filter_map(|file| {
27-
let path = file.path().to_str().expect("Could not convert path to str").to_string();
28-
let linecount = count_linenumbers(&path);
29-
if linecount > LIMIT {
30-
Some(path)
31-
} else {
32-
None
33-
}
34-
})
35-
}
36-
37-
fn stderr_files() -> impl Iterator<Item = walkdir::DirEntry> {
24+
fn exceeding_stderr_files() -> Vec<PathBuf> {
3825
// We use `WalkDir` instead of `fs::read_dir` here in order to recurse into subdirectories.
3926
WalkDir::new("../tests/ui")
4027
.into_iter()
41-
.filter_map(std::result::Result::ok)
42-
.filter(|f| f.path().extension() == Some(OsStr::new("stderr")))
28+
.filter_map(Result::ok)
29+
.filter_map(|e| {
30+
let p = e.into_path();
31+
if p.extension() == Some(OsStr::new("stderr")) && count_linenumbers(&p) > LENGTH_LIMIT {
32+
Some(p)
33+
} else {
34+
None
35+
}
36+
})
37+
.collect()
4338
}
4439

4540
#[must_use]
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
41+
fn count_linenumbers(filepath: &Path) -> usize {
42+
match fs::read(filepath) {
43+
Ok(content) => bytecount::count(&content, b'\n'),
44+
Err(e) => {
45+
eprintln!("Failed to read file: {}", e);
46+
0
47+
},
5348
}
5449
}

clippy_lints/src/missing_doc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub struct MissingDoc {
3636
doc_hidden_stack: Vec<bool>,
3737
}
3838

39-
impl ::std::default::Default for MissingDoc {
39+
impl Default for MissingDoc {
4040
#[must_use]
4141
fn default() -> Self {
4242
Self::new()

0 commit comments

Comments
 (0)