Skip to content

Commit 95c3875

Browse files
committed
Ensure that symbols list stays sorted
1 parent c7beba3 commit 95c3875

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

clippy_dev/src/fmt.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,31 @@ fn fmt_conf(check: bool) -> Result<(), Error> {
225225
Ok(())
226226
}
227227

228+
/// Format the symbols list
229+
fn fmt_syms(check: bool) -> Result<(), Error> {
230+
let path = "clippy_utils/src/sym.rs";
231+
let text = fs::read_to_string(path)?;
232+
233+
let (pre, conf) = text.split_once("generate! {\n").expect("can't find generate! call");
234+
let (conf, post) = conf.split_once("\n}\n").expect("can't find end of generate! call");
235+
let mut lines = conf
236+
.lines()
237+
.map(|line| {
238+
let line = line.trim();
239+
line.strip_suffix(',').unwrap_or(line).trim_end()
240+
})
241+
.collect::<Vec<_>>();
242+
lines.sort_by_cached_key(|a| a.to_uppercase());
243+
let new_text = format!("{pre}generate! {{\n {},\n}}\n{post}", lines.join(",\n "),);
244+
if text != new_text {
245+
if check {
246+
return Err(Error::CheckFailed);
247+
}
248+
fs::write(path, new_text.as_bytes())?;
249+
}
250+
Ok(())
251+
}
252+
228253
fn run_rustfmt(clippy: &ClippyInfo, update_mode: UpdateMode) {
229254
let mut rustfmt_path = String::from_utf8(run_with_output(
230255
"rustup which rustfmt",
@@ -337,7 +362,7 @@ pub fn run(clippy: &ClippyInfo, update_mode: UpdateMode) {
337362
}
338363
run_rustfmt(clippy, update_mode);
339364

340-
if let Err(e) = fmt_conf(update_mode.is_check()) {
365+
if let Err(e) = fmt_conf(update_mode.is_check()).and_then(|()| fmt_syms(update_mode.is_check())) {
341366
e.display();
342367
process::exit(1);
343368
}

clippy_utils/src/sym.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ macro_rules! generate {
3131

3232
// List of extra symbols to be included in Clippy (for example, as `sym::ambiguous_glob_reexports`).
3333
// An alternative content can be specified using a colon after the symbol name.
34+
//
35+
// `cargo dev fmt` ensures that the content of the `generate!()` macro call stays sorted.
3436
generate! {
3537
abs,
3638
align_of,

0 commit comments

Comments
 (0)