Merge pull request #3320 from phansch/riir_update_lints_use_walkdir

RIIR update_lints: use WalkDir instead of read_dir
This commit is contained in:
Philipp Hansch 2018-10-16 07:13:45 +02:00 committed by GitHub
commit eb683e6070
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 5 deletions

View file

@ -9,3 +9,4 @@ clap = "~2.32"
itertools = "0.7" itertools = "0.7"
regex = "1" regex = "1"
lazy_static = "1.0" lazy_static = "1.0"
walkdir = "2"

View file

@ -14,6 +14,7 @@
use itertools::Itertools; use itertools::Itertools;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use regex::Regex; use regex::Regex;
use walkdir::WalkDir;
use std::collections::HashMap; use std::collections::HashMap;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::fs; use std::fs;
@ -35,6 +36,7 @@ lazy_static! {
pub static ref DOCS_LINK: String = "https://rust-lang-nursery.github.io/rust-clippy/master/index.html".to_string(); pub static ref DOCS_LINK: String = "https://rust-lang-nursery.github.io/rust-clippy/master/index.html".to_string();
} }
/// Lint data parsed from the Clippy source code.
#[derive(Clone, PartialEq, Debug)] #[derive(Clone, PartialEq, Debug)]
pub struct Lint { pub struct Lint {
pub name: String, pub name: String,
@ -66,11 +68,12 @@ impl Lint {
} }
} }
/// Gathers all files in `src/clippy_lints` and gathers all lints inside
pub fn gather_all() -> impl Iterator<Item=Lint> { pub fn gather_all() -> impl Iterator<Item=Lint> {
lint_files().flat_map(|f| gather_from_file(&f)) lint_files().flat_map(|f| gather_from_file(&f))
} }
fn gather_from_file(dir_entry: &fs::DirEntry) -> impl Iterator<Item=Lint> { fn gather_from_file(dir_entry: &walkdir::DirEntry) -> impl Iterator<Item=Lint> {
let mut file = fs::File::open(dir_entry.path()).unwrap(); let mut file = fs::File::open(dir_entry.path()).unwrap();
let mut content = String::new(); let mut content = String::new();
file.read_to_string(&mut content).unwrap(); file.read_to_string(&mut content).unwrap();
@ -89,9 +92,11 @@ fn parse_contents(content: &str, filename: &str) -> impl Iterator<Item=Lint> {
} }
/// Collects all .rs files in the `clippy_lints/src` directory /// Collects all .rs files in the `clippy_lints/src` directory
fn lint_files() -> impl Iterator<Item=fs::DirEntry> { fn lint_files() -> impl Iterator<Item=walkdir::DirEntry> {
fs::read_dir("../clippy_lints/src") // We use `WalkDir` instead of `fs::read_dir` here in order to recurse into subdirectories.
.unwrap() // Otherwise we would not collect all the lints, for example in `clippy_lints/src/methods/`.
WalkDir::new("../clippy_lints/src")
.into_iter()
.filter_map(|f| f.ok()) .filter_map(|f| f.ok())
.filter(|f| f.path().extension() == Some(OsStr::new("rs"))) .filter(|f| f.path().extension() == Some(OsStr::new("rs")))
} }

View file

@ -44,7 +44,7 @@ fn print_lints() {
if lint_group == "Deprecated" { continue; } if lint_group == "Deprecated" { continue; }
println!("\n## {}", lint_group); println!("\n## {}", lint_group);
lints.sort_by(|a, b| a.name.cmp(&b.name)); lints.sort_by_key(|l| l.name.clone());
for lint in lints { for lint in lints {
println!("* [{}]({}#{}) ({})", lint.name, clippy_dev::DOCS_LINK.clone(), lint.name, lint.desc); println!("* [{}]({}#{}) ({})", lint.name, clippy_dev::DOCS_LINK.clone(), lint.name, lint.desc);