Refactor to use into_group_map from Itertools

This commit is contained in:
Philipp Hansch 2018-09-02 09:45:13 +02:00
parent 78d358b861
commit 586ef4ed72
No known key found for this signature in database
GPG key ID: B6FA06A6E0E2665B
4 changed files with 44 additions and 25 deletions

16
clippy_dev/Cargo.lock generated
View file

@ -48,10 +48,24 @@ name = "clippy_dev"
version = "0.0.1" version = "0.0.1"
dependencies = [ dependencies = [
"clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
"itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "either"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "itertools"
version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.1.0" version = "1.1.0"
@ -187,6 +201,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652" "checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652"
"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12"
"checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e" "checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e"
"checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0"
"checksum itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)" = "f58856976b776fedd95533137617a02fb25719f40e7d9b01c7043cd65474f450"
"checksum lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca488b89a5657b0a2ecd45b95609b3e848cf1755da332a0da46e2b2b1cb371a7" "checksum lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca488b89a5657b0a2ecd45b95609b3e848cf1755da332a0da46e2b2b1cb371a7"
"checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d" "checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d"
"checksum memchr 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a3b4142ab8738a78c51896f704f83c11df047ff1bda9a92a661aa6361552d93d" "checksum memchr 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a3b4142ab8738a78c51896f704f83c11df047ff1bda9a92a661aa6361552d93d"

View file

@ -5,5 +5,6 @@ authors = ["Philipp Hansch <dev@phansch.net>"]
[dependencies] [dependencies]
clap = "~2.32" clap = "~2.32"
itertools = "0.7"
regex = "1" regex = "1"
lazy_static = "1.0" lazy_static = "1.0"

View file

@ -1,8 +1,11 @@
extern crate regex; extern crate regex;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
extern crate itertools;
use regex::Regex; use regex::Regex;
use itertools::Itertools;
use std::collections::HashMap;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::fs; use std::fs;
use std::io::prelude::*; use std::io::prelude::*;
@ -47,8 +50,9 @@ impl Lint {
lints.iter().filter(|l| l.deprecation.is_none()).cloned().collect::<Vec<Lint>>() lints.iter().filter(|l| l.deprecation.is_none()).cloned().collect::<Vec<Lint>>()
} }
pub fn in_lint_group(group: &str, lints: &[Lint]) -> Vec<Lint> { /// Returns the lints in a HashMap, grouped by the different lint groups
lints.iter().filter(|l| l.group == group).cloned().collect::<Vec<Lint>>() pub fn by_lint_group(lints: &[Lint]) -> HashMap<String, Vec<Lint>> {
lints.iter().map(|lint| (lint.group.to_string(), lint.clone())).into_group_map()
} }
} }
@ -141,13 +145,19 @@ fn test_active_lints() {
} }
#[test] #[test]
fn test_in_lint_group() { fn test_by_lint_group() {
let lints = vec![ let lints = vec![
Lint::new("ptr_arg", "style", "really long text", None, "module_name"), Lint::new("should_assert_eq", "group1", "abc", None, "module_name"),
Lint::new("doc_markdown", "pedantic", "single line", None, "module_name"), Lint::new("should_assert_eq2", "group2", "abc", None, "module_name"),
Lint::new("incorrect_match", "group1", "abc", None, "module_name"),
]; ];
let expected = vec![ let mut expected: HashMap<String, Vec<Lint>> = HashMap::new();
Lint::new("ptr_arg", "style", "really long text", None, "module_name") expected.insert("group1".to_string(), vec![
]; Lint::new("should_assert_eq", "group1", "abc", None, "module_name"),
assert_eq!(expected, Lint::in_lint_group("style", &lints)); Lint::new("incorrect_match", "group1", "abc", None, "module_name"),
]);
expected.insert("group2".to_string(), vec![
Lint::new("should_assert_eq2", "group2", "abc", None, "module_name")
]);
assert_eq!(expected, Lint::by_lint_group(&lints));
} }

View file

@ -28,26 +28,18 @@ fn main() {
fn print_lints() { fn print_lints() {
let lint_list = collect_all(); let lint_list = collect_all();
let print_clippy_lint_groups: [&str; 7] = [ let grouped_by_lint_group = Lint::by_lint_group(&lint_list);
"correctness",
"style",
"complexity",
"perf",
"pedantic",
"nursery",
"restriction"
];
// We could use itertools' group_by to make this much more concise:
for group in &print_clippy_lint_groups {
println!("\n## {}", group);
let mut group_lints = Lint::in_lint_group(group, &lint_list); for (lint_group, mut lints) in grouped_by_lint_group {
group_lints.sort_by(|a, b| a.name.cmp(&b.name)); if lint_group == "Deprecated" { continue; }
println!("\n## {}", lint_group);
for lint in group_lints { lints.sort_by(|a, b| a.name.cmp(&b.name));
if lint.deprecation.is_some() { continue; }
for lint in lints {
println!("* [{}]({}#{}) ({})", lint.name, clippy_dev::DOCS_LINK.clone(), lint.name, lint.desc); println!("* [{}]({}#{}) ({})", lint.name, clippy_dev::DOCS_LINK.clone(), lint.name, lint.desc);
} }
} }
println!("there are {} lints", Lint::active_lints(&lint_list).len()); println!("there are {} lints", Lint::active_lints(&lint_list).len());
} }