Skip to content

Commit 98c30fe

Browse files
committed
Build lint lists once and the reuse them to update files
1 parent da67982 commit 98c30fe

File tree

2 files changed

+43
-40
lines changed

2 files changed

+43
-40
lines changed

clippy_dev/src/lib.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,25 @@ impl Lint {
6262
}
6363

6464
/// Returns all non-deprecated lints and non-internal lints
65-
pub fn usable_lints(lints: impl Iterator<Item = Self>) -> impl Iterator<Item = Self> {
66-
lints.filter(|l| l.deprecation.is_none() && !l.group.starts_with("internal"))
65+
#[must_use]
66+
pub fn usable_lints(lints: &[Self]) -> Vec<Self> {
67+
lints
68+
.iter()
69+
.filter(|l| l.deprecation.is_none() && !l.group.starts_with("internal"))
70+
.cloned()
71+
.collect()
6772
}
6873

6974
/// Returns all internal lints (not `internal_warn` lints)
70-
pub fn internal_lints(lints: impl Iterator<Item = Self>) -> impl Iterator<Item = Self> {
71-
lints.filter(|l| l.group == "internal")
75+
#[must_use]
76+
pub fn internal_lints(lints: &[Self]) -> Vec<Self> {
77+
lints.iter().filter(|l| l.group == "internal").cloned().collect()
78+
}
79+
80+
/// Returns all deprecated lints
81+
#[must_use]
82+
pub fn deprecated_lints(lints: &[Self]) -> Vec<Self> {
83+
lints.iter().filter(|l| l.deprecation.is_some()).cloned().collect()
7284
}
7385

7486
/// Returns the lints in a `HashMap`, grouped by the different lint groups
@@ -80,9 +92,8 @@ impl Lint {
8092

8193
/// Generates the Vec items for `register_lint_group` calls in `clippy_lints/src/lib.rs`.
8294
#[must_use]
83-
pub fn gen_lint_group_list(lints: &[Lint]) -> Vec<String> {
95+
pub fn gen_lint_group_list<'a>(lints: impl Iterator<Item = &'a Lint>) -> Vec<String> {
8496
lints
85-
.into_iter()
8697
.filter_map(|l| {
8798
if l.deprecation.is_some() {
8899
None
@@ -96,9 +107,8 @@ pub fn gen_lint_group_list(lints: &[Lint]) -> Vec<String> {
96107

97108
/// Generates the `pub mod module_name` list in `clippy_lints/src/lib.rs`.
98109
#[must_use]
99-
pub fn gen_modules_list(lints: &[Lint]) -> Vec<String> {
110+
pub fn gen_modules_list<'a>(lints: impl Iterator<Item = &'a Lint>) -> Vec<String> {
100111
lints
101-
.iter()
102112
.map(|l| &l.module)
103113
.unique()
104114
.map(|module| format!("pub mod {};", module))
@@ -108,9 +118,8 @@ pub fn gen_modules_list(lints: &[Lint]) -> Vec<String> {
108118

109119
/// Generates the list of lint links at the bottom of the README
110120
#[must_use]
111-
pub fn gen_changelog_lint_list(lints: &[Lint]) -> Vec<String> {
121+
pub fn gen_changelog_lint_list<'a>(lints: impl Iterator<Item = &'a Lint>) -> Vec<String> {
112122
lints
113-
.iter()
114123
.sorted_by_key(|l| l.name.clone())
115124
.filter_map(|l| {
116125
if l.group.starts_with("internal") {
@@ -124,9 +133,8 @@ pub fn gen_changelog_lint_list(lints: &[Lint]) -> Vec<String> {
124133

125134
/// Generates the `register_removed` code in `./clippy_lints/src/lib.rs`.
126135
#[must_use]
127-
pub fn gen_deprecated(lints: &[Lint]) -> Vec<String> {
136+
pub fn gen_deprecated<'a>(lints: impl Iterator<Item = &'a Lint>) -> Vec<String> {
128137
lints
129-
.iter()
130138
.filter_map(|l| {
131139
l.clone().deprecation.map(|depr_text| {
132140
vec![
@@ -142,11 +150,10 @@ pub fn gen_deprecated(lints: &[Lint]) -> Vec<String> {
142150
}
143151

144152
#[must_use]
145-
pub fn gen_register_lint_list(lints: &[Lint]) -> Vec<String> {
153+
pub fn gen_register_lint_list<'a>(lints: impl Iterator<Item = &'a Lint>) -> Vec<String> {
146154
let pre = " store.register_lints(&[".to_string();
147155
let post = " ]);".to_string();
148156
let mut inner = lints
149-
.iter()
150157
.map(|l| format!(" &{}::{},", l.module, l.name.to_uppercase()))
151158
.sorted()
152159
.collect::<Vec<String>>();
@@ -421,7 +428,7 @@ fn test_usable_lints() {
421428
None,
422429
"module_name",
423430
)];
424-
assert_eq!(expected, Lint::usable_lints(lints.into_iter()).collect::<Vec<Lint>>());
431+
assert_eq!(expected, Lint::usable_lints(&lints));
425432
}
426433

427434
#[test]
@@ -457,7 +464,7 @@ fn test_gen_changelog_lint_list() {
457464
format!("[`should_assert_eq`]: {}#should_assert_eq", DOCS_LINK.to_string()),
458465
format!("[`should_assert_eq2`]: {}#should_assert_eq2", DOCS_LINK.to_string()),
459466
];
460-
assert_eq!(expected, gen_changelog_lint_list(&lints));
467+
assert_eq!(expected, gen_changelog_lint_list(lints.iter()));
461468
}
462469

463470
#[test]
@@ -492,7 +499,7 @@ fn test_gen_deprecated() {
492499
.into_iter()
493500
.map(String::from)
494501
.collect();
495-
assert_eq!(expected, gen_deprecated(&lints));
502+
assert_eq!(expected, gen_deprecated(lints.iter()));
496503
}
497504

498505
#[test]
@@ -507,7 +514,7 @@ fn test_gen_modules_list() {
507514
"pub mod another_module;".to_string(),
508515
"pub mod module_name;".to_string(),
509516
];
510-
assert_eq!(expected, gen_modules_list(&lints));
517+
assert_eq!(expected, gen_modules_list(lints.iter()));
511518
}
512519

513520
#[test]
@@ -523,5 +530,5 @@ fn test_gen_lint_group_list() {
523530
" LintId::of(&module_name::INTERNAL),".to_string(),
524531
" LintId::of(&module_name::SHOULD_ASSERT_EQ),".to_string(),
525532
];
526-
assert_eq!(expected, gen_lint_group_list(&lints));
533+
assert_eq!(expected, gen_lint_group_list(lints.iter()));
527534
}

clippy_dev/src/update_lints.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ pub enum UpdateMode {
1414
pub fn run(update_mode: UpdateMode) {
1515
let lint_list: Vec<Lint> = gather_all().collect();
1616

17-
let internal_lints = Lint::internal_lints(lint_list.clone().into_iter());
18-
19-
let usable_lints: Vec<Lint> = Lint::usable_lints(lint_list.clone().into_iter()).collect();
20-
let usable_lint_count = round_to_fifty(usable_lints.len());
21-
17+
let internal_lints = Lint::internal_lints(&lint_list);
18+
let deprecated_lints = Lint::deprecated_lints(&lint_list);
19+
let usable_lints = Lint::usable_lints(&lint_list);
2220
let mut sorted_usable_lints = usable_lints.clone();
2321
sorted_usable_lints.sort_by_key(|lint| lint.name.clone());
2422

23+
let usable_lint_count = round_to_fifty(usable_lints.len());
24+
2525
let mut file_change = replace_region_in_file(
2626
Path::new("src/lintlist/mod.rs"),
2727
"begin lint list",
@@ -61,7 +61,7 @@ pub fn run(update_mode: UpdateMode) {
6161
"<!-- end autogenerated links to lint list -->",
6262
false,
6363
update_mode == UpdateMode::Change,
64-
|| gen_changelog_lint_list(&lint_list),
64+
|| gen_changelog_lint_list(usable_lints.iter().chain(deprecated_lints.iter())),
6565
)
6666
.changed;
6767

@@ -71,7 +71,7 @@ pub fn run(update_mode: UpdateMode) {
7171
"end deprecated lints",
7272
false,
7373
update_mode == UpdateMode::Change,
74-
|| gen_deprecated(&lint_list),
74+
|| gen_deprecated(deprecated_lints.iter()),
7575
)
7676
.changed;
7777

@@ -81,7 +81,7 @@ pub fn run(update_mode: UpdateMode) {
8181
"end register lints",
8282
false,
8383
update_mode == UpdateMode::Change,
84-
|| gen_register_lint_list(&usable_lints),
84+
|| gen_register_lint_list(usable_lints.iter().chain(internal_lints.iter())),
8585
)
8686
.changed;
8787

@@ -91,7 +91,7 @@ pub fn run(update_mode: UpdateMode) {
9191
"end lints modules",
9292
false,
9393
update_mode == UpdateMode::Change,
94-
|| gen_modules_list(&usable_lints),
94+
|| gen_modules_list(usable_lints.iter()),
9595
)
9696
.changed;
9797

@@ -104,15 +104,11 @@ pub fn run(update_mode: UpdateMode) {
104104
update_mode == UpdateMode::Change,
105105
|| {
106106
// clippy::all should only include the following lint groups:
107-
let all_group_lints = usable_lints
108-
.clone()
109-
.into_iter()
110-
.filter(|l| {
111-
l.group == "correctness" || l.group == "style" || l.group == "complexity" || l.group == "perf"
112-
})
113-
.collect::<Vec<_>>();
114-
115-
gen_lint_group_list(&all_group_lints)
107+
let all_group_lints = usable_lints.iter().filter(|l| {
108+
l.group == "correctness" || l.group == "style" || l.group == "complexity" || l.group == "perf"
109+
});
110+
111+
gen_lint_group_list(all_group_lints)
116112
},
117113
)
118114
.changed;
@@ -125,7 +121,7 @@ pub fn run(update_mode: UpdateMode) {
125121
r#"\]\);"#,
126122
false,
127123
update_mode == UpdateMode::Change,
128-
|| gen_lint_group_list(&lints),
124+
|| gen_lint_group_list(lints.iter()),
129125
)
130126
.changed;
131127
}
@@ -140,8 +136,8 @@ pub fn run(update_mode: UpdateMode) {
140136
}
141137

142138
pub fn print_lints() {
143-
let lint_list = gather_all();
144-
let usable_lints: Vec<Lint> = Lint::usable_lints(lint_list).collect();
139+
let lint_list: Vec<Lint> = gather_all().collect();
140+
let usable_lints = Lint::usable_lints(&lint_list);
145141
let usable_lint_count = usable_lints.len();
146142
let grouped_by_lint_group = Lint::by_lint_group(usable_lints.into_iter());
147143

0 commit comments

Comments
 (0)