|
1 |
| -use crate::utils::{ClippyInfo, ErrAction, UpdateMode, panic_action, run_with_args_split, run_with_output}; |
| 1 | +use crate::utils::{ |
| 2 | + ClippyInfo, ErrAction, FileUpdater, UpdateMode, UpdateStatus, panic_action, run_with_args_split, run_with_output, |
| 3 | +}; |
2 | 4 | use itertools::Itertools;
|
3 | 5 | use rustc_lexer::{TokenKind, tokenize};
|
| 6 | +use std::fmt::Write; |
4 | 7 | use std::fs;
|
5 | 8 | use std::io::{self, Read};
|
6 | 9 | use std::ops::ControlFlow;
|
@@ -225,6 +228,38 @@ fn fmt_conf(check: bool) -> Result<(), Error> {
|
225 | 228 | Ok(())
|
226 | 229 | }
|
227 | 230 |
|
| 231 | +/// Format the symbols list |
| 232 | +fn fmt_syms(update_mode: UpdateMode) { |
| 233 | + FileUpdater::default().update_file_checked( |
| 234 | + "cargo dev fmt", |
| 235 | + update_mode, |
| 236 | + "clippy_utils/src/sym.rs", |
| 237 | + &mut |_, text: &str, new_text: &mut String| { |
| 238 | + let (pre, conf) = text.split_once("generate! {\n").expect("can't find generate! call"); |
| 239 | + let (conf, post) = conf.split_once("\n}\n").expect("can't find end of generate! call"); |
| 240 | + let mut lines = conf |
| 241 | + .lines() |
| 242 | + .map(|line| { |
| 243 | + let line = line.trim(); |
| 244 | + line.strip_suffix(',').unwrap_or(line).trim_end() |
| 245 | + }) |
| 246 | + .collect::<Vec<_>>(); |
| 247 | + lines.sort_unstable(); |
| 248 | + write!( |
| 249 | + new_text, |
| 250 | + "{pre}generate! {{\n {},\n}}\n{post}", |
| 251 | + lines.join(",\n "), |
| 252 | + ) |
| 253 | + .unwrap(); |
| 254 | + if text == new_text { |
| 255 | + UpdateStatus::Unchanged |
| 256 | + } else { |
| 257 | + UpdateStatus::Changed |
| 258 | + } |
| 259 | + }, |
| 260 | + ); |
| 261 | +} |
| 262 | + |
228 | 263 | fn run_rustfmt(clippy: &ClippyInfo, update_mode: UpdateMode) {
|
229 | 264 | let mut rustfmt_path = String::from_utf8(run_with_output(
|
230 | 265 | "rustup which rustfmt",
|
@@ -337,7 +372,7 @@ pub fn run(clippy: &ClippyInfo, update_mode: UpdateMode) {
|
337 | 372 | return;
|
338 | 373 | }
|
339 | 374 | run_rustfmt(clippy, update_mode);
|
340 |
| - |
| 375 | + fmt_syms(update_mode); |
341 | 376 | if let Err(e) = fmt_conf(update_mode.is_check()) {
|
342 | 377 | e.display();
|
343 | 378 | process::exit(1);
|
|
0 commit comments