Skip to content

Commit

Permalink
Merge pull request #3320 from phansch/riir_update_lints_use_walkdir
Browse files Browse the repository at this point in the history
RIIR update_lints: use WalkDir instead of read_dir
  • Loading branch information
phansch committed Oct 16, 2018
2 parents 78cd2c8 + b5dd8f1 commit eb683e6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions clippy_dev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ clap = "~2.32"
itertools = "0.7"
regex = "1"
lazy_static = "1.0"
walkdir = "2"
13 changes: 9 additions & 4 deletions clippy_dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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> {
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 content = String::new();
file.read_to_string(&mut content).unwrap();
Expand All @@ -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
fn lint_files() -> impl Iterator<Item=fs::DirEntry> {
fs::read_dir("../clippy_lints/src")
.unwrap()
fn lint_files() -> impl Iterator<Item=walkdir::DirEntry> {
// 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")))
}
Expand Down
2 changes: 1 addition & 1 deletion clippy_dev/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit eb683e6

Please sign in to comment.