11use crate :: utils:: {
2- ClippyInfo , ErrAction , FileUpdater , UpdateMode , UpdateStatus , panic_action, run_with_args_split, run_with_output,
2+ ErrAction , FileUpdater , UpdateMode , UpdateStatus , panic_action, run_with_output, split_args_for_threads,
3+ walk_dir_no_dot_or_target,
34} ;
45use itertools:: Itertools ;
56use rustc_lexer:: { TokenKind , tokenize} ;
@@ -9,7 +10,6 @@ use std::io::{self, Read};
910use std:: ops:: ControlFlow ;
1011use std:: path:: PathBuf ;
1112use std:: process:: { self , Command , Stdio } ;
12- use walkdir:: WalkDir ;
1313
1414pub enum Error {
1515 Io ( io:: Error ) ,
@@ -260,50 +260,27 @@ fn fmt_syms(update_mode: UpdateMode) {
260260 ) ;
261261}
262262
263- fn run_rustfmt ( clippy : & ClippyInfo , update_mode : UpdateMode ) {
263+ fn run_rustfmt ( update_mode : UpdateMode ) {
264264 let mut rustfmt_path = String :: from_utf8 ( run_with_output (
265265 "rustup which rustfmt" ,
266266 Command :: new ( "rustup" ) . args ( [ "which" , "rustfmt" ] ) ,
267267 ) )
268268 . expect ( "invalid rustfmt path" ) ;
269269 rustfmt_path. truncate ( rustfmt_path. trim_end ( ) . len ( ) ) ;
270270
271- let mut cargo_path = String :: from_utf8 ( run_with_output (
272- "rustup which cargo" ,
273- Command :: new ( "rustup" ) . args ( [ "which" , "cargo" ] ) ,
274- ) )
275- . expect ( "invalid cargo path" ) ;
276- cargo_path. truncate ( cargo_path. trim_end ( ) . len ( ) ) ;
277-
278- // Start all format jobs first before waiting on the results.
279- let mut children = Vec :: with_capacity ( 16 ) ;
280- for & path in & [
281- "." ,
282- "clippy_config" ,
283- "clippy_dev" ,
284- "clippy_lints" ,
285- "clippy_lints_internal" ,
286- "clippy_utils" ,
287- "rustc_tools_util" ,
288- "lintcheck" ,
289- ] {
290- let mut cmd = Command :: new ( & cargo_path) ;
291- cmd. current_dir ( clippy. path . join ( path) )
292- . args ( [ "fmt" ] )
293- . env ( "RUSTFMT" , & rustfmt_path)
294- . stdout ( Stdio :: null ( ) )
295- . stdin ( Stdio :: null ( ) )
296- . stderr ( Stdio :: piped ( ) ) ;
297- if update_mode. is_check ( ) {
298- cmd. arg ( "--check" ) ;
299- }
300- match cmd. spawn ( ) {
301- Ok ( x) => children. push ( ( "cargo fmt" , x) ) ,
302- Err ( ref e) => panic_action ( & e, ErrAction :: Run , "cargo fmt" . as_ref ( ) ) ,
303- }
304- }
271+ let args: Vec < _ > = walk_dir_no_dot_or_target ( )
272+ . filter_map ( |e| {
273+ let e = e. expect ( "error reading `.`" ) ;
274+ e. path ( )
275+ . as_os_str ( )
276+ . as_encoded_bytes ( )
277+ . ends_with ( b".rs" )
278+ . then ( || e. into_path ( ) . into_os_string ( ) )
279+ } )
280+ . collect ( ) ;
305281
306- run_with_args_split (
282+ let mut children: Vec < _ > = split_args_for_threads (
283+ 32 ,
307284 || {
308285 let mut cmd = Command :: new ( & rustfmt_path) ;
309286 if update_mode. is_check ( ) {
@@ -312,27 +289,18 @@ fn run_rustfmt(clippy: &ClippyInfo, update_mode: UpdateMode) {
312289 cmd. stdout ( Stdio :: null ( ) )
313290 . stdin ( Stdio :: null ( ) )
314291 . stderr ( Stdio :: piped ( ) )
315- . args ( [ "--config " , "show_parse_errors=false " ] ) ;
292+ . args ( [ "--unstable-features " , "--skip-children " ] ) ;
316293 cmd
317294 } ,
318- |cmd| match cmd. spawn ( ) {
319- Ok ( x) => children. push ( ( "rustfmt" , x) ) ,
320- Err ( ref e) => panic_action ( & e, ErrAction :: Run , "rustfmt" . as_ref ( ) ) ,
321- } ,
322- WalkDir :: new ( "tests" )
323- . into_iter ( )
324- . filter_entry ( |p| p. path ( ) . file_name ( ) . is_none_or ( |x| x != "skip_rustfmt" ) )
325- . filter_map ( |e| {
326- let e = e. expect ( "error reading `tests`" ) ;
327- e. path ( )
328- . as_os_str ( )
329- . as_encoded_bytes ( )
330- . ends_with ( b".rs" )
331- . then ( || e. into_path ( ) . into_os_string ( ) )
332- } ) ,
333- ) ;
295+ args. iter ( ) ,
296+ )
297+ . map ( |mut cmd| match cmd. spawn ( ) {
298+ Ok ( x) => x,
299+ Err ( ref e) => panic_action ( & e, ErrAction :: Run , "rustfmt" . as_ref ( ) ) ,
300+ } )
301+ . collect ( ) ;
334302
335- for ( name , child) in & mut children {
303+ for child in & mut children {
336304 match child. wait ( ) {
337305 Ok ( status) => match ( update_mode, status. exit_ok ( ) ) {
338306 ( UpdateMode :: Check | UpdateMode :: Change , Ok ( ( ) ) ) => { } ,
@@ -353,25 +321,17 @@ fn run_rustfmt(clippy: &ClippyInfo, update_mode: UpdateMode) {
353321 {
354322 eprintln ! ( "{s}" ) ;
355323 }
356- panic_action ( & e, ErrAction :: Run , name . as_ref ( ) ) ;
324+ panic_action ( & e, ErrAction :: Run , "rustfmt" . as_ref ( ) ) ;
357325 } ,
358326 } ,
359- Err ( ref e) => panic_action ( e, ErrAction :: Run , name . as_ref ( ) ) ,
327+ Err ( ref e) => panic_action ( e, ErrAction :: Run , "rustfmt" . as_ref ( ) ) ,
360328 }
361329 }
362330}
363331
364332// the "main" function of cargo dev fmt
365- pub fn run ( clippy : & ClippyInfo , update_mode : UpdateMode ) {
366- if clippy. has_intellij_hook {
367- eprintln ! (
368- "error: a local rustc repo is enabled as path dependency via `cargo dev setup intellij`.\n \
369- Not formatting because that would format the local repo as well!\n \
370- Please revert the changes to `Cargo.toml`s with `cargo dev remove intellij`."
371- ) ;
372- return ;
373- }
374- run_rustfmt ( clippy, update_mode) ;
333+ pub fn run ( update_mode : UpdateMode ) {
334+ run_rustfmt ( update_mode) ;
375335 fmt_syms ( update_mode) ;
376336 if let Err ( e) = fmt_conf ( update_mode. is_check ( ) ) {
377337 e. display ( ) ;
0 commit comments