Skip to content

Commit 50ea370

Browse files
author
Michael Wright
committed
Move code generated by update_lints to includes
1 parent 685b773 commit 50ea370

17 files changed

+1779
-1706
lines changed

clippy_dev/src/lib.rs

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,24 @@ impl Lint {
100100

101101
/// Generates the Vec items for `register_lint_group` calls in `clippy_lints/src/lib.rs`.
102102
#[must_use]
103-
pub fn gen_lint_group_list<'a>(lints: impl Iterator<Item = &'a Lint>) -> Vec<String> {
104-
lints
105-
.map(|l| format!(" LintId::of({}::{}),", l.module, l.name.to_uppercase()))
106-
.sorted()
107-
.collect::<Vec<String>>()
103+
pub fn gen_lint_group_list<'a>(group_name: &str, lints: impl Iterator<Item = &'a Lint>) -> Vec<String> {
104+
let header = format!(
105+
r#"store.register_group(true, "clippy::{0}", Some("clippy_{0}"), vec!["#,
106+
group_name
107+
);
108+
let footer = "])".to_string();
109+
110+
let mut result = vec![header];
111+
112+
result.extend(
113+
lints
114+
.map(|l| format!("LintId::of({}::{}),", l.module, l.name.to_uppercase()))
115+
.sorted(),
116+
);
117+
118+
result.push(footer);
119+
120+
result
108121
}
109122

110123
/// Generates the `pub mod module_name` list in `clippy_lints/src/lib.rs`.
@@ -130,21 +143,22 @@ pub fn gen_changelog_lint_list<'a>(lints: impl Iterator<Item = &'a Lint>) -> Vec
130143
/// Generates the `register_removed` code in `./clippy_lints/src/lib.rs`.
131144
#[must_use]
132145
pub fn gen_deprecated<'a>(lints: impl Iterator<Item = &'a Lint>) -> Vec<String> {
133-
lints
134-
.flat_map(|l| {
135-
l.deprecation
136-
.clone()
137-
.map(|depr_text| {
138-
vec![
139-
" store.register_removed(".to_string(),
140-
format!(" \"clippy::{}\",", l.name),
141-
format!(" \"{}\",", depr_text),
142-
" );".to_string(),
143-
]
144-
})
145-
.expect("only deprecated lints should be passed")
146-
})
147-
.collect::<Vec<String>>()
146+
let mut result = vec!["{".to_string()];
147+
result.extend(lints.flat_map(|l| {
148+
l.deprecation
149+
.clone()
150+
.map(|depr_text| {
151+
vec![
152+
" store.register_removed(".to_string(),
153+
format!(" \"clippy::{}\",", l.name),
154+
format!(" \"{}\",", depr_text),
155+
" );".to_string(),
156+
]
157+
})
158+
.expect("only deprecated lints should be passed")
159+
}));
160+
result.push("}".to_string());
161+
result
148162
}
149163

