Skip to content

Commit 3da43d0

Browse files
authored
Rollup merge of rust-lang#5406 - flip1995:update_lints_fix, r=flip1995
Fix update_lints This fixes a bug in update_lints, where `internal` lints were not registered properly. This also cleans up some code. For example: The code generation functions no longer filter the lints the are given. This is now the task of the caller. This way, it is more obvious in the `replace_in_file` calls which lints will be included in which part of a file. This also turns the lint modules private. There is no need for them to be public, since shared code should be in the utils module anyway. And last but not least, this fixes the `register_lints` code generation, so also internal lints get registered. changelog: none
2 parents acd4342 + 30503a9 commit 3da43d0

File tree

5 files changed

+262
-287
lines changed

5 files changed

+262
-287
lines changed

clippy_dev/src/lib.rs

Lines changed: 53 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -62,110 +62,89 @@ 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.is_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()
7278
}
7379

74-
/// Returns the lints in a `HashMap`, grouped by the different lint groups
80+
/// Returns all deprecated lints
7581
#[must_use]
76-
pub fn by_lint_group(lints: impl Iterator<Item = Self>) -> HashMap<String, Vec<Self>> {
77-
lints.map(|lint| (lint.group.to_string(), lint)).into_group_map()
82+
pub fn deprecated_lints(lints: &[Self]) -> Vec<Self> {
83+
lints.iter().filter(|l| l.deprecation.is_some()).cloned().collect()
7884
}
7985

86+
/// Returns the lints in a `HashMap`, grouped by the different lint groups
8087
#[must_use]
81-
pub fn is_internal(&self) -> bool {
82-
self.group.starts_with("internal")
88+
pub fn by_lint_group(lints: impl Iterator<Item = Self>) -> HashMap<String, Vec<Self>> {
89+
lints.map(|lint| (lint.group.to_string(), lint)).into_group_map()
8390
}
8491
}
8592

8693
/// Generates the Vec items for `register_lint_group` calls in `clippy_lints/src/lib.rs`.
8794
#[must_use]
88-
pub fn gen_lint_group_list(lints: Vec<Lint>) -> Vec<String> {
95+
pub fn gen_lint_group_list<'a>(lints: impl Iterator<Item = &'a Lint>) -> Vec<String> {
8996
lints
90-
.into_iter()
91-
.filter_map(|l| {
92-
if l.deprecation.is_some() {
93-
None
94-
} else {
95-
Some(format!(" LintId::of(&{}::{}),", l.module, l.name.to_uppercase()))
96-
}
97-
})
97+
.map(|l| format!(" LintId::of(&{}::{}),", l.module, l.name.to_uppercase()))
9898
.sorted()
9999
.collect::<Vec<String>>()
100100
}
101101

102102
/// Generates the `pub mod module_name` list in `clippy_lints/src/lib.rs`.
103103
#[must_use]
104-
pub fn gen_modules_list(lints: Vec<Lint>) -> Vec<String> {
104+
pub fn gen_modules_list<'a>(lints: impl Iterator<Item = &'a Lint>) -> Vec<String> {
105105
lints
106-
.into_iter()
107-
.filter_map(|l| {
108-
if l.is_internal() || l.deprecation.is_some() {
109-
None
110-
} else {
111-
Some(l.module)
112-
}
113-
})
106+
.map(|l| &l.module)
114107
.unique()
115-
.map(|module| format!("pub mod {};", module))
108+
.map(|module| format!("mod {};", module))
116109
.sorted()
117110
.collect::<Vec<String>>()
118111
}
119112

120113
/// Generates the list of lint links at the bottom of the README
121114
#[must_use]
122-
pub fn gen_changelog_lint_list(lints: Vec<Lint>) -> Vec<String> {
123-
let mut lint_list_sorted: Vec<Lint> = lints;
124-
lint_list_sorted.sort_by_key(|l| l.name.clone());
125-
lint_list_sorted
126-
.iter()
127-
.filter_map(|l| {
128-
if l.is_internal() {
129-
None
130-
} else {
131-
Some(format!("[`{}`]: {}#{}", l.name, DOCS_LINK, l.name))
132-
}
133-
})
115+
pub fn gen_changelog_lint_list<'a>(lints: impl Iterator<Item = &'a Lint>) -> Vec<String> {
116+
lints
117+
.sorted_by_key(|l| &l.name)
118+
.map(|l| format!("[`{}`]: {}#{}", l.name, DOCS_LINK, l.name))
134119
.collect()
135120
}
136121

137122
/// Generates the `register_removed` code in `./clippy_lints/src/lib.rs`.
138123
#[must_use]
139-
pub fn gen_deprecated(lints: &[Lint]) -> Vec<String> {
124+
pub fn gen_deprecated<'a>(lints: impl Iterator<Item = &'a Lint>) -> Vec<String> {
140125
lints
141-
.iter()
142-
.filter_map(|l| {
143-
l.clone().deprecation.map(|depr_text| {
144-
vec![
145-
" store.register_removed(".to_string(),
146-
format!(" \"clippy::{}\",", l.name),
147-
format!(" \"{}\",", depr_text),
148-
" );".to_string(),
149-
]
150-
})
126+
.flat_map(|l| {
127+
l.deprecation
128+
.clone()
129+
.map(|depr_text| {
130+
vec![
131+
" store.register_removed(".to_string(),
132+
format!(" \"clippy::{}\",", l.name),
133+
format!(" \"{}\",", depr_text),
134+
" );".to_string(),
135+
]
136+
})
137+
.expect("only deprecated lints should be passed")
151138
})
152-
.flatten()
153139
.collect::<Vec<String>>()
154140
}
155141

156142
#[must_use]
157-
pub fn gen_register_lint_list(lints: &[Lint]) -> Vec<String> {
143+
pub fn gen_register_lint_list<'a>(lints: impl Iterator<Item = &'a Lint>) -> Vec<String> {
158144
let pre = " store.register_lints(&[".to_string();
159145
let post = " ]);".to_string();
160146
let mut inner = lints
161-
.iter()
162-
.filter_map(|l| {
163-
if !l.is_internal() && l.deprecation.is_none() {
164-
Some(format!(" &{}::{},", l.module, l.name.to_uppercase()))
165-
} else {
166-
None
167-
}
168-
})
147+
.map(|l| format!(" &{}::{},", l.module, l.name.to_uppercase()))
169148
.sorted()
170149
.collect::<Vec<String>>();
171150
inner.insert(0, pre);
@@ -439,7 +418,7 @@ fn test_usable_lints() {
439418
None,
440419
"module_name",
441420
)];
442-
assert_eq!(expected, Lint::usable_lints(lints.into_iter()).collect::<Vec<Lint>>());
421+
assert_eq!(expected, Lint::usable_lints(&lints));
443422
}
444423

445424
#[test]
@@ -469,13 +448,12 @@ fn test_gen_changelog_lint_list() {
469448
let lints = vec![
470449
Lint::new("should_assert_eq", "group1", "abc", None, "module_name"),
471450
Lint::new("should_assert_eq2", "group2", "abc", None, "module_name"),
472-
Lint::new("incorrect_internal", "internal_style", "abc", None, "module_name"),
473451
];
474452
let expected = vec![
475453
format!("[`should_assert_eq`]: {}#should_assert_eq", DOCS_LINK.to_string()),
476454
format!("[`should_assert_eq2`]: {}#should_assert_eq2", DOCS_LINK.to_string()),
477455
];
478-
assert_eq!(expected, gen_changelog_lint_list(lints));
456+
assert_eq!(expected, gen_changelog_lint_list(lints.iter()));
479457
}
480458

481459
#[test]
@@ -495,7 +473,6 @@ fn test_gen_deprecated() {
495473
Some("will be removed"),
496474
"module_name",
497475
),
498-
Lint::new("should_assert_eq2", "group2", "abc", None, "module_name"),
499476
];
500477
let expected: Vec<String> = vec![
501478
" store.register_removed(",
@@ -510,36 +487,37 @@ fn test_gen_deprecated() {
510487
.into_iter()
511488
.map(String::from)
512489
.collect();
513-
assert_eq!(expected, gen_deprecated(&lints));
490+
assert_eq!(expected, gen_deprecated(lints.iter()));
491+
}
492+
493+
#[test]
494+
#[should_panic]
495+
fn test_gen_deprecated_fail() {
496+
let lints = vec![Lint::new("should_assert_eq2", "group2", "abc", None, "module_name")];
497+
let _ = gen_deprecated(lints.iter());
514498
}
515499

516500
#[test]
517501
fn test_gen_modules_list() {
518502
let lints = vec![
519503
Lint::new("should_assert_eq", "group1", "abc", None, "module_name"),
520-
Lint::new("should_assert_eq2", "group2", "abc", Some("abc"), "deprecated"),
521504
Lint::new("incorrect_stuff", "group3", "abc", None, "another_module"),
522-
Lint::new("incorrect_internal", "internal_style", "abc", None, "module_name"),
523-
];
524-
let expected = vec![
525-
"pub mod another_module;".to_string(),
526-
"pub mod module_name;".to_string(),
527505
];
528-
assert_eq!(expected, gen_modules_list(lints));
506+
let expected = vec!["mod another_module;".to_string(), "mod module_name;".to_string()];
507+
assert_eq!(expected, gen_modules_list(lints.iter()));
529508
}
530509

531510
#[test]
532511
fn test_gen_lint_group_list() {
533512
let lints = vec![
534513
Lint::new("abc", "group1", "abc", None, "module_name"),
535514
Lint::new("should_assert_eq", "group1", "abc", None, "module_name"),
536-
Lint::new("should_assert_eq2", "group2", "abc", Some("abc"), "deprecated"),
537515
Lint::new("internal", "internal_style", "abc", None, "module_name"),
538516
];
539517
let expected = vec![
540518
" LintId::of(&module_name::ABC),".to_string(),
541519
" LintId::of(&module_name::INTERNAL),".to_string(),
542520
" LintId::of(&module_name::SHOULD_ASSERT_EQ),".to_string(),
543521
];
544-
assert_eq!(expected, gen_lint_group_list(lints));
522+
assert_eq!(expected, gen_lint_group_list(lints.iter()));
545523
}

clippy_dev/src/update_lints.rs

Lines changed: 15 additions & 19 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.clone()),
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(&lint_list),
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(lint_list.clone()),
94+
|| gen_modules_list(usable_lints.iter()),
9595
)
9696
.changed;
9797

@@ -104,13 +104,9 @@ 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();
107+
let all_group_lints = usable_lints.iter().filter(|l| {
108+
l.group == "correctness" || l.group == "style" || l.group == "complexity" || l.group == "perf"
109+
});
114110

115111
gen_lint_group_list(all_group_lints)
116112
},
@@ -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.clone()),
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)