Skip to content

Commit 5de0190

Browse files
committed
Move update_lints logic to its own module
1 parent deb1979 commit 5de0190

File tree

2 files changed

+168
-175
lines changed

2 files changed

+168
-175
lines changed

clippy_dev/src/main.rs

Lines changed: 5 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
22

33
use clap::{App, Arg, SubCommand};
4-
use clippy_dev::{
5-
gather_all, gen_changelog_lint_list, gen_deprecated, gen_lint_group_list, gen_modules_list, gen_register_lint_list,
6-
replace_region_in_file, Lint, DOCS_LINK,
7-
};
8-
use std::path::Path;
9-
10-
use clippy_dev::{fmt, new_lint, stderr_length_check};
11-
12-
#[derive(Clone, Copy, PartialEq)]
13-
enum UpdateMode {
14-
Check,
15-
Change,
16-
}
4+
use clippy_dev::{fmt, new_lint, stderr_length_check, update_lints};
175

186
fn main() {
197
let matches = App::new("Clippy developer tooling")
@@ -107,11 +95,11 @@ fn main() {
10795
},
10896
("update_lints", Some(matches)) => {
10997
if matches.is_present("print-only") {
110-
print_lints();
98+
update_lints::print_lints();
11199
} else if matches.is_present("check") {
112-
update_lints(UpdateMode::Check);
100+
update_lints::run(update_lints::UpdateMode::Check);
113101
} else {
114-
update_lints(UpdateMode::Change);
102+
update_lints::run(update_lints::UpdateMode::Change);
115103
}
116104
},
117105
("new_lint", Some(matches)) => {
@@ -120,7 +108,7 @@ fn main() {
120108
matches.value_of("name"),
121109
matches.value_of("category"),
122110
) {
123-
Ok(_) => update_lints(UpdateMode::Change),
111+
Ok(_) => update_lints::run(update_lints::UpdateMode::Change),
124112
Err(e) => eprintln!("Unable to create lint: {}", e),
125113
}
126114
},
@@ -130,161 +118,3 @@ fn main() {
130118
_ => {},
131119
}
132120
}
133-
134-
fn print_lints() {
135-
let lint_list = gather_all();
136-
let usable_lints: Vec<Lint> = Lint::usable_lints(lint_list).collect();
137-
let usable_lint_count = usable_lints.len();
138-
let grouped_by_lint_group = Lint::by_lint_group(usable_lints.into_iter());
139-
140-
for (lint_group, mut lints) in grouped_by_lint_group {
141-
if lint_group == "Deprecated" {
142-
continue;
143-
}
144-
println!("\n## {}", lint_group);
145-
146-
lints.sort_by_key(|l| l.name.clone());
147-
148-
for lint in lints {
149-
println!(
150-
"* [{}]({}#{}) ({})",
151-
lint.name,
152-
clippy_dev::DOCS_LINK,
153-
lint.name,
154-
lint.desc
155-
);
156-
}
157-
}
158-
159-
println!("there are {} lints", usable_lint_count);
160-
}
161-
162-
#[allow(clippy::too_many_lines)]
163-
fn update_lints(update_mode: UpdateMode) {
164-
let lint_list: Vec<Lint> = gather_all().collect();
165-
166-
let internal_lints = Lint::internal_lints(lint_list.clone().into_iter());
167-
168-
let usable_lints: Vec<Lint> = Lint::usable_lints(lint_list.clone().into_iter()).collect();
169-
let usable_lint_count = usable_lints.len();
170-
171-
let mut sorted_usable_lints = usable_lints.clone();
172-
sorted_usable_lints.sort_by_key(|lint| lint.name.clone());
173-
174-
let mut file_change = replace_region_in_file(
175-
Path::new("src/lintlist/mod.rs"),
176-
"begin lint list",
177-
"end lint list",
178-
false,
179-
update_mode == UpdateMode::Change,
180-
|| {
181-
format!(
182-
"pub const ALL_LINTS: [Lint; {}] = {:#?};",
183-
sorted_usable_lints.len(),
184-
sorted_usable_lints
185-
)
186-
.lines()
187-
.map(ToString::to_string)
188-
.collect::<Vec<_>>()
189-
},
190-
)
191-
.changed;
192-
193-
file_change |= replace_region_in_file(
194-
Path::new("README.md"),
195-
&format!(r#"\[There are \d+ lints included in this crate!\]\({}\)"#, DOCS_LINK),
196-
"",
197-
true,
198-
update_mode == UpdateMode::Change,
199-
|| {
200-
vec![format!(
201-
"[There are {} lints included in this crate!]({})",
202-
usable_lint_count, DOCS_LINK
203-
)]
204-
},
205-
)
206-
.changed;
207-
208-
file_change |= replace_region_in_file(
209-
Path::new("CHANGELOG.md"),
210-
"<!-- begin autogenerated links to lint list -->",
211-
"<!-- end autogenerated links to lint list -->",
212-
false,
213-
update_mode == UpdateMode::Change,
214-
|| gen_changelog_lint_list(lint_list.clone()),
215-
)
216-
.changed;
217-
218-
file_change |= replace_region_in_file(
219-
Path::new("clippy_lints/src/lib.rs"),
220-
"begin deprecated lints",
221-
"end deprecated lints",
222-
false,
223-
update_mode == UpdateMode::Change,
224-
|| gen_deprecated(&lint_list),
225-
)
226-
.changed;
227-
228-
file_change |= replace_region_in_file(
229-
Path::new("clippy_lints/src/lib.rs"),
230-
"begin register lints",
231-
"end register lints",
232-
false,
233-
update_mode == UpdateMode::Change,
234-
|| gen_register_lint_list(&lint_list),
235-
)
236-
.changed;
237-
238-
file_change |= replace_region_in_file(
239-
Path::new("clippy_lints/src/lib.rs"),
240-
"begin lints modules",
241-
"end lints modules",
242-
false,
243-
update_mode == UpdateMode::Change,
244-
|| gen_modules_list(lint_list.clone()),
245-
)
246-
.changed;
247-
248-
// Generate lists of lints in the clippy::all lint group
249-
file_change |= replace_region_in_file(
250-
Path::new("clippy_lints/src/lib.rs"),
251-
r#"store.register_group\(true, "clippy::all""#,
252-
r#"\]\);"#,
253-
false,
254-
update_mode == UpdateMode::Change,
255-
|| {
256-
// clippy::all should only include the following lint groups:
257-
let all_group_lints = usable_lints
258-
.clone()
259-
.into_iter()
260-
.filter(|l| {
261-
l.group == "correctness" || l.group == "style" || l.group == "complexity" || l.group == "perf"
262-
})
263-
.collect();
264-
265-
gen_lint_group_list(all_group_lints)
266-
},
267-
)
268-
.changed;
269-
270-
// Generate the list of lints for all other lint groups
271-
for (lint_group, lints) in Lint::by_lint_group(usable_lints.into_iter().chain(internal_lints)) {
272-
file_change |= replace_region_in_file(
273-
Path::new("clippy_lints/src/lib.rs"),
274-
&format!("store.register_group\\(true, \"clippy::{}\"", lint_group),
275-
r#"\]\);"#,
276-
false,
277-
update_mode == UpdateMode::Change,
278-
|| gen_lint_group_list(lints.clone()),
279-
)
280-
.changed;
281-
}
282-
283-
if update_mode == UpdateMode::Check && file_change {
284-
println!(
285-
"Not all lints defined properly. \
286-
Please run `cargo dev update_lints` to make sure all lints are defined properly."
287-
);
288-
std::process::exit(1);
289-
}
290-
}

clippy_dev/src/update_lints.rs

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
use crate::{
2+
gather_all, gen_changelog_lint_list, gen_deprecated, gen_lint_group_list, gen_modules_list, gen_register_lint_list,
3+
replace_region_in_file, Lint, DOCS_LINK,
4+
};
5+
use std::path::Path;
6+
7+
#[derive(Clone, Copy, PartialEq)]
8+
pub enum UpdateMode {
9+
Check,
10+
Change,
11+
}
12+
13+
#[allow(clippy::too_many_lines)]
14+
pub fn run(update_mode: UpdateMode) {
15+
let lint_list: Vec<Lint> = gather_all().collect();
16+
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 = usable_lints.len();
21+
22+
let mut sorted_usable_lints = usable_lints.clone();
23+
sorted_usable_lints.sort_by_key(|lint| lint.name.clone());
24+
25+
let mut file_change = replace_region_in_file(
26+
Path::new("src/lintlist/mod.rs"),
27+
"begin lint list",
28+
"end lint list",
29+
false,
30+
update_mode == UpdateMode::Change,
31+
|| {
32+
format!(
33+
"pub const ALL_LINTS: [Lint; {}] = {:#?};",
34+
sorted_usable_lints.len(),
35+
sorted_usable_lints
36+
)
37+
.lines()
38+
.map(ToString::to_string)
39+
.collect::<Vec<_>>()
40+
},
41+
)
42+
.changed;
43+
44+
file_change |= replace_region_in_file(
45+
Path::new("README.md"),
46+
&format!(r#"\[There are \d+ lints included in this crate!\]\({}\)"#, DOCS_LINK),
47+
"",
48+
true,
49+
update_mode == UpdateMode::Change,
50+
|| {
51+
vec![format!(
52+
"[There are {} lints included in this crate!]({})",
53+
usable_lint_count, DOCS_LINK
54+
)]
55+
},
56+
)
57+
.changed;
58+
59+
file_change |= replace_region_in_file(
60+
Path::new("CHANGELOG.md"),
61+
"<!-- begin autogenerated links to lint list -->",
62+
"<!-- end autogenerated links to lint list -->",
63+
false,
64+
update_mode == UpdateMode::Change,
65+
|| gen_changelog_lint_list(lint_list.clone()),
66+
)
67+
.changed;
68+
69+
file_change |= replace_region_in_file(
70+
Path::new("clippy_lints/src/lib.rs"),
71+
"begin deprecated lints",
72+
"end deprecated lints",
73+
false,
74+
update_mode == UpdateMode::Change,
75+
|| gen_deprecated(&lint_list),
76+
)
77+
.changed;
78+
79+
file_change |= replace_region_in_file(
80+
Path::new("clippy_lints/src/lib.rs"),
81+
"begin register lints",
82+
"end register lints",
83+
false,
84+
update_mode == UpdateMode::Change,
85+
|| gen_register_lint_list(&lint_list),
86+
)
87+
.changed;
88+
89+
file_change |= replace_region_in_file(
90+
Path::new("clippy_lints/src/lib.rs"),
91+
"begin lints modules",
92+
"end lints modules",
93+
false,
94+
update_mode == UpdateMode::Change,
95+
|| gen_modules_list(lint_list.clone()),
96+
)
97+
.changed;
98+
99+
// Generate lists of lints in the clippy::all lint group
100+
file_change |= replace_region_in_file(
101+
Path::new("clippy_lints/src/lib.rs"),
102+
r#"store.register_group\(true, "clippy::all""#,
103+
r#"\]\);"#,
104+
false,
105+
update_mode == UpdateMode::Change,
106+
|| {
107+
// clippy::all should only include the following lint groups:
108+
let all_group_lints = usable_lints
109+
.clone()
110+
.into_iter()
111+
.filter(|l| {
112+
l.group == "correctness" || l.group == "style" || l.group == "complexity" || l.group == "perf"
113+
})
114+
.collect();
115+
116+
gen_lint_group_list(all_group_lints)
117+
},
118+
)
119+
.changed;
120+
121+
// Generate the list of lints for all other lint groups
122+
for (lint_group, lints) in Lint::by_lint_group(usable_lints.into_iter().chain(internal_lints)) {
123+
file_change |= replace_region_in_file(
124+
Path::new("clippy_lints/src/lib.rs"),
125+
&format!("store.register_group\\(true, \"clippy::{}\"", lint_group),
126+
r#"\]\);"#,
127+
false,
128+
update_mode == UpdateMode::Change,
129+
|| gen_lint_group_list(lints.clone()),
130+
)
131+
.changed;
132+
}
133+
134+
if update_mode == UpdateMode::Check && file_change {
135+
println!(
136+
"Not all lints defined properly. \
137+
Please run `cargo dev update_lints` to make sure all lints are defined properly."
138+
);
139+
std::process::exit(1);
140+
}
141+
}
142+
143+
pub fn print_lints() {
144+
let lint_list = gather_all();
145+
let usable_lints: Vec<Lint> = Lint::usable_lints(lint_list).collect();
146+
let usable_lint_count = usable_lints.len();
147+
let grouped_by_lint_group = Lint::by_lint_group(usable_lints.into_iter());
148+
149+
for (lint_group, mut lints) in grouped_by_lint_group {
150+
if lint_group == "Deprecated" {
151+
continue;
152+
}
153+
println!("\n## {}", lint_group);
154+
155+
lints.sort_by_key(|l| l.name.clone());
156+
157+
for lint in lints {
158+
println!("* [{}]({}#{}) ({})", lint.name, DOCS_LINK, lint.name, lint.desc);
159+
}
160+
}
161+
162+
println!("there are {} lints", usable_lint_count);
163+
}

0 commit comments

Comments
 (0)