diff --git a/clippy_dev/Cargo.toml b/clippy_dev/Cargo.toml index d6057ba97..5380ecd98 100644 --- a/clippy_dev/Cargo.toml +++ b/clippy_dev/Cargo.toml @@ -9,3 +9,4 @@ clap = "~2.32" itertools = "0.7" regex = "1" lazy_static = "1.0" +walkdir = "2" diff --git a/clippy_dev/src/lib.rs b/clippy_dev/src/lib.rs index eee9089e7..8477183ae 100644 --- a/clippy_dev/src/lib.rs +++ b/clippy_dev/src/lib.rs @@ -14,6 +14,7 @@ use itertools::Itertools; use lazy_static::lazy_static; use regex::Regex; +use walkdir::WalkDir; use std::collections::HashMap; use std::ffi::OsStr; 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(); } +/// Lint data parsed from the Clippy source code. #[derive(Clone, PartialEq, Debug)] pub struct Lint { 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 { lint_files().flat_map(|f| gather_from_file(&f)) } -fn gather_from_file(dir_entry: &fs::DirEntry) -> impl Iterator { +fn gather_from_file(dir_entry: &walkdir::DirEntry) -> impl Iterator { let mut file = fs::File::open(dir_entry.path()).unwrap(); let mut content = String::new(); file.read_to_string(&mut content).unwrap(); @@ -89,9 +92,11 @@ fn parse_contents(content: &str, filename: &str) -> impl Iterator { } /// Collects all .rs files in the `clippy_lints/src` directory -fn lint_files() -> impl Iterator { - fs::read_dir("../clippy_lints/src") - .unwrap() +fn lint_files() -> impl Iterator { + // We use `WalkDir` instead of `fs::read_dir` here in order to recurse into subdirectories. + // 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(|f| f.path().extension() == Some(OsStr::new("rs"))) } diff --git a/clippy_dev/src/main.rs b/clippy_dev/src/main.rs index d1161323b..9e78def78 100644 --- a/clippy_dev/src/main.rs +++ b/clippy_dev/src/main.rs @@ -44,7 +44,7 @@ fn print_lints() { if lint_group == "Deprecated" { continue; } 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 { println!("* [{}]({}#{}) ({})", lint.name, clippy_dev::DOCS_LINK.clone(), lint.name, lint.desc);