Build lint lists once and the reuse them to update files

This commit is contained in:
flip1995
2020-04-02 22:04:54 +02:00
parent da679825e0
commit 98c30fea8c
2 changed files with 42 additions and 39 deletions

View File

@@ -62,13 +62,25 @@ impl Lint {
} }
/// Returns all non-deprecated lints and non-internal lints /// Returns all non-deprecated lints and non-internal lints
pub fn usable_lints(lints: impl Iterator<Item = Self>) -> impl Iterator<Item = Self> { #[must_use]
lints.filter(|l| l.deprecation.is_none() && !l.group.starts_with("internal")) pub fn usable_lints(lints: &[Self]) -> Vec<Self> {
lints
.iter()
.filter(|l| l.deprecation.is_none() && !l.group.starts_with("internal"))
.cloned()
.collect()
} }
/// Returns all internal lints (not `internal_warn` lints) /// Returns all internal lints (not `internal_warn` lints)
pub fn internal_lints(lints: impl Iterator<Item = Self>) -> impl Iterator<Item = Self> { #[must_use]
lints.filter(|l| l.group == "internal") pub fn internal_lints(lints: &[Self]) -> Vec<Self> {
lints.iter().filter(|l| l.group == "internal").cloned().collect()
}
/// Returns all deprecated lints
#[must_use]
pub fn deprecated_lints(lints: &[Self]) -> Vec<Self> {
lints.iter().filter(|l| l.deprecation.is_some()).cloned().collect()
} }
/// Returns the lints in a `HashMap`, grouped by the different lint groups /// Returns the lints in a `HashMap`, grouped by the different lint groups
@@ -80,9 +92,8 @@ impl Lint {
/// Generates the Vec items for `register_lint_group` calls in `clippy_lints/src/lib.rs`. /// Generates the Vec items for `register_lint_group` calls in `clippy_lints/src/lib.rs`.
#[must_use] #[must_use]
pub fn gen_lint_group_list(lints: &[Lint]) -> Vec<String> { pub fn gen_lint_group_list<'a>(lints: impl Iterator<Item = &'a Lint>) -> Vec<String> {
lints lints
.into_iter()
.filter_map(|l| { .filter_map(|l| {
if l.deprecation.is_some() { if l.deprecation.is_some() {
None None
@@ -96,9 +107,8 @@ pub fn gen_lint_group_list(lints: &[Lint]) -> Vec<String> {
/// Generates the `pub mod module_name` list in `clippy_lints/src/lib.rs`. /// Generates the `pub mod module_name` list in `clippy_lints/src/lib.rs`.
#[must_use] #[must_use]
pub fn gen_modules_list(lints: &[Lint]) -> Vec<String> { pub fn gen_modules_list<'a>(lints: impl Iterator<Item = &'a Lint>) -> Vec<String> {
lints lints
.iter()
.map(|l| &l.module) .map(|l| &l.module)
.unique() .unique()
.map(|module| format!("pub mod {};", module)) .map(|module| format!("pub mod {};", module))
@@ -108,9 +118,8 @@ pub fn gen_modules_list(lints: &[Lint]) -> Vec<String> {
/// Generates the list of lint links at the bottom of the README /// Generates the list of lint links at the bottom of the README
#[must_use] #[must_use]
pub fn gen_changelog_lint_list(lints: &[Lint]) -> Vec<String> { pub fn gen_changelog_lint_list<'a>(lints: impl Iterator<Item = &'a Lint>) -> Vec<String> {
lints lints
.iter()
.sorted_by_key(|l| l.name.clone()) .sorted_by_key(|l| l.name.clone())
.filter_map(|l| { .filter_map(|l| {
if l.group.starts_with("internal") { if l.group.starts_with("internal") {
@@ -124,9 +133,8 @@ pub fn gen_changelog_lint_list(lints: &[Lint]) -> Vec<String> {
/// Generates the `register_removed` code in `./clippy_lints/src/lib.rs`. /// Generates the `register_removed` code in `./clippy_lints/src/lib.rs`.
#[must_use] #[must_use]
pub fn gen_deprecated(lints: &[Lint]) -> Vec<String> { pub fn gen_deprecated<'a>(lints: impl Iterator<Item = &'a Lint>) -> Vec<String> {
lints lints
.iter()
.filter_map(|l| { .filter_map(|l| {
l.clone().deprecation.map(|depr_text| { l.clone().deprecation.map(|depr_text| {
vec![ vec![
@@ -142,11 +150,10 @@ pub fn gen_deprecated(lints: &[Lint]) -> Vec<String> {
} }
#[must_use] #[must_use]
pub fn gen_register_lint_list(lints: &[Lint]) -> Vec<String> { pub fn gen_register_lint_list<'a>(lints: impl Iterator<Item = &'a Lint>) -> Vec<String> {
let pre = " store.register_lints(&[".to_string(); let pre = " store.register_lints(&[".to_string();
let post = " ]);".to_string(); let post = " ]);".to_string();
let mut inner = lints let mut inner = lints
.iter()
.map(|l| format!(" &{}::{},", l.module, l.name.to_uppercase())) .map(|l| format!(" &{}::{},", l.module, l.name.to_uppercase()))
.sorted() .sorted()
.collect::<Vec<String>>(); .collect::<Vec<String>>();
@@ -421,7 +428,7 @@ fn test_usable_lints() {
None, None,
"module_name", "module_name",
)]; )];
assert_eq!(expected, Lint::usable_lints(lints.into_iter()).collect::<Vec<Lint>>()); assert_eq!(expected, Lint::usable_lints(&lints));
} }
#[test] #[test]
@@ -457,7 +464,7 @@ fn test_gen_changelog_lint_list() {
format!("[`should_assert_eq`]: {}#should_assert_eq", DOCS_LINK.to_string()), format!("[`should_assert_eq`]: {}#should_assert_eq", DOCS_LINK.to_string()),
format!("[`should_assert_eq2`]: {}#should_assert_eq2", DOCS_LINK.to_string()), format!("[`should_assert_eq2`]: {}#should_assert_eq2", DOCS_LINK.to_string()),
]; ];
assert_eq!(expected, gen_changelog_lint_list(&lints)); assert_eq!(expected, gen_changelog_lint_list(lints.iter()));
} }
#[test] #[test]
@@ -492,7 +499,7 @@ fn test_gen_deprecated() {
.into_iter() .into_iter()
.map(String::from) .map(String::from)
.collect(); .collect();
assert_eq!(expected, gen_deprecated(&lints)); assert_eq!(expected, gen_deprecated(lints.iter()));
} }
#[test] #[test]
@@ -507,7 +514,7 @@ fn test_gen_modules_list() {
"pub mod another_module;".to_string(), "pub mod another_module;".to_string(),
"pub mod module_name;".to_string(), "pub mod module_name;".to_string(),
]; ];
assert_eq!(expected, gen_modules_list(&lints)); assert_eq!(expected, gen_modules_list(lints.iter()));
} }
#[test] #[test]
@@ -523,5 +530,5 @@ fn test_gen_lint_group_list() {
" LintId::of(&module_name::INTERNAL),".to_string(), " LintId::of(&module_name::INTERNAL),".to_string(),
" LintId::of(&module_name::SHOULD_ASSERT_EQ),".to_string(), " LintId::of(&module_name::SHOULD_ASSERT_EQ),".to_string(),
]; ];
assert_eq!(expected, gen_lint_group_list(&lints)); assert_eq!(expected, gen_lint_group_list(lints.iter()));
} }

View File

@@ -14,14 +14,14 @@ pub enum UpdateMode {
pub fn run(update_mode: UpdateMode) { pub fn run(update_mode: UpdateMode) {
let lint_list: Vec<Lint> = gather_all().collect(); let lint_list: Vec<Lint> = gather_all().collect();
let internal_lints = Lint::internal_lints(lint_list.clone().into_iter()); let internal_lints = Lint::internal_lints(&lint_list);
let deprecated_lints = Lint::deprecated_lints(&lint_list);
let usable_lints: Vec<Lint> = Lint::usable_lints(lint_list.clone().into_iter()).collect(); let usable_lints = Lint::usable_lints(&lint_list);
let usable_lint_count = round_to_fifty(usable_lints.len());
let mut sorted_usable_lints = usable_lints.clone(); let mut sorted_usable_lints = usable_lints.clone();
sorted_usable_lints.sort_by_key(|lint| lint.name.clone()); sorted_usable_lints.sort_by_key(|lint| lint.name.clone());
let usable_lint_count = round_to_fifty(usable_lints.len());
let mut file_change = replace_region_in_file( let mut file_change = replace_region_in_file(
Path::new("src/lintlist/mod.rs"), Path::new("src/lintlist/mod.rs"),
"begin lint list", "begin lint list",
@@ -61,7 +61,7 @@ pub fn run(update_mode: UpdateMode) {
"<!-- end autogenerated links to lint list -->", "<!-- end autogenerated links to lint list -->",
false, false,
update_mode == UpdateMode::Change, update_mode == UpdateMode::Change,
|| gen_changelog_lint_list(&lint_list), || gen_changelog_lint_list(usable_lints.iter().chain(deprecated_lints.iter())),
) )
.changed; .changed;
@@ -71,7 +71,7 @@ pub fn run(update_mode: UpdateMode) {
"end deprecated lints", "end deprecated lints",
false, false,
update_mode == UpdateMode::Change, update_mode == UpdateMode::Change,
|| gen_deprecated(&lint_list), || gen_deprecated(deprecated_lints.iter()),
) )
.changed; .changed;
@@ -81,7 +81,7 @@ pub fn run(update_mode: UpdateMode) {
"end register lints", "end register lints",
false, false,
update_mode == UpdateMode::Change, update_mode == UpdateMode::Change,
|| gen_register_lint_list(&usable_lints), || gen_register_lint_list(usable_lints.iter().chain(internal_lints.iter())),
) )
.changed; .changed;
@@ -91,7 +91,7 @@ pub fn run(update_mode: UpdateMode) {
"end lints modules", "end lints modules",
false, false,
update_mode == UpdateMode::Change, update_mode == UpdateMode::Change,
|| gen_modules_list(&usable_lints), || gen_modules_list(usable_lints.iter()),
) )
.changed; .changed;
@@ -104,15 +104,11 @@ pub fn run(update_mode: UpdateMode) {
update_mode == UpdateMode::Change, update_mode == UpdateMode::Change,
|| { || {
// clippy::all should only include the following lint groups: // clippy::all should only include the following lint groups:
let all_group_lints = usable_lints let all_group_lints = usable_lints.iter().filter(|l| {
.clone()
.into_iter()
.filter(|l| {
l.group == "correctness" || l.group == "style" || l.group == "complexity" || l.group == "perf" l.group == "correctness" || l.group == "style" || l.group == "complexity" || l.group == "perf"
}) });
.collect::<Vec<_>>();
gen_lint_group_list(&all_group_lints) gen_lint_group_list(all_group_lints)
}, },
) )
.changed; .changed;
@@ -125,7 +121,7 @@ pub fn run(update_mode: UpdateMode) {
r#"\]\);"#, r#"\]\);"#,
false, false,
update_mode == UpdateMode::Change, update_mode == UpdateMode::Change,
|| gen_lint_group_list(&lints), || gen_lint_group_list(lints.iter()),
) )
.changed; .changed;
} }
@@ -140,8 +136,8 @@ pub fn run(update_mode: UpdateMode) {
} }
pub fn print_lints() { pub fn print_lints() {
let lint_list = gather_all(); let lint_list: Vec<Lint> = gather_all().collect();
let usable_lints: Vec<Lint> = Lint::usable_lints(lint_list).collect(); let usable_lints = Lint::usable_lints(&lint_list);
let usable_lint_count = usable_lints.len(); let usable_lint_count = usable_lints.len();
let grouped_by_lint_group = Lint::by_lint_group(usable_lints.into_iter()); let grouped_by_lint_group = Lint::by_lint_group(usable_lints.into_iter());