From 8b9d70d3490698dac74f67a9caad1c5ee825e716 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Tue, 31 Mar 2020 15:09:11 +0200 Subject: [PATCH 1/5] Define modules in lib.rs instead of main.rs --- clippy_dev/src/fmt.rs | 2 +- clippy_dev/src/lib.rs | 5 +++++ clippy_dev/src/main.rs | 4 +--- clippy_dev/src/new_lint.rs | 7 ++++++- clippy_dev/src/stderr_length_check.rs | 4 +--- 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/clippy_dev/src/fmt.rs b/clippy_dev/src/fmt.rs index a6043c4be0d..6ae3f58c1f2 100644 --- a/clippy_dev/src/fmt.rs +++ b/clippy_dev/src/fmt.rs @@ -1,4 +1,4 @@ -use clippy_dev::clippy_project_root; +use crate::clippy_project_root; use shell_escape::escape; use std::ffi::OsStr; use std::io; diff --git a/clippy_dev/src/lib.rs b/clippy_dev/src/lib.rs index 6fe7bb155ac..83f60f15906 100644 --- a/clippy_dev/src/lib.rs +++ b/clippy_dev/src/lib.rs @@ -9,6 +9,11 @@ use std::fs; use std::path::{Path, PathBuf}; use walkdir::WalkDir; +pub mod fmt; +pub mod new_lint; +pub mod stderr_length_check; +pub mod update_lints; + lazy_static! { static ref DEC_CLIPPY_LINT_RE: Regex = Regex::new( r#"(?x) diff --git a/clippy_dev/src/main.rs b/clippy_dev/src/main.rs index 901e663ded3..222658a628b 100644 --- a/clippy_dev/src/main.rs +++ b/clippy_dev/src/main.rs @@ -7,9 +7,7 @@ use clippy_dev::{ }; use std::path::Path; -mod fmt; -mod new_lint; -mod stderr_length_check; +use clippy_dev::{fmt, new_lint, stderr_length_check}; #[derive(Clone, Copy, PartialEq)] enum UpdateMode { diff --git a/clippy_dev/src/new_lint.rs b/clippy_dev/src/new_lint.rs index 9e2a4617cde..44b2a5383d2 100644 --- a/clippy_dev/src/new_lint.rs +++ b/clippy_dev/src/new_lint.rs @@ -1,10 +1,15 @@ -use clippy_dev::clippy_project_root; +use crate::clippy_project_root; use std::fs::{File, OpenOptions}; use std::io; use std::io::prelude::*; use std::io::ErrorKind; use std::path::Path; +/// Creates files required to implement and test a new lint and runs `update_lints`. +/// +/// # Errors +/// +/// This function errors, if the files couldn't be created pub fn create(pass: Option<&str>, lint_name: Option<&str>, category: Option<&str>) -> Result<(), io::Error> { let pass = pass.expect("`pass` argument is validated by clap"); let lint_name = lint_name.expect("`name` argument is validated by clap"); diff --git a/clippy_dev/src/stderr_length_check.rs b/clippy_dev/src/stderr_length_check.rs index c511733f7bf..e02b6f7da5f 100644 --- a/clippy_dev/src/stderr_length_check.rs +++ b/clippy_dev/src/stderr_length_check.rs @@ -1,11 +1,9 @@ +use crate::clippy_project_root; use std::ffi::OsStr; use std::fs; use std::path::{Path, PathBuf}; - use walkdir::WalkDir; -use clippy_dev::clippy_project_root; - // The maximum length allowed for stderr files. // // We limit this because small files are easier to deal with than bigger files. From deb1979b8e36dfc8c651638816ae061144fe58af Mon Sep 17 00:00:00 2001 From: flip1995 Date: Tue, 31 Mar 2020 15:13:38 +0200 Subject: [PATCH 2/5] Make limit_stderr_length a subcommand --- .github/workflows/clippy_dev.yml | 4 ++-- clippy_dev/src/main.rs | 14 ++++++-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/.github/workflows/clippy_dev.yml b/.github/workflows/clippy_dev.yml index 9ca2e630cbb..ffb88cdb4d9 100644 --- a/.github/workflows/clippy_dev.yml +++ b/.github/workflows/clippy_dev.yml @@ -38,8 +38,8 @@ jobs: run: cargo build --features deny-warnings working-directory: clippy_dev - - name: Test limit-stderr-length - run: cargo dev --limit-stderr-length + - name: Test limit_stderr_length + run: cargo dev limit_stderr_length - name: Test update_lints run: cargo dev update_lints --check diff --git a/clippy_dev/src/main.rs b/clippy_dev/src/main.rs index 222658a628b..dadb2d7c04c 100644 --- a/clippy_dev/src/main.rs +++ b/clippy_dev/src/main.rs @@ -95,17 +95,12 @@ fn main() { .takes_value(true), ), ) - .arg( - Arg::with_name("limit-stderr-length") - .long("limit-stderr-length") - .help("Ensures that stderr files do not grow longer than a certain amount of lines."), + .subcommand( + SubCommand::with_name("limit_stderr_length") + .about("Ensures that stderr files do not grow longer than a certain amount of lines."), ) .get_matches(); - if matches.is_present("limit-stderr-length") { - stderr_length_check::check(); - } - match matches.subcommand() { ("fmt", Some(matches)) => { fmt::run(matches.is_present("check"), matches.is_present("verbose")); @@ -129,6 +124,9 @@ fn main() { Err(e) => eprintln!("Unable to create lint: {}", e), } }, + ("limit_stderr_length", _) => { + stderr_length_check::check(); + }, _ => {}, } } From 5de019074b08d4625ab9e24280071a270e9b8eef Mon Sep 17 00:00:00 2001 From: flip1995 Date: Tue, 31 Mar 2020 15:14:29 +0200 Subject: [PATCH 3/5] Move update_lints logic to its own module --- clippy_dev/src/main.rs | 180 +-------------------------------- clippy_dev/src/update_lints.rs | 163 +++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 175 deletions(-) create mode 100644 clippy_dev/src/update_lints.rs diff --git a/clippy_dev/src/main.rs b/clippy_dev/src/main.rs index dadb2d7c04c..d99235f7c07 100644 --- a/clippy_dev/src/main.rs +++ b/clippy_dev/src/main.rs @@ -1,19 +1,7 @@ #![cfg_attr(feature = "deny-warnings", deny(warnings))] use clap::{App, Arg, SubCommand}; -use clippy_dev::{ - gather_all, gen_changelog_lint_list, gen_deprecated, gen_lint_group_list, gen_modules_list, gen_register_lint_list, - replace_region_in_file, Lint, DOCS_LINK, -}; -use std::path::Path; - -use clippy_dev::{fmt, new_lint, stderr_length_check}; - -#[derive(Clone, Copy, PartialEq)] -enum UpdateMode { - Check, - Change, -} +use clippy_dev::{fmt, new_lint, stderr_length_check, update_lints}; fn main() { let matches = App::new("Clippy developer tooling") @@ -107,11 +95,11 @@ fn main() { }, ("update_lints", Some(matches)) => { if matches.is_present("print-only") { - print_lints(); + update_lints::print_lints(); } else if matches.is_present("check") { - update_lints(UpdateMode::Check); + update_lints::run(update_lints::UpdateMode::Check); } else { - update_lints(UpdateMode::Change); + update_lints::run(update_lints::UpdateMode::Change); } }, ("new_lint", Some(matches)) => { @@ -120,7 +108,7 @@ fn main() { matches.value_of("name"), matches.value_of("category"), ) { - Ok(_) => update_lints(UpdateMode::Change), + Ok(_) => update_lints::run(update_lints::UpdateMode::Change), Err(e) => eprintln!("Unable to create lint: {}", e), } }, @@ -130,161 +118,3 @@ fn main() { _ => {}, } } - -fn print_lints() { - let lint_list = gather_all(); - let usable_lints: Vec = Lint::usable_lints(lint_list).collect(); - let usable_lint_count = usable_lints.len(); - let grouped_by_lint_group = Lint::by_lint_group(usable_lints.into_iter()); - - for (lint_group, mut lints) in grouped_by_lint_group { - if lint_group == "Deprecated" { - continue; - } - println!("\n## {}", lint_group); - - lints.sort_by_key(|l| l.name.clone()); - - for lint in lints { - println!( - "* [{}]({}#{}) ({})", - lint.name, - clippy_dev::DOCS_LINK, - lint.name, - lint.desc - ); - } - } - - println!("there are {} lints", usable_lint_count); -} - -#[allow(clippy::too_many_lines)] -fn update_lints(update_mode: UpdateMode) { - let lint_list: Vec = gather_all().collect(); - - let internal_lints = Lint::internal_lints(lint_list.clone().into_iter()); - - let usable_lints: Vec = Lint::usable_lints(lint_list.clone().into_iter()).collect(); - let usable_lint_count = usable_lints.len(); - - let mut sorted_usable_lints = usable_lints.clone(); - sorted_usable_lints.sort_by_key(|lint| lint.name.clone()); - - let mut file_change = replace_region_in_file( - Path::new("src/lintlist/mod.rs"), - "begin lint list", - "end lint list", - false, - update_mode == UpdateMode::Change, - || { - format!( - "pub const ALL_LINTS: [Lint; {}] = {:#?};", - sorted_usable_lints.len(), - sorted_usable_lints - ) - .lines() - .map(ToString::to_string) - .collect::>() - }, - ) - .changed; - - file_change |= replace_region_in_file( - Path::new("README.md"), - &format!(r#"\[There are \d+ lints included in this crate!\]\({}\)"#, DOCS_LINK), - "", - true, - update_mode == UpdateMode::Change, - || { - vec![format!( - "[There are {} lints included in this crate!]({})", - usable_lint_count, DOCS_LINK - )] - }, - ) - .changed; - - file_change |= replace_region_in_file( - Path::new("CHANGELOG.md"), - "", - "", - false, - update_mode == UpdateMode::Change, - || gen_changelog_lint_list(lint_list.clone()), - ) - .changed; - - file_change |= replace_region_in_file( - Path::new("clippy_lints/src/lib.rs"), - "begin deprecated lints", - "end deprecated lints", - false, - update_mode == UpdateMode::Change, - || gen_deprecated(&lint_list), - ) - .changed; - - file_change |= replace_region_in_file( - Path::new("clippy_lints/src/lib.rs"), - "begin register lints", - "end register lints", - false, - update_mode == UpdateMode::Change, - || gen_register_lint_list(&lint_list), - ) - .changed; - - file_change |= replace_region_in_file( - Path::new("clippy_lints/src/lib.rs"), - "begin lints modules", - "end lints modules", - false, - update_mode == UpdateMode::Change, - || gen_modules_list(lint_list.clone()), - ) - .changed; - - // Generate lists of lints in the clippy::all lint group - file_change |= replace_region_in_file( - Path::new("clippy_lints/src/lib.rs"), - r#"store.register_group\(true, "clippy::all""#, - r#"\]\);"#, - false, - update_mode == UpdateMode::Change, - || { - // clippy::all should only include the following lint groups: - let all_group_lints = usable_lints - .clone() - .into_iter() - .filter(|l| { - l.group == "correctness" || l.group == "style" || l.group == "complexity" || l.group == "perf" - }) - .collect(); - - gen_lint_group_list(all_group_lints) - }, - ) - .changed; - - // Generate the list of lints for all other lint groups - for (lint_group, lints) in Lint::by_lint_group(usable_lints.into_iter().chain(internal_lints)) { - file_change |= replace_region_in_file( - Path::new("clippy_lints/src/lib.rs"), - &format!("store.register_group\\(true, \"clippy::{}\"", lint_group), - r#"\]\);"#, - false, - update_mode == UpdateMode::Change, - || gen_lint_group_list(lints.clone()), - ) - .changed; - } - - if update_mode == UpdateMode::Check && file_change { - println!( - "Not all lints defined properly. \ - Please run `cargo dev update_lints` to make sure all lints are defined properly." - ); - std::process::exit(1); - } -} diff --git a/clippy_dev/src/update_lints.rs b/clippy_dev/src/update_lints.rs new file mode 100644 index 00000000000..f6fc322431c --- /dev/null +++ b/clippy_dev/src/update_lints.rs @@ -0,0 +1,163 @@ +use crate::{ + gather_all, gen_changelog_lint_list, gen_deprecated, gen_lint_group_list, gen_modules_list, gen_register_lint_list, + replace_region_in_file, Lint, DOCS_LINK, +}; +use std::path::Path; + +#[derive(Clone, Copy, PartialEq)] +pub enum UpdateMode { + Check, + Change, +} + +#[allow(clippy::too_many_lines)] +pub fn run(update_mode: UpdateMode) { + let lint_list: Vec = gather_all().collect(); + + let internal_lints = Lint::internal_lints(lint_list.clone().into_iter()); + + let usable_lints: Vec = Lint::usable_lints(lint_list.clone().into_iter()).collect(); + let usable_lint_count = usable_lints.len(); + + let mut sorted_usable_lints = usable_lints.clone(); + sorted_usable_lints.sort_by_key(|lint| lint.name.clone()); + + let mut file_change = replace_region_in_file( + Path::new("src/lintlist/mod.rs"), + "begin lint list", + "end lint list", + false, + update_mode == UpdateMode::Change, + || { + format!( + "pub const ALL_LINTS: [Lint; {}] = {:#?};", + sorted_usable_lints.len(), + sorted_usable_lints + ) + .lines() + .map(ToString::to_string) + .collect::>() + }, + ) + .changed; + + file_change |= replace_region_in_file( + Path::new("README.md"), + &format!(r#"\[There are \d+ lints included in this crate!\]\({}\)"#, DOCS_LINK), + "", + true, + update_mode == UpdateMode::Change, + || { + vec![format!( + "[There are {} lints included in this crate!]({})", + usable_lint_count, DOCS_LINK + )] + }, + ) + .changed; + + file_change |= replace_region_in_file( + Path::new("CHANGELOG.md"), + "", + "", + false, + update_mode == UpdateMode::Change, + || gen_changelog_lint_list(lint_list.clone()), + ) + .changed; + + file_change |= replace_region_in_file( + Path::new("clippy_lints/src/lib.rs"), + "begin deprecated lints", + "end deprecated lints", + false, + update_mode == UpdateMode::Change, + || gen_deprecated(&lint_list), + ) + .changed; + + file_change |= replace_region_in_file( + Path::new("clippy_lints/src/lib.rs"), + "begin register lints", + "end register lints", + false, + update_mode == UpdateMode::Change, + || gen_register_lint_list(&lint_list), + ) + .changed; + + file_change |= replace_region_in_file( + Path::new("clippy_lints/src/lib.rs"), + "begin lints modules", + "end lints modules", + false, + update_mode == UpdateMode::Change, + || gen_modules_list(lint_list.clone()), + ) + .changed; + + // Generate lists of lints in the clippy::all lint group + file_change |= replace_region_in_file( + Path::new("clippy_lints/src/lib.rs"), + r#"store.register_group\(true, "clippy::all""#, + r#"\]\);"#, + false, + update_mode == UpdateMode::Change, + || { + // clippy::all should only include the following lint groups: + let all_group_lints = usable_lints + .clone() + .into_iter() + .filter(|l| { + l.group == "correctness" || l.group == "style" || l.group == "complexity" || l.group == "perf" + }) + .collect(); + + gen_lint_group_list(all_group_lints) + }, + ) + .changed; + + // Generate the list of lints for all other lint groups + for (lint_group, lints) in Lint::by_lint_group(usable_lints.into_iter().chain(internal_lints)) { + file_change |= replace_region_in_file( + Path::new("clippy_lints/src/lib.rs"), + &format!("store.register_group\\(true, \"clippy::{}\"", lint_group), + r#"\]\);"#, + false, + update_mode == UpdateMode::Change, + || gen_lint_group_list(lints.clone()), + ) + .changed; + } + + if update_mode == UpdateMode::Check && file_change { + println!( + "Not all lints defined properly. \ + Please run `cargo dev update_lints` to make sure all lints are defined properly." + ); + std::process::exit(1); + } +} + +pub fn print_lints() { + let lint_list = gather_all(); + let usable_lints: Vec = Lint::usable_lints(lint_list).collect(); + let usable_lint_count = usable_lints.len(); + let grouped_by_lint_group = Lint::by_lint_group(usable_lints.into_iter()); + + for (lint_group, mut lints) in grouped_by_lint_group { + if lint_group == "Deprecated" { + continue; + } + println!("\n## {}", lint_group); + + lints.sort_by_key(|l| l.name.clone()); + + for lint in lints { + println!("* [{}]({}#{}) ({})", lint.name, DOCS_LINK, lint.name, lint.desc); + } + } + + println!("there are {} lints", usable_lint_count); +} From 3155eedb68b4aaefe89731b3e1c788453cee1f80 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Tue, 31 Mar 2020 17:23:14 +0200 Subject: [PATCH 4/5] Don't use an exact lint counter anymore --- README.md | 2 +- clippy_dev/src/update_lints.rs | 25 ++++++++++++++----------- src/lintlist/mod.rs | 6 +++++- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 856058e58b0..eaa42aa6962 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code. -[There are 363 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html) +[There are over 363 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html) We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you: diff --git a/clippy_dev/src/update_lints.rs b/clippy_dev/src/update_lints.rs index f6fc322431c..d30d6f97a2f 100644 --- a/clippy_dev/src/update_lints.rs +++ b/clippy_dev/src/update_lints.rs @@ -17,7 +17,7 @@ pub fn run(update_mode: UpdateMode) { let internal_lints = Lint::internal_lints(lint_list.clone().into_iter()); let usable_lints: Vec = Lint::usable_lints(lint_list.clone().into_iter()).collect(); - let usable_lint_count = usable_lints.len(); + let usable_lint_count = round_to_fifty(usable_lints.len()); let mut sorted_usable_lints = usable_lints.clone(); sorted_usable_lints.sort_by_key(|lint| lint.name.clone()); @@ -29,27 +29,26 @@ pub fn run(update_mode: UpdateMode) { false, update_mode == UpdateMode::Change, || { - format!( - "pub const ALL_LINTS: [Lint; {}] = {:#?};", - sorted_usable_lints.len(), - sorted_usable_lints - ) - .lines() - .map(ToString::to_string) - .collect::>() + format!("pub static ref ALL_LINTS: Vec = vec!{:#?};", sorted_usable_lints) + .lines() + .map(ToString::to_string) + .collect::>() }, ) .changed; file_change |= replace_region_in_file( Path::new("README.md"), - &format!(r#"\[There are \d+ lints included in this crate!\]\({}\)"#, DOCS_LINK), + &format!( + r#"\[There are over \d+ lints included in this crate!\]\({}\)"#, + DOCS_LINK + ), "", true, update_mode == UpdateMode::Change, || { vec![format!( - "[There are {} lints included in this crate!]({})", + "[There are over {} lints included in this crate!]({})", usable_lint_count, DOCS_LINK )] }, @@ -161,3 +160,7 @@ pub fn print_lints() { println!("there are {} lints", usable_lint_count); } + +fn round_to_fifty(count: usize) -> usize { + count / 50 * 50 +} diff --git a/src/lintlist/mod.rs b/src/lintlist/mod.rs index fa51af156ef..3b89f5d1947 100644 --- a/src/lintlist/mod.rs +++ b/src/lintlist/mod.rs @@ -1,12 +1,15 @@ //! This file is managed by `cargo dev update_lints`. Do not edit. +use lazy_static::lazy_static; + pub mod lint; pub use lint::Level; pub use lint::Lint; pub use lint::LINT_LEVELS; +lazy_static! { // begin lint list, do not remove this comment, it’s used in `update_lints` -pub const ALL_LINTS: [Lint; 363] = [ +pub static ref ALL_LINTS: Vec = vec![ Lint { name: "absurd_extreme_comparisons", group: "correctness", @@ -2550,3 +2553,4 @@ pub const ALL_LINTS: [Lint; 363] = [ }, ]; // end lint list, do not remove this comment, it’s used in `update_lints` +} From cbdf4cc71e36baa780bd4bc7aca66bc344ab70ec Mon Sep 17 00:00:00 2001 From: flip1995 Date: Tue, 31 Mar 2020 17:23:30 +0200 Subject: [PATCH 5/5] Run update_lints --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index eaa42aa6962..2a30f5e8e53 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code. -[There are over 363 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html) +[There are over 350 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html) We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you: