diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2025-05-18 18:20:53 +0200 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2025-05-19 22:47:57 +0200 |
| commit | 82bf659dc80a1ab4da1b473206131f4d70a41ea9 (patch) | |
| tree | a8badbd4d6f22a809921b5dcc8dd5e6c5e96227c | |
| parent | bb724f34215ca54c0a82deadf20342d881af99d3 (diff) | |
| download | rust-82bf659dc80a1ab4da1b473206131f4d70a41ea9.tar.gz rust-82bf659dc80a1ab4da1b473206131f4d70a41ea9.zip | |
Ensure that symbols list stays sorted
| -rw-r--r-- | clippy_dev/src/fmt.rs | 39 | ||||
| -rw-r--r-- | clippy_utils/src/sym.rs | 2 |
2 files changed, 39 insertions, 2 deletions
diff --git a/clippy_dev/src/fmt.rs b/clippy_dev/src/fmt.rs index e43b2d7a5b3..13d6b1285dc 100644 --- a/clippy_dev/src/fmt.rs +++ b/clippy_dev/src/fmt.rs @@ -1,6 +1,9 @@ -use crate::utils::{ClippyInfo, ErrAction, UpdateMode, panic_action, run_with_args_split, run_with_output}; +use crate::utils::{ + ClippyInfo, ErrAction, FileUpdater, UpdateMode, UpdateStatus, panic_action, run_with_args_split, run_with_output, +}; use itertools::Itertools; use rustc_lexer::{TokenKind, tokenize}; +use std::fmt::Write; use std::fs; use std::io::{self, Read}; use std::ops::ControlFlow; @@ -225,6 +228,38 @@ fn fmt_conf(check: bool) -> Result<(), Error> { Ok(()) } +/// Format the symbols list +fn fmt_syms(update_mode: UpdateMode) { + FileUpdater::default().update_file_checked( + "cargo dev fmt", + update_mode, + "clippy_utils/src/sym.rs", + &mut |_, text: &str, new_text: &mut String| { + let (pre, conf) = text.split_once("generate! {\n").expect("can't find generate! call"); + let (conf, post) = conf.split_once("\n}\n").expect("can't find end of generate! call"); + let mut lines = conf + .lines() + .map(|line| { + let line = line.trim(); + line.strip_suffix(',').unwrap_or(line).trim_end() + }) + .collect::<Vec<_>>(); + lines.sort_unstable(); + write!( + new_text, + "{pre}generate! {{\n {},\n}}\n{post}", + lines.join(",\n "), + ) + .unwrap(); + if text == new_text { + UpdateStatus::Unchanged + } else { + UpdateStatus::Changed + } + }, + ); +} + fn run_rustfmt(clippy: &ClippyInfo, update_mode: UpdateMode) { let mut rustfmt_path = String::from_utf8(run_with_output( "rustup which rustfmt", @@ -337,7 +372,7 @@ pub fn run(clippy: &ClippyInfo, update_mode: UpdateMode) { return; } run_rustfmt(clippy, update_mode); - + fmt_syms(update_mode); if let Err(e) = fmt_conf(update_mode.is_check()) { e.display(); process::exit(1); diff --git a/clippy_utils/src/sym.rs b/clippy_utils/src/sym.rs index 4e1e573ad90..a5f0e9562c9 100644 --- a/clippy_utils/src/sym.rs +++ b/clippy_utils/src/sym.rs @@ -31,6 +31,8 @@ macro_rules! generate { // List of extra symbols to be included in Clippy (for example, as `sym::ambiguous_glob_reexports`). // An alternative content can be specified using a colon after the symbol name. +// +// `cargo dev fmt` ensures that the content of the `generate!()` macro call stays sorted. generate! { AsyncReadExt, AsyncWriteExt, |
