From 5fc25d30e25fa5c05c34812eab311c42b464bb17 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Thu, 1 Nov 2018 20:16:38 +0100 Subject: [PATCH 1/5] RIIR update lints: Generate modules section --- clippy_dev/src/lib.rs | 38 ++++++++++++++++++++++++++++++++++++-- clippy_dev/src/main.rs | 8 ++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/clippy_dev/src/lib.rs b/clippy_dev/src/lib.rs index 656a271aec9..53b9d5e18ec 100644 --- a/clippy_dev/src/lib.rs +++ b/clippy_dev/src/lib.rs @@ -72,6 +72,19 @@ impl Lint { } } +/// Generates the `pub mod module_name` list in `clippy_lints/src/lib.rs`. +pub fn gen_modules_list(lints: Vec) -> Vec { + lints.into_iter() + .filter_map(|l| { + if l.is_internal() || l.deprecation.is_some() { None } else { Some(l.module) } + }) + .unique() + .map(|module| { + format!("pub mod {};", module) + }) + .sorted() +} + /// Generates the list of lint links at the bottom of the README pub fn gen_changelog_lint_list(lints: Vec) -> Vec { let mut lint_list_sorted: Vec = lints; @@ -113,7 +126,13 @@ fn gather_from_file(dir_entry: &walkdir::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()) + let mut filename = dir_entry.path().file_stem().unwrap().to_str().unwrap(); + // If the lints are stored in mod.rs, we get the module name from + // the containing directory: + if filename == "mod" { + filename = dir_entry.path().parent().unwrap().file_stem().unwrap().to_str().unwrap() + } + parse_contents(&content, filename) } fn parse_contents(content: &str, filename: &str) -> impl Iterator { @@ -215,7 +234,7 @@ pub fn replace_region_in_text(text: &str, start: &str, end: &str, replace_sta // This happens if the provided regex in `clippy_dev/src/main.rs` is not found in the // given text or file. Most likely this is an error on the programmer's side and the Regex // is incorrect. - println!("regex {:?} not found. You may have to update it.", start); + eprintln!("error: regex `{:?}` not found. You may have to update it.", start); } new_lines.join("\n") } @@ -356,3 +375,18 @@ fn test_gen_deprecated() { ]; assert_eq!(expected, gen_deprecated(&lints)); } + +#[test] +fn test_gen_modules_list() { + let lints = vec![ + Lint::new("should_assert_eq", "group1", "abc", None, "module_name"), + Lint::new("should_assert_eq2", "group2", "abc", Some("abc"), "deprecated"), + Lint::new("incorrect_internal", "internal_style", "abc", None, "another_module"), + Lint::new("incorrect_internal", "internal_style", "abc", None, "module_name"), + ]; + let expected = vec![ + "pub mod another_module;\n".to_string(), + "pub mod module_name;\n".to_string(), + ]; + assert_eq!(expected, gen_modules_list(lints)); +} diff --git a/clippy_dev/src/main.rs b/clippy_dev/src/main.rs index 887a4ab9328..4832b428e9c 100644 --- a/clippy_dev/src/main.rs +++ b/clippy_dev/src/main.rs @@ -90,4 +90,12 @@ fn update_lints() { false, || { gen_deprecated(&lint_list) } ); + + replace_region_in_file( + "../clippy_lints/src/lib.rs", + "begin lints modules", + "end lints modules", + false, + || { gen_modules_list(lint_list.clone()) } + ); } From 6e3320c7efc9cdaa1ddb3865181ec017d9f5de26 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Sat, 3 Nov 2018 10:58:45 +0100 Subject: [PATCH 2/5] Test clippy_dev on CI and fix test --- ci/base-tests.sh | 1 + clippy_dev/src/lib.rs | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ci/base-tests.sh b/ci/base-tests.sh index 9b73263c24a..dc0802a941d 100755 --- a/ci/base-tests.sh +++ b/ci/base-tests.sh @@ -22,6 +22,7 @@ cargo build --features debugging cargo test --features debugging cd clippy_lints && cargo test && cd .. cd rustc_tools_util && cargo test && cd .. +cd clippy_dev && cargo test && cd .. # check that the lint lists are up-to-date ./util/update_lints.py -c diff --git a/clippy_dev/src/lib.rs b/clippy_dev/src/lib.rs index 53b9d5e18ec..28cc4600a09 100644 --- a/clippy_dev/src/lib.rs +++ b/clippy_dev/src/lib.rs @@ -381,12 +381,12 @@ fn test_gen_modules_list() { let lints = vec![ Lint::new("should_assert_eq", "group1", "abc", None, "module_name"), Lint::new("should_assert_eq2", "group2", "abc", Some("abc"), "deprecated"), - Lint::new("incorrect_internal", "internal_style", "abc", None, "another_module"), + Lint::new("incorrect_stuff", "group3", "abc", None, "another_module"), Lint::new("incorrect_internal", "internal_style", "abc", None, "module_name"), ]; let expected = vec![ - "pub mod another_module;\n".to_string(), - "pub mod module_name;\n".to_string(), + "pub mod another_module;".to_string(), + "pub mod module_name;".to_string(), ]; assert_eq!(expected, gen_modules_list(lints)); } From 4f38538d7585caed83bb6a95ede130f407ca7d3f Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Sat, 3 Nov 2018 12:59:13 +0100 Subject: [PATCH 3/5] RIIR update lints: Generate lint group registrations --- clippy_dev/src/lib.rs | 28 ++++++++++++++++++++++++++++ clippy_dev/src/main.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/clippy_dev/src/lib.rs b/clippy_dev/src/lib.rs index 28cc4600a09..f16ea14a3de 100644 --- a/clippy_dev/src/lib.rs +++ b/clippy_dev/src/lib.rs @@ -72,6 +72,19 @@ impl Lint { } } +/// Generates the Vec items for `register_lint_group` calls in `clippy_lints/src/lib.rs`. +pub fn gen_lint_group_list(lints: Vec) -> Vec { + lints.into_iter() + .filter_map(|l| { + if l.is_internal() || l.deprecation.is_some() { + None + } else { + Some(format!(" {}::{},", l.module, l.name.to_uppercase())) + } + }) + .sorted() +} + /// Generates the `pub mod module_name` list in `clippy_lints/src/lib.rs`. pub fn gen_modules_list(lints: Vec) -> Vec { lints.into_iter() @@ -390,3 +403,18 @@ fn test_gen_modules_list() { ]; assert_eq!(expected, gen_modules_list(lints)); } + +#[test] +fn test_gen_lint_group_list() { + let lints = vec![ + Lint::new("abc", "group1", "abc", None, "module_name"), + Lint::new("should_assert_eq", "group1", "abc", None, "module_name"), + Lint::new("should_assert_eq2", "group2", "abc", Some("abc"), "deprecated"), + Lint::new("incorrect_internal", "internal_style", "abc", None, "module_name"), + ]; + let expected = vec![ + " module_name::ABC,".to_string(), + " module_name::SHOULD_ASSERT_EQ,".to_string(), + ]; + assert_eq!(expected, gen_lint_group_list(lints)); +} diff --git a/clippy_dev/src/main.rs b/clippy_dev/src/main.rs index 4832b428e9c..128feaa8aea 100644 --- a/clippy_dev/src/main.rs +++ b/clippy_dev/src/main.rs @@ -98,4 +98,34 @@ fn update_lints() { false, || { gen_modules_list(lint_list.clone()) } ); + + // Generate lists of lints in the clippy::all lint group + replace_region_in_file( + "../clippy_lints/src/lib.rs", + r#"reg.register_lint_group\("clippy::all""#, + r#"\]\);"#, + false, + || { + // 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) + } + ); + + // Generate the list of lints for all other lint groups + for (lint_group, lints) in Lint::by_lint_group(&usable_lints) { + replace_region_in_file( + "../clippy_lints/src/lib.rs", + &format!("reg.register_lint_group\\(\"clippy::{}\"", lint_group), + r#"\]\);"#, + false, + || { gen_lint_group_list(lints.clone()) } + ); + } } From cca50701d9b798c821bb4c5c4a20eb02d8d5f3a8 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Sat, 3 Nov 2018 12:59:40 +0100 Subject: [PATCH 4/5] Improve clippy_dev help text --- clippy_dev/src/main.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/clippy_dev/src/main.rs b/clippy_dev/src/main.rs index 128feaa8aea..807e4a1b9af 100644 --- a/clippy_dev/src/main.rs +++ b/clippy_dev/src/main.rs @@ -19,12 +19,17 @@ fn main() { let matches = App::new("Clippy developer tooling") .subcommand( SubCommand::with_name("update_lints") - .about("Update the lint list") + .about("Makes sure that:\n \ + * the lint count in README.md is correct\n \ + * the changelog contains markdown link references at the bottom\n \ + * all lints groups include the correct lints\n \ + * lint modules in `clippy_lints/*` are visible in `src/lib.rs` via `pub mod`\n \ + * all lints are registered in the lint store") .arg( Arg::with_name("print-only") .long("print-only") .short("p") - .help("Print a table of lints to STDOUT. Does not modify any files."), + .help("Print a table of lints to STDOUT. This does not include deprecated and internal lints. (Does not modify any files)"), ) ) .get_matches(); From facfb5a7a979054c26fdb3adf11b1c6c762fde13 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Sat, 3 Nov 2018 18:48:39 +0100 Subject: [PATCH 5/5] Fix typo --- clippy_dev/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_dev/src/main.rs b/clippy_dev/src/main.rs index 807e4a1b9af..288fb7c58b4 100644 --- a/clippy_dev/src/main.rs +++ b/clippy_dev/src/main.rs @@ -22,7 +22,7 @@ fn main() { .about("Makes sure that:\n \ * the lint count in README.md is correct\n \ * the changelog contains markdown link references at the bottom\n \ - * all lints groups include the correct lints\n \ + * all lint groups include the correct lints\n \ * lint modules in `clippy_lints/*` are visible in `src/lib.rs` via `pub mod`\n \ * all lints are registered in the lint store") .arg(