Skip to content

Commit cf89c58

Browse files
committed
Refactor to use into_group_map from Itertools
1 parent a98e2f8 commit cf89c58

File tree

4 files changed

+46
-27
lines changed

4 files changed

+46
-27
lines changed

clippy_dev/Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clippy_dev/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ authors = ["Philipp Hansch <[email protected]>"]
55

66
[dependencies]
77
clap = "~2.32"
8+
itertools = "0.7"
89
regex = "1"
910
lazy_static = "1.0"

clippy_dev/src/lib.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
extern crate regex;
22
#[macro_use]
33
extern crate lazy_static;
4+
extern crate itertools;
45

56
use regex::Regex;
7+
use itertools::Itertools;
8+
use std::collections::HashMap;
69
use std::ffi::OsStr;
710
use std::fs;
811
use std::io::prelude::*;
@@ -47,8 +50,9 @@ impl Lint {
4750
lints.iter().filter(|l| l.deprecation.is_none()).cloned().collect::<Vec<Lint>>()
4851
}
4952

50-
pub fn in_lint_group(group: &str, lints: &[Lint]) -> Vec<Lint> {
51-
lints.iter().filter(|l| l.group == group).cloned().collect::<Vec<Lint>>()
53+
/// Returns the lints in a HashMap, grouped by the different lint groups
54+
pub fn by_lint_group(lints: &[Lint]) -> HashMap<String, Vec<Lint>> {
55+
lints.iter().map(|lint| (lint.group.to_string(), lint.clone())).into_group_map()
5256
}
5357
}
5458

@@ -141,13 +145,19 @@ fn test_active_lints() {
141145
}
142146

143147
#[test]
144-
fn test_in_lint_group() {
148+
fn test_by_lint_group() {
145149
let lints = vec![
146-
Lint::new("ptr_arg", "style", "really long text", None, "module_name"),
147-
Lint::new("doc_markdown", "pedantic", "single line", None, "module_name"),
148-
];
149-
let expected = vec![
150-
Lint::new("ptr_arg", "style", "really long text", None, "module_name")
150+
Lint::new("should_assert_eq", "group1", "abc", None, "module_name"),
151+
Lint::new("should_assert_eq2", "group2", "abc", None, "module_name"),
152+
Lint::new("incorrect_match", "group1", "abc", None, "module_name"),
151153
];
152-
assert_eq!(expected, Lint::in_lint_group("style", &lints));
154+
let mut expected: HashMap<String, Vec<Lint>> = HashMap::new();
155+
expected.insert("group1".to_string(), vec![
156+
Lint::new("should_assert_eq", "group1", "abc", None, "module_name"),
157+
Lint::new("incorrect_match", "group1", "abc", None, "module_name"),
158+
]);
159+
expected.insert("group2".to_string(), vec![
160+
Lint::new("should_assert_eq2", "group2", "abc", None, "module_name")
161+
]);
162+
assert_eq!(expected, Lint::by_lint_group(&lints));
153163
}

clippy_dev/src/main.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,18 @@ fn main() {
2828

2929
fn print_lints() {
3030
let lint_list = collect_all();
31-
let print_clippy_lint_groups: [&str; 7] = [
32-
"correctness",
33-
"style",
34-
"complexity",
35-
"perf",
36-
"pedantic",
37-
"nursery",
38-
"restriction"
39-
];
40-
// We could use itertools' group_by to make this much more concise:
41-
for group in &print_clippy_lint_groups {
42-
println!("\n## {}", group);
43-
44-
let mut group_lints = Lint::in_lint_group(group, &lint_list);
45-
group_lints.sort_by(|a, b| a.name.cmp(&b.name));
46-
47-
for lint in group_lints {
48-
if lint.deprecation.is_some() { continue; }
31+
let grouped_by_lint_group = Lint::by_lint_group(&lint_list);
32+
33+
for (lint_group, mut lints) in grouped_by_lint_group {
34+
if lint_group == "Deprecated" { continue; }
35+
println!("\n## {}", lint_group);
36+
37+
lints.sort_by(|a, b| a.name.cmp(&b.name));
38+
39+
for lint in lints {
4940
println!("* [{}]({}#{}) ({})", lint.name, clippy_dev::DOCS_LINK.clone(), lint.name, lint.desc);
5041
}
5142
}
43+
5244
println!("there are {} lints", Lint::active_lints(&lint_list).len());
5345
}

0 commit comments

Comments
 (0)