diff --git a/clippy_dev/src/lib.rs b/clippy_dev/src/lib.rs index 8eabf3a97..c49993434 100644 --- a/clippy_dev/src/lib.rs +++ b/clippy_dev/src/lib.rs @@ -46,8 +46,9 @@ impl Lint { } } - pub fn active_lints(lints: &[Lint]) -> Vec { - lints.iter().filter(|l| l.deprecation.is_none()).cloned().collect::>() + /// Returns all non-deprecated lints + pub fn active_lints(lints: &[Lint]) -> impl Iterator { + lints.iter().filter(|l| l.deprecation.is_none()) } /// Returns the lints in a HashMap, grouped by the different lint groups @@ -56,22 +57,22 @@ impl Lint { } } -pub fn collect_all() -> Vec { +pub fn gather_all() -> impl Iterator { let mut lints = vec![]; for dir_entry in lint_files() { - lints.append(&mut collect_from_file(&dir_entry)); + lints.append(&mut gather_from_file(&dir_entry).collect()); } - lints + lints.into_iter() } -fn collect_from_file(dir_entry: &fs::DirEntry) -> Vec { +fn gather_from_file(dir_entry: &fs::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(); parse_contents(&content, dir_entry.path().file_stem().unwrap().to_str().unwrap()) } -fn parse_contents(content: &str, filename: &str) -> Vec { +fn parse_contents(content: &str, filename: &str) -> impl Iterator { let mut lints: Vec = DEC_CLIPPY_LINT_RE .captures_iter(&content) .map(|m| Lint::new(&m["name"], &m["cat"], &m["desc"], None, filename)) @@ -81,21 +82,20 @@ fn parse_contents(content: &str, filename: &str) -> Vec { .map(|m| Lint::new( &m["name"], "Deprecated", &m["desc"], Some(&m["desc"]), filename)) .collect(); lints.append(&mut deprecated); - lints + lints.into_iter() } /// Collects all .rs files in the `clippy_lints/src` directory -fn lint_files() -> Vec { +fn lint_files() -> impl Iterator { let paths = fs::read_dir("../clippy_lints/src").unwrap(); paths .filter_map(|f| f.ok()) .filter(|f| f.path().extension() == Some(OsStr::new("rs"))) - .collect::>() } #[test] fn test_parse_contents() { - let result = parse_contents( + let result: Vec = parse_contents( r#" declare_clippy_lint! { pub PTR_ARG, @@ -116,7 +116,7 @@ declare_deprecated_lint! { "`assert!()` will be more flexible with RFC 2011" } "#, - "module_name"); + "module_name").collect(); let expected = vec![ Lint::new("ptr_arg", "style", "really long text", None, "module_name"), @@ -141,7 +141,7 @@ fn test_active_lints() { let expected = vec![ Lint::new("should_assert_eq2", "Not Deprecated", "abc", None, "module_name") ]; - assert_eq!(expected, Lint::active_lints(&lints)); + assert_eq!(expected, Lint::active_lints(&lints).cloned().collect::>()); } #[test] diff --git a/clippy_dev/src/main.rs b/clippy_dev/src/main.rs index 6b55a7b7b..f45c52e22 100644 --- a/clippy_dev/src/main.rs +++ b/clippy_dev/src/main.rs @@ -27,7 +27,7 @@ fn main() { } fn print_lints() { - let lint_list = collect_all(); + let lint_list = gather_all().collect::>(); let grouped_by_lint_group = Lint::by_lint_group(&lint_list); for (lint_group, mut lints) in grouped_by_lint_group { @@ -41,5 +41,5 @@ fn print_lints() { } } - println!("there are {} lints", Lint::active_lints(&lint_list).len()); + println!("there are {} lints", Lint::active_lints(&lint_list).count()); }