6
6
7
7
#![ cfg( feature = "lintcheck" ) ]
8
8
#![ allow( clippy:: filter_map, clippy:: collapsible_else_if) ]
9
- #![ allow( clippy:: blocks_in_if_conditions) ] // FP on `if x.iter().any(|x| ...)`
10
9
11
10
use crate :: clippy_project_root;
12
11
13
- use std:: collections:: HashMap ;
14
12
use std:: process:: Command ;
15
13
use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
14
+ use std:: { collections:: HashMap , io:: ErrorKind } ;
16
15
use std:: {
17
16
env, fmt,
18
17
fs:: write,
@@ -116,9 +115,7 @@ impl CrateSource {
116
115
// url to download the crate from crates.io
117
116
let url = format ! ( "https://crates.io/api/v1/crates/{}/{}/download" , name, version) ;
118
117
println ! ( "Downloading and extracting {} {} from {}" , name, version, url) ;
119
- let _ = std:: fs:: create_dir ( "target/lintcheck/" ) ;
120
- let _ = std:: fs:: create_dir ( & krate_download_dir) ;
121
- let _ = std:: fs:: create_dir ( & extract_dir) ;
118
+ create_dirs ( & krate_download_dir, & extract_dir) ;
122
119
123
120
let krate_file_path = krate_download_dir. join ( format ! ( "{}-{}.crate.tar.gz" , name, version) ) ;
124
121
// don't download/extract if we already have done so
@@ -198,18 +195,18 @@ impl CrateSource {
198
195
// the source path of the crate we copied, ${copy_dest}/crate_name
199
196
let crate_root = copy_dest. join ( name) ; // .../crates/local_crate
200
197
201
- if !crate_root. exists ( ) {
202
- println ! ( "Copying {} to {}" , path. display( ) , copy_dest. display( ) ) ;
203
-
204
- dir:: copy ( path, & copy_dest, & dir:: CopyOptions :: new ( ) ) . unwrap_or_else ( |_| {
205
- panic ! ( "Failed to copy from {}, to {}" , path. display( ) , crate_root. display( ) )
206
- } ) ;
207
- } else {
198
+ if crate_root. exists ( ) {
208
199
println ! (
209
200
"Not copying {} to {}, destination already exists" ,
210
201
path. display( ) ,
211
202
crate_root. display( )
212
203
) ;
204
+ } else {
205
+ println ! ( "Copying {} to {}" , path. display( ) , copy_dest. display( ) ) ;
206
+
207
+ dir:: copy ( path, & copy_dest, & dir:: CopyOptions :: new ( ) ) . unwrap_or_else ( |_| {
208
+ panic ! ( "Failed to copy from {}, to {}" , path. display( ) , crate_root. display( ) )
209
+ } ) ;
213
210
}
214
211
215
212
Crate {
@@ -236,8 +233,8 @@ impl Crate {
236
233
// advance the atomic index by one
237
234
let index = target_dir_index. fetch_add ( 1 , Ordering :: SeqCst ) ;
238
235
// "loop" the index within 0..thread_limit
239
- let target_dir_index = index % thread_limit;
240
- let perc = ( ( index * 100 ) as f32 / total_crates_to_lint as f32 ) as u8 ;
236
+ let thread_index = index % thread_limit;
237
+ let perc = ( index * 100 ) / total_crates_to_lint;
241
238
242
239
if thread_limit == 1 {
243
240
println ! (
@@ -247,7 +244,7 @@ impl Crate {
247
244
} else {
248
245
println ! (
249
246
"{}/{} {}% Linting {} {} in target dir {:?}" ,
250
- index, total_crates_to_lint, perc, & self . name, & self . version, target_dir_index
247
+ index, total_crates_to_lint, perc, & self . name, & self . version, thread_index
251
248
) ;
252
249
}
253
250
@@ -269,7 +266,7 @@ impl Crate {
269
266
// use the looping index to create individual target dirs
270
267
. env (
271
268
"CARGO_TARGET_DIR" ,
272
- shared_target_dir. join ( format ! ( "_{:?}" , target_dir_index ) ) ,
269
+ shared_target_dir. join ( format ! ( "_{:?}" , thread_index ) ) ,
273
270
)
274
271
// lint warnings will look like this:
275
272
// src/cargo/ops/cargo_compile.rs:127:35: warning: usage of `FromIterator::from_iter`
@@ -529,6 +526,10 @@ fn lintcheck_needs_rerun(lintcheck_logs_path: &Path) -> bool {
529
526
}
530
527
531
528
/// lintchecks `main()` function
529
+ ///
530
+ /// # Panics
531
+ ///
532
+ /// This function panics if the clippy binaries don't exist.
532
533
pub fn run ( clap_config : & ArgMatches ) {
533
534
let config = LintcheckConfig :: from_clap ( clap_config) ;
534
535
@@ -579,9 +580,9 @@ pub fn run(clap_config: &ArgMatches) {
579
580
// if we don't have the specified crate in the .toml, throw an error
580
581
if !crates. iter ( ) . any ( |krate| {
581
582
let name = match krate {
582
- CrateSource :: CratesIo { name, .. } => name,
583
- CrateSource :: Git { name , .. } => name,
584
- CrateSource :: Path { name , .. } => name ,
583
+ CrateSource :: CratesIo { name, .. } | CrateSource :: Git { name, .. } | CrateSource :: Path { name , .. } => {
584
+ name
585
+ } ,
585
586
} ;
586
587
name == only_one_crate
587
588
} ) {
@@ -597,8 +598,7 @@ pub fn run(clap_config: &ArgMatches) {
597
598
. into_iter ( )
598
599
. map ( |krate| krate. download_and_extract ( ) )
599
600
. filter ( |krate| krate. name == only_one_crate)
600
- . map ( |krate| krate. run_clippy_lints ( & cargo_clippy_path, & AtomicUsize :: new ( 0 ) , 1 , 1 ) )
601
- . flatten ( )
601
+ . flat_map ( |krate| krate. run_clippy_lints ( & cargo_clippy_path, & AtomicUsize :: new ( 0 ) , 1 , 1 ) )
602
602
. collect ( )
603
603
} else {
604
604
if config. max_jobs > 1 {
@@ -621,17 +621,15 @@ pub fn run(clap_config: &ArgMatches) {
621
621
crates
622
622
. into_par_iter ( )
623
623
. map ( |krate| krate. download_and_extract ( ) )
624
- . map ( |krate| krate. run_clippy_lints ( & cargo_clippy_path, & counter, num_cpus, num_crates) )
625
- . flatten ( )
624
+ . flat_map ( |krate| krate. run_clippy_lints ( & cargo_clippy_path, & counter, num_cpus, num_crates) )
626
625
. collect ( )
627
626
} else {
628
627
// run sequential
629
628
let num_crates = crates. len ( ) ;
630
629
crates
631
630
. into_iter ( )
632
631
. map ( |krate| krate. download_and_extract ( ) )
633
- . map ( |krate| krate. run_clippy_lints ( & cargo_clippy_path, & counter, 1 , num_crates) )
634
- . flatten ( )
632
+ . flat_map ( |krate| krate. run_clippy_lints ( & cargo_clippy_path, & counter, 1 , num_crates) )
635
633
. collect ( )
636
634
}
637
635
} ;
@@ -646,7 +644,7 @@ pub fn run(clap_config: &ArgMatches) {
646
644
. map ( |w| ( & w. crate_name , & w. message ) )
647
645
. collect ( ) ;
648
646
649
- let mut all_msgs: Vec < String > = clippy_warnings. iter ( ) . map ( |warning| warning . to_string ( ) ) . collect ( ) ;
647
+ let mut all_msgs: Vec < String > = clippy_warnings. iter ( ) . map ( ToString :: to_string) . collect ( ) ;
650
648
all_msgs. sort ( ) ;
651
649
all_msgs. push ( "\n \n \n \n Stats:\n " . into ( ) ) ;
652
650
all_msgs. push ( stats_formatted) ;
@@ -673,13 +671,13 @@ fn read_stats_from_file(file_path: &Path) -> HashMap<String, usize> {
673
671
} ,
674
672
} ;
675
673
676
- let lines: Vec < String > = file_content. lines ( ) . map ( |l| l . to_string ( ) ) . collect ( ) ;
674
+ let lines: Vec < String > = file_content. lines ( ) . map ( ToString :: to_string) . collect ( ) ;
677
675
678
676
// search for the beginning "Stats:" and the end "ICEs:" of the section we want
679
677
let start = lines. iter ( ) . position ( |line| line == "Stats:" ) . unwrap ( ) ;
680
678
let end = lines. iter ( ) . position ( |line| line == "ICEs:" ) . unwrap ( ) ;
681
679
682
- let stats_lines = & lines[ start + 1 ..= end - 1 ] ;
680
+ let stats_lines = & lines[ start + 1 ..end] ;
683
681
684
682
stats_lines
685
683
. iter ( )
@@ -738,6 +736,29 @@ fn print_stats(old_stats: HashMap<String, usize>, new_stats: HashMap<&String, us
738
736
} ) ;
739
737
}
740
738
739
+ /// Create necessary directories to run the lintcheck tool.
740
+ ///
741
+ /// # Panics
742
+ ///
743
+ /// This function panics if creating one of the dirs fails.
744
+ fn create_dirs ( krate_download_dir : & Path , extract_dir : & Path ) {
745
+ std:: fs:: create_dir ( "target/lintcheck/" ) . unwrap_or_else ( |err| {
746
+ if err. kind ( ) != ErrorKind :: AlreadyExists {
747
+ panic ! ( "cannot create lintcheck target dir" ) ;
748
+ }
749
+ } ) ;
750
+ std:: fs:: create_dir ( & krate_download_dir) . unwrap_or_else ( |err| {
751
+ if err. kind ( ) != ErrorKind :: AlreadyExists {
752
+ panic ! ( "cannot create crate download dir" ) ;
753
+ }
754
+ } ) ;
755
+ std:: fs:: create_dir ( & extract_dir) . unwrap_or_else ( |err| {
756
+ if err. kind ( ) != ErrorKind :: AlreadyExists {
757
+ panic ! ( "cannot create crate extraction dir" ) ;
758
+ }
759
+ } ) ;
760
+ }
761
+
741
762
#[ test]
742
763
fn lintcheck_test ( ) {
743
764
let args = [
0 commit comments