150164
#[must_use]
@@ -153,7 +167,7 @@ pub fn gen_register_lint_list<'a>(
153167
usable_lints: impl Iterator<Item = &'a Lint>,
154168
) -> Vec<String> {
155169
let header = " store.register_lints(&[".to_string();
156-
let footer = " ]);".to_string();
170+
let footer = " ])".to_string();
157171
let internal_lints = internal_lints
158172
.sorted_by_key(|l| format!(" {}::{},", l.module, l.name.to_uppercase()))
159173
.map(|l| {
@@ -511,6 +525,7 @@ fn test_gen_deprecated() {
511525
),
512526
];
513527
let expected: Vec<String> = vec![
528+
"{",
514529
" store.register_removed(",
515530
" \"clippy::should_assert_eq\",",
516531
" \"has been superseded by should_assert_eq2\",",
@@ -519,6 +534,7 @@ fn test_gen_deprecated() {
519534
" \"clippy::another_deprecated\",",
520535
" \"will be removed\",",
521536
" );",
537+
"}",
522538
]
523539
.into_iter()
524540
.map(String::from)
@@ -551,9 +567,11 @@ fn test_gen_lint_group_list() {
551567
Lint::new("internal", "internal_style", "abc", None, "module_name"),
552568
];
553569
let expected = vec![
554-
" LintId::of(module_name::ABC),".to_string(),
555-
" LintId::of(module_name::INTERNAL),".to_string(),
556-
" LintId::of(module_name::SHOULD_ASSERT_EQ),".to_string(),
570+
"store.register_group(true, \"clippy::group1\", Some(\"clippy_group1\"), vec![",
571+
"LintId::of(module_name::ABC),",
572+
"LintId::of(module_name::INTERNAL),",
573+
"LintId::of(module_name::SHOULD_ASSERT_EQ),",
574+
"])",
557575
];
558-
assert_eq!(expected, gen_lint_group_list(lints.iter()));
576+
assert_eq!(expected, gen_lint_group_list("group1", lints.iter()));
559577
}

clippy_dev/src/update_lints.rs

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{
22
gather_all, gen_changelog_lint_list, gen_deprecated, gen_lint_group_list, gen_modules_list, gen_register_lint_list,
33
replace_region_in_file, Lint, DOCS_LINK,
44
};
5+
use std::fs;
56
use std::path::Path;
67

78
#[derive(Clone, Copy, PartialEq)]
@@ -10,6 +11,15 @@ pub enum UpdateMode {
1011
Change,
1112
}
1213

14+
/// Runs the `update_lints` command.
15+
///
16+
/// This updates various generated values from the lint source code.
17+
///
18+
/// `update_mode` indicates if the files should be updated or if updates should be checked for.
19+
///
20+
/// # Panics
21+
///
22+
/// Panics if a file path could not read from or then written to
1323
#[allow(clippy::too_many_lines)]
1424
pub fn run(update_mode: UpdateMode) {
1525
let lint_list: Vec<Lint> = gather_all().collect();
@@ -52,76 +62,37 @@ pub fn run(update_mode: UpdateMode) {
5262
)
5363
.changed;
5464

55-
file_change |= replace_region_in_file(
56-
Path::new("clippy_lints/src/lib.rs"),
57-
"begin deprecated lints",
58-
"end deprecated lints",
59-
false,
60-
update_mode == UpdateMode::Change,
61-
|| gen_deprecated(deprecated_lints.iter()),
62-
)
63-
.changed;
64-
65-
file_change |= replace_region_in_file(
66-
Path::new("clippy_lints/src/lib.rs"),
67-
"begin register lints",
68-
"end register lints",
69-
false,
70-
update_mode == UpdateMode::Change,
71-
|| gen_register_lint_list(internal_lints.iter(), usable_lints.iter()),
72-
)
73-
.changed;
74-
75-
file_change |= replace_region_in_file(
76-
Path::new("clippy_lints/src/lib.rs"),
77-
"begin lints modules",
78-
"end lints modules",
79-
false,
80-
update_mode == UpdateMode::Change,
81-
|| gen_modules_list(usable_lints.iter()),
82-
)
83-
.changed;
65+
if file_change && update_mode == UpdateMode::Check {
66+
exit_with_failure();
67+
}
8468

85-
// Generate lists of lints in the clippy::all lint group
86-
file_change |= replace_region_in_file(
87-
Path::new("clippy_lints/src/lib.rs"),
88-
r#"store.register_group\(true, "clippy::all""#,
89-
r#"\]\);"#,
90-
false,
91-
update_mode == UpdateMode::Change,
92-
|| {
93-
// clippy::all should only include the following lint groups:
69+
for (name, lines) in [
70+
("mods", gen_modules_list(usable_lints.iter())),
71+
("deprecated", gen_deprecated(deprecated_lints.iter())),
72+
(
73+
"register_lints",
74+
gen_register_lint_list(internal_lints.iter(), usable_lints.iter()),
75+
),
76+
("register_all", {
9477
let all_group_lints = usable_lints.iter().filter(|l| {
9578
matches!(
9679
&*l.group,
9780
"correctness" | "suspicious" | "style" | "complexity" | "perf"
9881
)
9982
});
10083

101-
gen_lint_group_list(all_group_lints)
102-
},
103-
)
104-
.changed;
105-
106-
// Generate the list of lints for all other lint groups
107-
for (lint_group, lints) in Lint::by_lint_group(usable_lints.into_iter().chain(internal_lints)) {
108-
file_change |= replace_region_in_file(
109-
Path::new("clippy_lints/src/lib.rs"),
110-
&format!("store.register_group\\(true, \"clippy::{}\"", lint_group),
111-
r#"\]\);"#,
112-
false,
113-
update_mode == UpdateMode::Change,
114-
|| gen_lint_group_list(lints.iter()),
115-
)
116-
.changed;
84+
gen_lint_group_list("all", all_group_lints)
85+
}),
86+
] {
87+
process_file(&format!("clippy_lints/src/lib.{}.rs", name), update_mode, &lines[..]);
11788
}
11889

119-
if update_mode == UpdateMode::Check && file_change {
120-
println!(
121-
"Not all lints defined properly. \
122-
Please run `cargo dev update_lints` to make sure all lints are defined properly."
90+
for (lint_group, lints) in Lint::by_lint_group(usable_lints.into_iter().chain(internal_lints)) {
91+
process_file(
92+
&format!("clippy_lints/src/lib.register_{}.rs", lint_group),
93+
update_mode,
94+
&gen_lint_group_list(&lints.get(0).expect("group non-empty").group, lints.iter())[..],
12395
);
124-
std::process::exit(1);
12596
}
12697
}
12798

@@ -150,3 +121,30 @@ pub fn print_lints() {
150121
fn round_to_fifty(count: usize) -> usize {
151122
count / 50 * 50
152123
}
124+
125+
fn process_file(path: impl AsRef<Path>, update_mode: UpdateMode, new_lines: &[String]) {
126+
let mut new_content = "// This file was generated by `cargo dev update_lints`.\n\
127+
// Use that command to update this file and do not edit by hand.\n\
128+
// Manual edits will be overwritten.\n\n"
129+
.to_string();
130+
new_content.push_str(&new_lines.join("\n"));
131+
132+
if update_mode == UpdateMode::Check {
133+
let old_content =
134+
fs::read_to_string(&path).unwrap_or_else(|e| panic!("Cannot read from {}: {}", path.as_ref().display(), e));
135+
if new_content != old_content {
136+
exit_with_failure();
137+
}
138+
} else {
139+
fs::write(&path, new_content.as_bytes())
140+
.unwrap_or_else(|e| panic!("Cannot write to {}: {}", path.as_ref().display(), e));
141+
}
142+
}
143+
144+
fn exit_with_failure() {
145+
println!(
146+
"Not all lints defined properly. \
147+
Please run `cargo dev update_lints` to make sure all lints are defined properly."
148+
);
149+
std::process::exit(1);
150+
}

clippy_lints/src/lib.deprecated.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// This file was generated by `cargo dev update_lints`.
2+
// Use that command to update this file and do not edit by hand.
3+
// Manual edits will be overwritten.
4+
5+
{
6+
store.register_removed(
7+
"clippy::should_assert_eq",
8+
"`assert!()` will be more flexible with RFC 2011",
9+
);
10+
store.register_removed(
11+
"clippy::extend_from_slice",
12+
"`.extend_from_slice(_)` is a faster way to extend a Vec by a slice",
13+
);
14+
store.register_removed(
15+
"clippy::range_step_by_zero",
16+
"`iterator.step_by(0)` panics nowadays",
17+
);
18+
store.register_removed(
19+
"clippy::unstable_as_slice",
20+
"`Vec::as_slice` has been stabilized in 1.7",
21+
);
22+
store.register_removed(
23+
"clippy::unstable_as_mut_slice",
24+
"`Vec::as_mut_slice` has been stabilized in 1.7",
25+
);
26+
store.register_removed(
27+
"clippy::misaligned_transmute",
28+
"this lint has been split into cast_ptr_alignment and transmute_ptr_to_ptr",
29+
);
30+
store.register_removed(
31+
"clippy::assign_ops",
32+
"using compound assignment operators (e.g., `+=`) is harmless",
33+
);
34+
store.register_removed(
35+
"clippy::if_let_redundant_pattern_matching",
36+
"this lint has been changed to redundant_pattern_matching",
37+
);
38+
store.register_removed(
39+
"clippy::unsafe_vector_initialization",
40+
"the replacement suggested by this lint had substantially different behavior",
41+
);
42+
store.register_removed(
43+
"clippy::unused_collect",
44+
"`collect` has been marked as #[must_use] in rustc and that covers all cases of this lint",
45+
);
46+
store.register_removed(
47+
"clippy::replace_consts",
48+
"associated-constants `MIN`/`MAX` of integers are preferred to `{min,max}_value()` and module constants",
49+
);
50+
store.register_removed(
51+
"clippy::regex_macro",
52+
"the regex! macro has been removed from the regex crate in 2018",
53+
);
54+
store.register_removed(
55+
"clippy::find_map",
56+
"this lint has been replaced by `manual_find_map`, a more specific lint",
57+
);
58+
store.register_removed(
59+
"clippy::filter_map",
60+
"this lint has been replaced by `manual_filter_map`, a more specific lint",
61+
);
62+
store.register_removed(
63+
"clippy::pub_enum_variant_names",
64+
"set the `avoid-breaking-exported-api` config option to `false` to enable the `enum_variant_names` lint for public items",
65+
);
66+
store.register_removed(
67+
"clippy::wrong_pub_self_convention",
68+
"set the `avoid-breaking-exported-api` config option to `false` to enable the `wrong_self_convention` lint for public items",
69+
);
70+
}

0 commit comments

Comments
 (0)