Skip to content

Commit 79bec03

Browse files
committed
Return impl Iterator instead of Vec<Lint>
This makes the API of `lib.rs` a bit more flexible.
1 parent be995dc commit 79bec03

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

clippy_dev/src/lib.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ impl Lint {
4646
}
4747
}
4848

49-
pub fn active_lints(lints: &[Lint]) -> Vec<Lint> {
50-
lints.iter().filter(|l| l.deprecation.is_none()).cloned().collect::<Vec<Lint>>()
49+
/// Returns all non-deprecated lints
50+
pub fn active_lints(lints: &[Lint]) -> impl Iterator<Item=&Lint> {
51+
lints.iter().filter(|l| l.deprecation.is_none())
5152
}
5253

5354
/// Returns the lints in a HashMap, grouped by the different lint groups
@@ -56,22 +57,22 @@ impl Lint {
5657
}
5758
}
5859

59-
pub fn collect_all() -> Vec<Lint> {
60+
pub fn gather_all() -> impl Iterator<Item=Lint> {
6061
let mut lints = vec![];
6162
for dir_entry in lint_files() {
62-
lints.append(&mut collect_from_file(&dir_entry));
63+
lints.append(&mut gather_from_file(&dir_entry).collect());
6364
}
64-
lints
65+
lints.into_iter()
6566
}
6667

67-
fn collect_from_file(dir_entry: &fs::DirEntry) -> Vec<Lint> {
68+
fn gather_from_file(dir_entry: &fs::DirEntry) -> impl Iterator<Item=Lint> {
6869
let mut file = fs::File::open(dir_entry.path()).unwrap();
6970
let mut content = String::new();
7071
file.read_to_string(&mut content).unwrap();
7172
parse_contents(&content, dir_entry.path().file_stem().unwrap().to_str().unwrap())
7273
}
7374

74-
fn parse_contents(content: &str, filename: &str) -> Vec<Lint> {
75+
fn parse_contents(content: &str, filename: &str) -> impl Iterator<Item=Lint> {
7576
let mut lints: Vec<Lint> = DEC_CLIPPY_LINT_RE
7677
.captures_iter(&content)
7778
.map(|m| Lint::new(&m["name"], &m["cat"], &m["desc"], None, filename))
@@ -81,21 +82,20 @@ fn parse_contents(content: &str, filename: &str) -> Vec<Lint> {
8182
.map(|m| Lint::new( &m["name"], "Deprecated", &m["desc"], Some(&m["desc"]), filename))
8283
.collect();
8384
lints.append(&mut deprecated);
84-
lints
85+
lints.into_iter()
8586
}
8687

8788
/// Collects all .rs files in the `clippy_lints/src` directory
88-
fn lint_files() -> Vec<fs::DirEntry> {
89+
fn lint_files() -> impl Iterator<Item=fs::DirEntry> {
8990
let paths = fs::read_dir("../clippy_lints/src").unwrap();
9091
paths
9192
.filter_map(|f| f.ok())
9293
.filter(|f| f.path().extension() == Some(OsStr::new("rs")))
93-
.collect::<Vec<fs::DirEntry>>()
9494
}
9595

9696
#[test]
9797
fn test_parse_contents() {
98-
let result = parse_contents(
98+
let result: Vec<Lint> = parse_contents(
9999
r#"
100100
declare_clippy_lint! {
101101
pub PTR_ARG,
@@ -116,7 +116,7 @@ declare_deprecated_lint! {
116116
"`assert!()` will be more flexible with RFC 2011"
117117
}
118118
"#,
119-
"module_name");
119+
"module_name").collect();
120120

121121
let expected = vec![
122122
Lint::new("ptr_arg", "style", "really long text", None, "module_name"),
@@ -141,7 +141,7 @@ fn test_active_lints() {
141141
let expected = vec![
142142
Lint::new("should_assert_eq2", "Not Deprecated", "abc", None, "module_name")
143143
];
144-
assert_eq!(expected, Lint::active_lints(&lints));
144+
assert_eq!(expected, Lint::active_lints(&lints).cloned().collect::<Vec<Lint>>());
145145
}
146146

147147
#[test]

clippy_dev/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn main() {
2727
}
2828

2929
fn print_lints() {
30-
let lint_list = collect_all();
30+
let lint_list = gather_all().collect::<Vec<Lint>>();
3131
let grouped_by_lint_group = Lint::by_lint_group(&lint_list);
3232

3333
for (lint_group, mut lints) in grouped_by_lint_group {
@@ -41,5 +41,5 @@ fn print_lints() {
4141
}
4242
}
4343

44-
println!("there are {} lints", Lint::active_lints(&lint_list).len());
44+
println!("there are {} lints", Lint::active_lints(&lint_list).count());
4545
}

0 commit comments

Comments
 (0)