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,21 @@ 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
+ std:: fs:: create_dir ( "target/lintcheck/" ) . unwrap_or_else ( |err| {
119
+ if err. kind ( ) != ErrorKind :: AlreadyExists {
120
+ panic ! ( "cannot create lintcheck target dir" ) ;
121
+ }
122
+ } ) ;
123
+ std:: fs:: create_dir ( & krate_download_dir) . unwrap_or_else ( |err| {
124
+ if err. kind ( ) != ErrorKind :: AlreadyExists {
125
+ panic ! ( "cannot create crate download dir" ) ;
126
+ }
127
+ } ) ;
128
+ std:: fs:: create_dir ( & extract_dir) . unwrap_or_else ( |err| {
129
+ if err. kind ( ) != ErrorKind :: AlreadyExists {
130
+ panic ! ( "cannot create crate extraction dir" ) ;
131
+ }
132
+ } ) ;
122
133
123
134
let krate_file_path = krate_download_dir. join ( format ! ( "{}-{}.crate.tar.gz" , name, version) ) ;
124
135
// don't download/extract if we already have done so
@@ -198,18 +209,18 @@ impl CrateSource {
198
209
// the source path of the crate we copied, ${copy_dest}/crate_name
199
210
let crate_root = copy_dest. join ( name) ; // .../crates/local_crate
200
211
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 {
212
+ if crate_root. exists ( ) {
208
213
println ! (
209
214
"Not copying {} to {}, destination already exists" ,
210
215
path. display( ) ,
211
216
crate_root. display( )
212
217
) ;
218
+ } else {
219
+ println ! ( "Copying {} to {}" , path. display( ) , copy_dest. display( ) ) ;
220
+
221
+ dir:: copy ( path, & copy_dest, & dir:: CopyOptions :: new ( ) ) . unwrap_or_else ( |_| {
222
+ panic ! ( "Failed to copy from {}, to {}" , path. display( ) , crate_root. display( ) )
223
+ } ) ;
213
224
}
214
225
215
226
Crate {
@@ -236,8 +247,8 @@ impl Crate {
236
247
// advance the atomic index by one
237
248
let index = target_dir_index. fetch_add ( 1 , Ordering :: SeqCst ) ;
238
249
// "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 ;
250
+ let thread_index = index % thread_limit;
251
+ let perc = ( index * 100 ) / total_crates_to_lint;
241
252
242
253
if thread_limit == 1 {
243
254
println ! (
@@ -247,7 +258,7 @@ impl Crate {
247
258
} else {
248
259
println ! (
249
260
"{}/{} {}% Linting {} {} in target dir {:?}" ,
250
- index, total_crates_to_lint, perc, & self . name, & self . version, target_dir_index
261
+ index, total_crates_to_lint, perc, & self . name, & self . version, thread_index
251
262
) ;
252
263
}
253
264
@@ -269,7 +280,7 @@ impl Crate {
269
280
// use the looping index to create individual target dirs
270
281
. env (
271
282
"CARGO_TARGET_DIR" ,
272
- shared_target_dir. join ( format ! ( "_{:?}" , target_dir_index ) ) ,
283
+ shared_target_dir. join ( format ! ( "_{:?}" , thread_index ) ) ,
273
284
)
274
285
// lint warnings will look like this:
275
286
// src/cargo/ops/cargo_compile.rs:127:35: warning: usage of `FromIterator::from_iter`
@@ -529,6 +540,10 @@ fn lintcheck_needs_rerun(lintcheck_logs_path: &Path) -> bool {
529
540
}
530
541
531
542
/// lintchecks `main()` function
543
+ ///
544
+ /// # Panics
545
+ ///
546
+ /// This function panics if the clippy binaries don't exist.
532
547
pub fn run ( clap_config : & ArgMatches ) {
533
548
let config = LintcheckConfig :: from_clap ( clap_config) ;
534
549
@@ -579,9 +594,9 @@ pub fn run(clap_config: &ArgMatches) {
579
594
// if we don't have the specified crate in the .toml, throw an error
580
595
if !crates. iter ( ) . any ( |krate| {
581
596
let name = match krate {
582
- CrateSource :: CratesIo { name, .. } => name,
583
- CrateSource :: Git { name , .. } => name,
584
- CrateSource :: Path { name , .. } => name ,
597
+ CrateSource :: CratesIo { name, .. } | CrateSource :: Git { name, .. } | CrateSource :: Path { name , .. } => {
598
+ name
599
+ } ,
585
600
} ;
586
601
name == only_one_crate
587
602
} ) {
@@ -597,8 +612,7 @@ pub fn run(clap_config: &ArgMatches) {
597
612
. into_iter ( )
598
613
. map ( |krate| krate. download_and_extract ( ) )
599
614
. filter ( |krate| krate. name == only_one_crate)
600
- . map ( |krate| krate. run_clippy_lints ( & cargo_clippy_path, & AtomicUsize :: new ( 0 ) , 1 , 1 ) )
601
- . flatten ( )
615
+ . flat_map ( |krate| krate. run_clippy_lints ( & cargo_clippy_path, & AtomicUsize :: new ( 0 ) , 1 , 1 ) )
602
616
. collect ( )
603
617
} else {
604
618
if config. max_jobs > 1 {
@@ -621,17 +635,15 @@ pub fn run(clap_config: &ArgMatches) {
621
635
crates
622
636
. into_par_iter ( )
623
637
. map ( |krate| krate. download_and_extract ( ) )
624
- . map ( |krate| krate. run_clippy_lints ( & cargo_clippy_path, & counter, num_cpus, num_crates) )
625
- . flatten ( )
638
+ . flat_map ( |krate| krate. run_clippy_lints ( & cargo_clippy_path, & counter, num_cpus, num_crates) )
626
639
. collect ( )
627
640
} else {
628
641
// run sequential
629
642
let num_crates = crates. len ( ) ;
630
643
crates
631
644
. into_iter ( )
632
645
. map ( |krate| krate. download_and_extract ( ) )
633
- . map ( |krate| krate. run_clippy_lints ( & cargo_clippy_path, & counter, 1 , num_crates) )
634
- . flatten ( )
646
+ . flat_map ( |krate| krate. run_clippy_lints ( & cargo_clippy_path, & counter, 1 , num_crates) )
635
647
. collect ( )
636
648
}
637
649
} ;
@@ -646,7 +658,7 @@ pub fn run(clap_config: &ArgMatches) {
646
658
. map ( |w| ( & w. crate_name , & w. message ) )
647
659
. collect ( ) ;
648
660
649
- let mut all_msgs: Vec < String > = clippy_warnings. iter ( ) . map ( |warning| warning . to_string ( ) ) . collect ( ) ;
661
+ let mut all_msgs: Vec < String > = clippy_warnings. iter ( ) . map ( ToString :: to_string) . collect ( ) ;
650
662
all_msgs. sort ( ) ;
651
663
all_msgs. push ( "\n \n \n \n Stats:\n " . into ( ) ) ;
652
664
all_msgs. push ( stats_formatted) ;
@@ -673,13 +685,13 @@ fn read_stats_from_file(file_path: &Path) -> HashMap<String, usize> {
673
685
} ,
674
686
} ;
675
687
676
- let lines: Vec < String > = file_content. lines ( ) . map ( |l| l . to_string ( ) ) . collect ( ) ;
688
+ let lines: Vec < String > = file_content. lines ( ) . map ( ToString :: to_string) . collect ( ) ;
677
689
678
690
// search for the beginning "Stats:" and the end "ICEs:" of the section we want
679
691
let start = lines. iter ( ) . position ( |line| line == "Stats:" ) . unwrap ( ) ;
680
692
let end = lines. iter ( ) . position ( |line| line == "ICEs:" ) . unwrap ( ) ;
681
693
682
- let stats_lines = & lines[ start + 1 ..= end - 1 ] ;
694
+ let stats_lines = & lines[ start + 1 ..end] ;
683
695
684
696
stats_lines
685
697
. iter ( )
0 commit comments