From 35f100b4f9a055a98973f2a2ac2e1eb5c5cfacff Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Tue, 17 Jul 2018 22:50:17 +0200 Subject: [PATCH 01/10] update_lints rewrite: Add structure and --print-only --- Cargo.toml | 6 ++ clippy_dev/.gitignore | 38 ++++++++ clippy_dev/Cargo.lock | 215 +++++++++++++++++++++++++++++++++++++++++ clippy_dev/Cargo.toml | 9 ++ clippy_dev/src/lib.rs | 144 +++++++++++++++++++++++++++ clippy_dev/src/main.rs | 53 ++++++++++ util/dev | 3 + util/update_lints.py | 15 +-- 8 files changed, 470 insertions(+), 13 deletions(-) create mode 100644 clippy_dev/.gitignore create mode 100644 clippy_dev/Cargo.lock create mode 100644 clippy_dev/Cargo.toml create mode 100644 clippy_dev/src/lib.rs create mode 100644 clippy_dev/src/main.rs create mode 100755 util/dev diff --git a/Cargo.toml b/Cargo.toml index d368b71d993..52f93820c48 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,11 @@ name = "clippy-driver" test = false path = "src/driver.rs" +[[bin]] +name = "clippy-dev" +test = false +path = "src/main.rs" + [dependencies] # begin automatic update clippy_lints = { version = "0.0.212", path = "clippy_lints" } @@ -46,6 +51,7 @@ regex = "1" semver = "0.9" [dev-dependencies] +clippy_dev = { version = "0.0.1", path = "clippy_dev" } cargo_metadata = "0.6" compiletest_rs = "0.3.7" lazy_static = "1.0" diff --git a/clippy_dev/.gitignore b/clippy_dev/.gitignore new file mode 100644 index 00000000000..5ca1e06a5e5 --- /dev/null +++ b/clippy_dev/.gitignore @@ -0,0 +1,38 @@ +# Used by Travis to be able to push: +/.github/deploy_key +out + +# Compiled files +*.o +*.d +*.so +*.rlib +*.dll +*.pyc +*.rmeta + +# Executables +*.exe + +# Generated by Cargo +Cargo.lock +/target +/clippy_lints/target +/clippy_workspace_tests/target + +# Generated by dogfood +/target_recur/ + +# gh pages docs +util/gh-pages/lints.json + +# rustfmt backups +*.rs.bk + +helper.txt +*.iml +.vscode +.idea + +# Used by the Clippy build script +min_version.txt diff --git a/clippy_dev/Cargo.lock b/clippy_dev/Cargo.lock new file mode 100644 index 00000000000..ad5c705c292 --- /dev/null +++ b/clippy_dev/Cargo.lock @@ -0,0 +1,215 @@ +[[package]] +name = "aho-corasick" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "atty" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)", + "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bitflags" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "clap" +version = "2.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "atty 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "clippy_dev" +version = "0.0.1" +dependencies = [ + "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "lazy_static" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "libc" +version = "0.2.42" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "memchr" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "redox_syscall" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "redox_termios" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-syntax" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "strsim" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "termion" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "textwrap" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "thread_local" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ucd-util" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "unicode-width" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "unreachable" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "utf8-ranges" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "vec_map" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum aho-corasick 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c1c6d463cbe7ed28720b5b489e7c083eeb8f90d08be2a0d6bb9e1ffea9ce1afa" +"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +"checksum atty 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "2fc4a1aa4c24c0718a250f0681885c1af91419d242f29eb8f2ab28502d80dbd1" +"checksum bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d0c54bb8f454c567f21197eefcdbf5679d0bd99f2ddbe52e84c77061952e6789" +"checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e" +"checksum lazy_static 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "fb497c35d362b6a331cfd94956a07fc2c78a4604cdbee844a81170386b996dd3" +"checksum libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)" = "b685088df2b950fccadf07a7187c8ef846a959c142338a48f9dc0b94517eb5f1" +"checksum memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "796fba70e76612589ed2ce7f45282f5af869e0fdd7cc6199fa1aa1f1d591ba9d" +"checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1" +"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" +"checksum regex 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5bbbea44c5490a1e84357ff28b7d518b4619a159fed5d25f6c1de2d19cc42814" +"checksum regex-syntax 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "747ba3b235651f6e2f67dfa8bcdcd073ddb7c243cb21c442fc12395dfcac212d" +"checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" +"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" +"checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" +"checksum thread_local 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "279ef31c19ededf577bfd12dfae728040a21f635b06a24cd670ff510edd38963" +"checksum ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fd2be2d6639d0f8fe6cdda291ad456e23629558d466e2789d2c3e9892bda285d" +"checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" +"checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" +"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122" +"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" +"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" +"checksum winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "773ef9dcc5f24b7d850d0ff101e542ff24c3b090a9768e03ff889fdef41f00fd" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/clippy_dev/Cargo.toml b/clippy_dev/Cargo.toml new file mode 100644 index 00000000000..010380907af --- /dev/null +++ b/clippy_dev/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "clippy_dev" +version = "0.0.1" +authors = ["Philipp Hansch "] + +[dependencies] +clap = "~2.32" +regex = "1" +lazy_static = "1.0" diff --git a/clippy_dev/src/lib.rs b/clippy_dev/src/lib.rs new file mode 100644 index 00000000000..2a86de9cbaa --- /dev/null +++ b/clippy_dev/src/lib.rs @@ -0,0 +1,144 @@ +extern crate regex; +#[macro_use] +extern crate lazy_static; + +use regex::Regex; +use std::ffi::OsStr; +use std::fs; +use std::io::prelude::*; + +lazy_static! { + static ref DEC_CLIPPY_LINT_RE: Regex = Regex::new(r#"declare_clippy_lint!\s*[\{(]\s*pub\s+(?P[A-Z_][A-Z_0-9]*)\s*,\s*(?P[a-z_]+)\s*,\s*"(?P(?:[^"\\]+|\\(?s).(?-s))*)"\s*[})]"#).unwrap(); + static ref DEC_DEPRECATED_LINT_RE: Regex = Regex::new(r#"declare_deprecated_lint!\s*[{(]\s*pub\s+(?P[A-Z_][A-Z_0-9]*)\s*,\s*"(?P(?:[^"\\]+|\\(?s).(?-s))*)"\s*[})]"#).unwrap(); + static ref NL_ESCAPE_RE: Regex = Regex::new(r#"\\\n\s*"#).unwrap(); + pub static ref DOCS_LINK: String = "https://rust-lang-nursery.github.io/rust-clippy/master/index.html".to_string(); +} + +#[derive(Clone, PartialEq, Debug)] +pub struct Lint { + pub name: String, + pub group: String, + pub desc: String, + pub deprecation: Option, + pub module: String, +} + +impl Lint { + pub fn new(name: &str, group: &str, desc: &str, deprecation: Option<&str>, module: &str) -> Lint { + Lint { + name: name.to_lowercase(), + group: group.to_string(), + desc: NL_ESCAPE_RE.replace(&desc.replace("\\\"", "\""), "").to_string(), + deprecation: deprecation.map(|d| d.to_string()), + module: module.to_string(), + } + } + + pub fn active_lints(lints: &[Lint]) -> Vec { + lints.iter().filter(|l| l.deprecation.is_none()).cloned().collect::>() + } + + pub fn in_lint_group(group: &str, lints: &[Lint]) -> Vec { + lints.iter().filter(|l| l.group == group).cloned().collect::>() + } +} + +pub fn collect_all() -> Vec { + let mut lints = vec![]; + for direntry in lint_files() { + lints.append(&mut collect_from_file(&direntry)); + } + lints +} + +fn collect_from_file(direntry: &fs::DirEntry) -> Vec { + let mut file = fs::File::open(direntry.path()).unwrap(); + let mut content = String::new(); + file.read_to_string(&mut content).unwrap(); + parse_contents(&content, direntry.path().file_stem().unwrap().to_str().unwrap()) +} + +fn parse_contents(content: &str, filename: &str) -> Vec { + let mut lints: Vec = DEC_CLIPPY_LINT_RE + .captures_iter(&content) + .map(|m| Lint::new(&m["name"], &m["cat"], &m["desc"], None, filename)) + .collect(); + let mut deprecated = DEC_DEPRECATED_LINT_RE + .captures_iter(&content) + .map(|m| Lint::new( &m["name"], "Deprecated", &m["desc"], Some(&m["desc"]), filename)) + .collect(); + lints.append(&mut deprecated); + lints +} + +/// Collects all .rs files in the `clippy_lints/src` directory +fn lint_files() -> Vec { + 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( + r#" +declare_clippy_lint! { + pub PTR_ARG, + style, + "really long \ + text" +} + +declare_clippy_lint!{ + pub DOC_MARKDOWN, + pedantic, + "single line" +} + +/// some doc comment +declare_deprecated_lint! { + pub SHOULD_ASSERT_EQ, + "`assert!()` will be more flexible with RFC 2011" +} + "#, + "module_name"); + + let expected = vec![ + Lint::new("ptr_arg", "style", "really long text", None, "module_name"), + Lint::new("doc_markdown", "pedantic", "single line", None, "module_name"), + Lint::new( + "should_assert_eq", + "Deprecated", + "`assert!()` will be more flexible with RFC 2011", + Some("`assert!()` will be more flexible with RFC 2011"), + "module_name" + ), + ]; + assert_eq!(expected, result); +} + +#[test] +fn test_active_lints() { + let lints = vec![ + Lint::new("should_assert_eq", "Deprecated", "abc", Some("Reason"), "module_name"), + Lint::new("should_assert_eq2", "Not Deprecated", "abc", None, "module_name") + ]; + let expected = vec![ + Lint::new("should_assert_eq2", "Not Deprecated", "abc", None, "module_name") + ]; + assert_eq!(expected, Lint::active_lints(&lints)); +} + +#[test] +fn test_in_lint_group() { + let lints = vec![ + Lint::new("ptr_arg", "style", "really long text", None, "module_name"), + Lint::new("doc_markdown", "pedantic", "single line", None, "module_name"), + ]; + let expected = vec![ + Lint::new("ptr_arg", "style", "really long text", None, "module_name") + ]; + assert_eq!(expected, Lint::in_lint_group("style", &lints)); +} diff --git a/clippy_dev/src/main.rs b/clippy_dev/src/main.rs new file mode 100644 index 00000000000..7bf6eb01245 --- /dev/null +++ b/clippy_dev/src/main.rs @@ -0,0 +1,53 @@ +extern crate clap; +extern crate clippy_dev; +extern crate regex; + +use clap::{App, Arg, SubCommand}; +use clippy_dev::*; + +fn main() { + let matches = App::new("Clippy developer tooling") + .subcommand( + SubCommand::with_name("update_lints") + .about("Update the lint list") + .arg( + Arg::with_name("print-only") + .long("print-only") + .short("p") + .help("Print a table of lints to STDOUT. Does not modify any files."), + ) + ) + .get_matches(); + + if let Some(matches) = matches.subcommand_matches("update_lints") { + if matches.is_present("print-only") { + print_lints(); + } + } +} + +fn print_lints() { + let lint_list = collect_all(); + let print_clippy_lint_groups: [&str; 7] = [ + "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); + group_lints.sort_by(|a, b| a.name.cmp(&b.name)); + + for lint in group_lints { + if lint.deprecation.is_some() { continue; } + println!("* [{}]({}#{}) ({})", lint.name, clippy_dev::DOCS_LINK.clone(), lint.name, lint.desc); + } + } + println!("there are {} lints", Lint::active_lints(&lint_list).len()); +} diff --git a/util/dev b/util/dev new file mode 100755 index 00000000000..4fa6e69b752 --- /dev/null +++ b/util/dev @@ -0,0 +1,3 @@ +#!/bin/sh + +cd clippy_dev && cargo run -- $@ diff --git a/util/update_lints.py b/util/update_lints.py index ea7b992abb7..15242abd606 100755 --- a/util/update_lints.py +++ b/util/update_lints.py @@ -7,6 +7,7 @@ import os import re import sys +from subprocess import call declare_deprecated_lint_re = re.compile(r''' declare_deprecated_lint! \s* [{(] \s* @@ -166,19 +167,7 @@ def main(print_only=False, check=False): all_lints += value if print_only: - print_clippy_lint_groups = [ - "correctness", - "style", - "complexity", - "perf", - "pedantic", - "nursery", - "restriction" - ] - for group in print_clippy_lint_groups: - sys.stdout.write('\n## ' + group + '\n') - for (_, name, _, descr) in sorted(clippy_lints[group]): - sys.stdout.write('* [' + name + '](https://rust-lang-nursery.github.io/rust-clippy/master/index.html#' + name + ') (' + descr + ')\n') + call(["./util/dev", "update_lints", "--print-only"]) return # update the lint counter in README.md From 502357df65eb026721b9c3bc5a71ea749bd0669e Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Thu, 30 Aug 2018 07:57:11 +0200 Subject: [PATCH 02/10] cargo update in clippy_dev --- clippy_dev/Cargo.lock | 81 ++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 44 deletions(-) diff --git a/clippy_dev/Cargo.lock b/clippy_dev/Cargo.lock index ad5c705c292..8731a7c8668 100644 --- a/clippy_dev/Cargo.lock +++ b/clippy_dev/Cargo.lock @@ -1,9 +1,9 @@ [[package]] name = "aho-corasick" -version = "0.6.6" +version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -16,17 +16,17 @@ dependencies = [ [[package]] name = "atty" -version = "0.2.10" +version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "bitflags" -version = "1.0.3" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -35,8 +35,8 @@ version = "2.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", - "atty 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", - "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -48,26 +48,29 @@ name = "clippy_dev" version = "0.0.1" dependencies = [ "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", - "regex 1.0.2 (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)", ] [[package]] name = "lazy_static" -version = "1.0.2" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "version_check 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "libc" -version = "0.2.42" +version = "0.2.43" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "memchr" -version = "2.0.1" +version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -85,14 +88,14 @@ dependencies = [ [[package]] name = "regex" -version = "1.0.2" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "aho-corasick 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)", - "memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "aho-corasick 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "regex-syntax 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", - "thread_local 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", - "utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -113,7 +116,7 @@ name = "termion" version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", "redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -128,11 +131,10 @@ dependencies = [ [[package]] name = "thread_local" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "lazy_static 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", - "unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -145,17 +147,9 @@ name = "unicode-width" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "unreachable" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "utf8-ranges" -version = "1.0.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -164,8 +158,8 @@ version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] -name = "void" -version = "1.0.2" +name = "version_check" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -188,28 +182,27 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] -"checksum aho-corasick 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c1c6d463cbe7ed28720b5b489e7c083eeb8f90d08be2a0d6bb9e1ffea9ce1afa" +"checksum aho-corasick 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "68f56c7353e5a9547cbd76ed90f7bb5ffc3ba09d4ea9bd1d8c06c8b1142eeb5a" "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" -"checksum atty 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "2fc4a1aa4c24c0718a250f0681885c1af91419d242f29eb8f2ab28502d80dbd1" -"checksum bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d0c54bb8f454c567f21197eefcdbf5679d0bd99f2ddbe52e84c77061952e6789" +"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 clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e" -"checksum lazy_static 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "fb497c35d362b6a331cfd94956a07fc2c78a4604cdbee844a81170386b996dd3" -"checksum libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)" = "b685088df2b950fccadf07a7187c8ef846a959c142338a48f9dc0b94517eb5f1" -"checksum memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "796fba70e76612589ed2ce7f45282f5af869e0fdd7cc6199fa1aa1f1d591ba9d" +"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 memchr 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a3b4142ab8738a78c51896f704f83c11df047ff1bda9a92a661aa6361552d93d" "checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1" "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" -"checksum regex 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5bbbea44c5490a1e84357ff28b7d518b4619a159fed5d25f6c1de2d19cc42814" +"checksum regex 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "67d0301b0c6804eca7e3c275119d0b01ff3b7ab9258a65709e608a66312a1025" "checksum regex-syntax 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "747ba3b235651f6e2f67dfa8bcdcd073ddb7c243cb21c442fc12395dfcac212d" "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" -"checksum thread_local 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "279ef31c19ededf577bfd12dfae728040a21f635b06a24cd670ff510edd38963" +"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fd2be2d6639d0f8fe6cdda291ad456e23629558d466e2789d2c3e9892bda285d" "checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" -"checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" -"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122" +"checksum utf8-ranges 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fd70f467df6810094968e2fce0ee1bd0e87157aceb026a8c083bcf5e25b9efe4" "checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" -"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" +"checksum version_check 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7716c242968ee87e5542f8021178248f267f295a5c4803beae8b8b7fd9bc6051" "checksum winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "773ef9dcc5f24b7d850d0ff101e542ff24c3b090a9768e03ff889fdef41f00fd" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" From 70312430dd8c32c0f1adc9e6f24c18bd2dd7d9c7 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Thu, 30 Aug 2018 07:57:54 +0200 Subject: [PATCH 03/10] Use insignificant whitespace mode for nice regex --- clippy_dev/src/lib.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/clippy_dev/src/lib.rs b/clippy_dev/src/lib.rs index 2a86de9cbaa..1431a0fc05d 100644 --- a/clippy_dev/src/lib.rs +++ b/clippy_dev/src/lib.rs @@ -8,8 +8,17 @@ use std::fs; use std::io::prelude::*; lazy_static! { - static ref DEC_CLIPPY_LINT_RE: Regex = Regex::new(r#"declare_clippy_lint!\s*[\{(]\s*pub\s+(?P[A-Z_][A-Z_0-9]*)\s*,\s*(?P[a-z_]+)\s*,\s*"(?P(?:[^"\\]+|\\(?s).(?-s))*)"\s*[})]"#).unwrap(); - static ref DEC_DEPRECATED_LINT_RE: Regex = Regex::new(r#"declare_deprecated_lint!\s*[{(]\s*pub\s+(?P[A-Z_][A-Z_0-9]*)\s*,\s*"(?P(?:[^"\\]+|\\(?s).(?-s))*)"\s*[})]"#).unwrap(); + static ref DEC_CLIPPY_LINT_RE: Regex = Regex::new(r#"(?x) + declare_clippy_lint!\s*[\{(]\s* + pub\s+(?P[A-Z_][A-Z_0-9]*)\s*,\s* + (?P[a-z_]+)\s*,\s* + "(?P(?:[^"\\]+|\\(?s).(?-s))*)"\s*[})] + "#).unwrap(); + static ref DEC_DEPRECATED_LINT_RE: Regex = Regex::new(r#"(?x) + declare_deprecated_lint!\s*[{(]\s* + pub\s+(?P[A-Z_][A-Z_0-9]*)\s*,\s* + "(?P(?:[^"\\]+|\\(?s).(?-s))*)"\s*[})] + "#).unwrap(); static ref NL_ESCAPE_RE: Regex = Regex::new(r#"\\\n\s*"#).unwrap(); pub static ref DOCS_LINK: String = "https://rust-lang-nursery.github.io/rust-clippy/master/index.html".to_string(); } From 78d358b861442137182ea7871d1177c799fab7e8 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Thu, 30 Aug 2018 08:00:16 +0200 Subject: [PATCH 04/10] s/direntry/dir_entry --- clippy_dev/src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/clippy_dev/src/lib.rs b/clippy_dev/src/lib.rs index 1431a0fc05d..e1de1bec57f 100644 --- a/clippy_dev/src/lib.rs +++ b/clippy_dev/src/lib.rs @@ -54,17 +54,17 @@ impl Lint { pub fn collect_all() -> Vec { let mut lints = vec![]; - for direntry in lint_files() { - lints.append(&mut collect_from_file(&direntry)); + for dir_entry in lint_files() { + lints.append(&mut collect_from_file(&dir_entry)); } lints } -fn collect_from_file(direntry: &fs::DirEntry) -> Vec { - let mut file = fs::File::open(direntry.path()).unwrap(); +fn collect_from_file(dir_entry: &fs::DirEntry) -> Vec { + 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, direntry.path().file_stem().unwrap().to_str().unwrap()) + parse_contents(&content, dir_entry.path().file_stem().unwrap().to_str().unwrap()) } fn parse_contents(content: &str, filename: &str) -> Vec { From 586ef4ed7237b2290bb6365b038e8da59776533f Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Sun, 2 Sep 2018 09:45:13 +0200 Subject: [PATCH 05/10] Refactor to use into_group_map from Itertools --- clippy_dev/Cargo.lock | 16 ++++++++++++++++ clippy_dev/Cargo.toml | 1 + clippy_dev/src/lib.rs | 28 +++++++++++++++++++--------- clippy_dev/src/main.rs | 24 ++++++++---------------- 4 files changed, 44 insertions(+), 25 deletions(-) diff --git a/clippy_dev/Cargo.lock b/clippy_dev/Cargo.lock index 8731a7c8668..2d94755bae3 100644 --- a/clippy_dev/Cargo.lock +++ b/clippy_dev/Cargo.lock @@ -48,10 +48,24 @@ name = "clippy_dev" version = "0.0.1" dependencies = [ "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)", "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]] name = "lazy_static" 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 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 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 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" diff --git a/clippy_dev/Cargo.toml b/clippy_dev/Cargo.toml index 010380907af..503dd53aa20 100644 --- a/clippy_dev/Cargo.toml +++ b/clippy_dev/Cargo.toml @@ -5,5 +5,6 @@ authors = ["Philipp Hansch "] [dependencies] clap = "~2.32" +itertools = "0.7" regex = "1" lazy_static = "1.0" diff --git a/clippy_dev/src/lib.rs b/clippy_dev/src/lib.rs index e1de1bec57f..8eabf3a97c0 100644 --- a/clippy_dev/src/lib.rs +++ b/clippy_dev/src/lib.rs @@ -1,8 +1,11 @@ extern crate regex; #[macro_use] extern crate lazy_static; +extern crate itertools; use regex::Regex; +use itertools::Itertools; +use std::collections::HashMap; use std::ffi::OsStr; use std::fs; use std::io::prelude::*; @@ -47,8 +50,9 @@ impl Lint { lints.iter().filter(|l| l.deprecation.is_none()).cloned().collect::>() } - pub fn in_lint_group(group: &str, lints: &[Lint]) -> Vec { - lints.iter().filter(|l| l.group == group).cloned().collect::>() + /// Returns the lints in a HashMap, grouped by the different lint groups + pub fn by_lint_group(lints: &[Lint]) -> HashMap> { + lints.iter().map(|lint| (lint.group.to_string(), lint.clone())).into_group_map() } } @@ -141,13 +145,19 @@ fn test_active_lints() { } #[test] -fn test_in_lint_group() { +fn test_by_lint_group() { let lints = vec![ - Lint::new("ptr_arg", "style", "really long text", None, "module_name"), - Lint::new("doc_markdown", "pedantic", "single line", None, "module_name"), + Lint::new("should_assert_eq", "group1", "abc", 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![ - Lint::new("ptr_arg", "style", "really long text", None, "module_name") - ]; - assert_eq!(expected, Lint::in_lint_group("style", &lints)); + let mut expected: HashMap> = HashMap::new(); + expected.insert("group1".to_string(), vec![ + Lint::new("should_assert_eq", "group1", "abc", None, "module_name"), + 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)); } diff --git a/clippy_dev/src/main.rs b/clippy_dev/src/main.rs index 7bf6eb01245..6b55a7b7bf4 100644 --- a/clippy_dev/src/main.rs +++ b/clippy_dev/src/main.rs @@ -28,26 +28,18 @@ fn main() { fn print_lints() { let lint_list = collect_all(); - let print_clippy_lint_groups: [&str; 7] = [ - "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 grouped_by_lint_group = Lint::by_lint_group(&lint_list); - let mut group_lints = Lint::in_lint_group(group, &lint_list); - group_lints.sort_by(|a, b| a.name.cmp(&b.name)); + for (lint_group, mut lints) in grouped_by_lint_group { + if lint_group == "Deprecated" { continue; } + println!("\n## {}", lint_group); - for lint in group_lints { - if lint.deprecation.is_some() { continue; } + lints.sort_by(|a, b| a.name.cmp(&b.name)); + + for lint in lints { println!("* [{}]({}#{}) ({})", lint.name, clippy_dev::DOCS_LINK.clone(), lint.name, lint.desc); } } + println!("there are {} lints", Lint::active_lints(&lint_list).len()); } From be995dc0e841cb2db46d7995958ba24bac91e1e2 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Sun, 2 Sep 2018 10:13:53 +0200 Subject: [PATCH 06/10] Run clippy on clippy_dev, too --- ci/base-tests.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/ci/base-tests.sh b/ci/base-tests.sh index dee709ab4eb..b85ed4fab66 100755 --- a/ci/base-tests.sh +++ b/ci/base-tests.sh @@ -19,6 +19,7 @@ cd clippy_workspace_tests && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clip cd clippy_workspace_tests/src && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy::all && cd ../.. cd clippy_workspace_tests/subcrate && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy::all && cd ../.. cd clippy_workspace_tests/subcrate/src && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy::all && cd ../../.. +cd clippy_dev && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy::all && cd .. # test --manifest-path PATH=$PATH:~/rust/cargo/bin cargo clippy --manifest-path=clippy_workspace_tests/Cargo.toml -- -D clippy::all cd clippy_workspace_tests/subcrate && PATH=$PATH:~/rust/cargo/bin cargo clippy --manifest-path=../Cargo.toml -- -D clippy::all && cd ../.. From 79bec036f8cd928e74e3c26d79daf94b4a30dea1 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Wed, 5 Sep 2018 13:34:28 +0200 Subject: [PATCH 07/10] Return impl Iterator instead of Vec This makes the API of `lib.rs` a bit more flexible. --- clippy_dev/src/lib.rs | 26 +++++++++++++------------- clippy_dev/src/main.rs | 4 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/clippy_dev/src/lib.rs b/clippy_dev/src/lib.rs index 8eabf3a97c0..c499934346e 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 6b55a7b7bf4..f45c52e2271 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()); } From 20836d3003050d4abd313eb9fda4dc8bb6bc7f9c Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Wed, 5 Sep 2018 13:35:57 +0200 Subject: [PATCH 08/10] Remove duplicated .gitignore --- .gitignore | 4 +--- clippy_dev/.gitignore | 38 -------------------------------------- 2 files changed, 1 insertion(+), 41 deletions(-) delete mode 100644 clippy_dev/.gitignore diff --git a/.gitignore b/.gitignore index 5ca1e06a5e5..5fd7f2fc1a0 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ Cargo.lock /target /clippy_lints/target /clippy_workspace_tests/target +/clippy_dev/target # Generated by dogfood /target_recur/ @@ -33,6 +34,3 @@ helper.txt *.iml .vscode .idea - -# Used by the Clippy build script -min_version.txt diff --git a/clippy_dev/.gitignore b/clippy_dev/.gitignore deleted file mode 100644 index 5ca1e06a5e5..00000000000 --- a/clippy_dev/.gitignore +++ /dev/null @@ -1,38 +0,0 @@ -# Used by Travis to be able to push: -/.github/deploy_key -out - -# Compiled files -*.o -*.d -*.so -*.rlib -*.dll -*.pyc -*.rmeta - -# Executables -*.exe - -# Generated by Cargo -Cargo.lock -/target -/clippy_lints/target -/clippy_workspace_tests/target - -# Generated by dogfood -/target_recur/ - -# gh pages docs -util/gh-pages/lints.json - -# rustfmt backups -*.rs.bk - -helper.txt -*.iml -.vscode -.idea - -# Used by the Clippy build script -min_version.txt From de36d42e80864f40fb3075dc4e3389cfa0c113b2 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Wed, 5 Sep 2018 20:32:26 +0200 Subject: [PATCH 09/10] More refactoring --- clippy_dev/src/lib.rs | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/clippy_dev/src/lib.rs b/clippy_dev/src/lib.rs index c499934346e..998efb142cc 100644 --- a/clippy_dev/src/lib.rs +++ b/clippy_dev/src/lib.rs @@ -58,14 +58,10 @@ impl Lint { } pub fn gather_all() -> impl Iterator { - let mut lints = vec![]; - for dir_entry in lint_files() { - lints.append(&mut gather_from_file(&dir_entry).collect()); - } - lints.into_iter() + lint_files().flat_map(gather_from_file) } -fn gather_from_file(dir_entry: &fs::DirEntry) -> impl Iterator { +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(); @@ -73,22 +69,20 @@ fn gather_from_file(dir_entry: &fs::DirEntry) -> impl Iterator { } 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)) - .collect(); - let mut deprecated = DEC_DEPRECATED_LINT_RE - .captures_iter(&content) - .map(|m| Lint::new( &m["name"], "Deprecated", &m["desc"], Some(&m["desc"]), filename)) - .collect(); - lints.append(&mut deprecated); - lints.into_iter() + let lints = DEC_CLIPPY_LINT_RE + .captures_iter(content) + .map(|m| Lint::new(&m["name"], &m["cat"], &m["desc"], None, filename)); + let deprecated = DEC_DEPRECATED_LINT_RE + .captures_iter(content) + .map(|m| Lint::new( &m["name"], "Deprecated", &m["desc"], Some(&m["desc"]), filename)); + // Removing the `.collect::>().into_iter()` causes some lifetime issues due to the map + lints.chain(deprecated).collect::>().into_iter() } /// Collects all .rs files in the `clippy_lints/src` directory fn lint_files() -> impl Iterator { - let paths = fs::read_dir("../clippy_lints/src").unwrap(); - paths + fs::read_dir("../clippy_lints/src") + .unwrap() .filter_map(|f| f.ok()) .filter(|f| f.path().extension() == Some(OsStr::new("rs"))) } From 3bdc691a91e3a7ac05f808772c0fbc2370fff511 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Thu, 6 Sep 2018 08:19:09 +0200 Subject: [PATCH 10/10] Pass by ref instead of value --- clippy_dev/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clippy_dev/src/lib.rs b/clippy_dev/src/lib.rs index 998efb142cc..a872ecf3feb 100644 --- a/clippy_dev/src/lib.rs +++ b/clippy_dev/src/lib.rs @@ -58,10 +58,10 @@ impl Lint { } pub fn gather_all() -> impl Iterator { - lint_files().flat_map(gather_from_file) + lint_files().flat_map(|f| gather_from_file(&f)) } -fn gather_from_file(dir_entry: fs::DirEntry) -> impl Iterator { +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();