@@ -287,6 +287,61 @@ impl Crate {
287
287
}
288
288
}
289
289
290
+ #[ derive( Debug ) ]
291
+ struct LintcheckConfig {
292
+ // max number of jobs to spawn (default 1)
293
+ max_jobs : usize ,
294
+ // we read the sources to check from here
295
+ sources_toml_path : PathBuf ,
296
+ // we save the clippy lint results here
297
+ lintcheck_results_path : PathBuf ,
298
+ }
299
+
300
+ impl LintcheckConfig {
301
+ fn from_clap ( clap_config : & ArgMatches ) -> Self {
302
+ // first, check if we got anything passed via the LINTCHECK_TOML env var,
303
+ // if not, ask clap if we got any value for --crates-toml <foo>
304
+ // if not, use the default "clippy_dev/lintcheck_crates.toml"
305
+ let sources_toml = env:: var ( "LINTCHECK_TOML" ) . unwrap_or (
306
+ clap_config
307
+ . value_of ( "crates-toml" )
308
+ . clone ( )
309
+ . unwrap_or ( "clippy_dev/lintcheck_crates.toml" )
310
+ . to_string ( ) ,
311
+ ) ;
312
+
313
+ let sources_toml_path = PathBuf :: from ( sources_toml) ;
314
+
315
+ // for the path where we save the lint results, get the filename without extenstion ( so for
316
+ // wasd.toml, use "wasd"....)
317
+ let filename: PathBuf = sources_toml_path. file_stem ( ) . unwrap ( ) . into ( ) ;
318
+ let lintcheck_results_path = PathBuf :: from ( format ! ( "lintcheck-logs/{}_logs.txt" , filename. display( ) ) ) ;
319
+
320
+ let max_jobs = match clap_config. value_of ( "threads" ) {
321
+ Some ( threads) => {
322
+ let threads: usize = threads
323
+ . parse ( )
324
+ . expect ( & format ! ( "Failed to parse '{}' to a digit" , threads) ) ;
325
+ if threads == 0 {
326
+ // automatic choice
327
+ // Rayon seems to return thread count so half that for core count
328
+ ( rayon:: current_num_threads ( ) / 2 ) as usize
329
+ } else {
330
+ threads
331
+ }
332
+ } ,
333
+ // no -j passed, use a single thread
334
+ None => 1 ,
335
+ } ;
336
+
337
+ LintcheckConfig {
338
+ max_jobs,
339
+ sources_toml_path,
340
+ lintcheck_results_path,
341
+ }
342
+ }
343
+ }
344
+
290
345
/// takes a single json-formatted clippy warnings and returns true (we are interested in that line)
291
346
/// or false (we aren't)
292
347
fn filter_clippy_warnings ( line : & str ) -> bool {
@@ -310,19 +365,6 @@ fn filter_clippy_warnings(line: &str) -> bool {
310
365
false
311
366
}
312
367
313
- /// get the path to lintchecks crate sources .toml file, check LINTCHECK_TOML first but if it's
314
- /// empty use the default path
315
- fn lintcheck_config_toml ( toml_path : Option < & str > ) -> PathBuf {
316
- PathBuf :: from (
317
- env:: var ( "LINTCHECK_TOML" ) . unwrap_or (
318
- toml_path
319
- . clone ( )
320
- . unwrap_or ( "clippy_dev/lintcheck_crates.toml" )
321
- . to_string ( ) ,
322
- ) ,
323
- )
324
- }
325
-
326
368
/// Builds clippy inside the repo to make sure we have a clippy executable we can use.
327
369
fn build_clippy ( ) {
328
370
let status = Command :: new ( "cargo" )
@@ -336,9 +378,7 @@ fn build_clippy() {
336
378
}
337
379
338
380
/// Read a `toml` file and return a list of `CrateSources` that we want to check with clippy
339
- fn read_crates ( toml_path : & PathBuf ) -> ( String , Vec < CrateSource > ) {
340
- // save it so that we can use the name of the sources.toml as name for the logfile later.
341
- let toml_filename = toml_path. file_stem ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ;
381
+ fn read_crates ( toml_path : & PathBuf ) -> Vec < CrateSource > {
342
382
let toml_content: String =
343
383
std:: fs:: read_to_string ( & toml_path) . unwrap_or_else ( |_| panic ! ( "Failed to read {}" , toml_path. display( ) ) ) ;
344
384
let crate_list: SourceList =
@@ -398,7 +438,7 @@ fn read_crates(toml_path: &PathBuf) -> (String, Vec<CrateSource>) {
398
438
// sort the crates
399
439
crate_sources. sort ( ) ;
400
440
401
- ( toml_filename , crate_sources)
441
+ crate_sources
402
442
}
403
443
404
444
/// Parse the json output of clippy and return a `ClippyWarning`
@@ -476,16 +516,15 @@ fn lintcheck_needs_rerun(toml_path: &PathBuf) -> bool {
476
516
477
517
/// lintchecks `main()` function
478
518
pub fn run ( clap_config : & ArgMatches ) {
519
+ let config = LintcheckConfig :: from_clap ( clap_config) ;
520
+
479
521
println ! ( "Compiling clippy..." ) ;
480
522
build_clippy ( ) ;
481
523
println ! ( "Done compiling" ) ;
482
524
483
- let clap_toml_path: Option < & str > = clap_config. value_of ( "crates-toml" ) ;
484
- let toml_path: PathBuf = lintcheck_config_toml ( clap_toml_path) ;
485
-
486
525
// if the clippy bin is newer than our logs, throw away target dirs to force clippy to
487
526
// refresh the logs
488
- if lintcheck_needs_rerun ( & toml_path ) {
527
+ if lintcheck_needs_rerun ( & config . sources_toml_path ) {
489
528
let shared_target_dir = "target/lintcheck/shared_target_dir" ;
490
529
match std:: fs:: metadata ( & shared_target_dir) {
491
530
Ok ( metadata) => {
@@ -520,9 +559,8 @@ pub fn run(clap_config: &ArgMatches) {
520
559
// download and extract the crates, then run clippy on them and collect clippys warnings
521
560
// flatten into one big list of warnings
522
561
523
- let ( filename, crates) = read_crates ( & toml_path) ;
524
- let file = format ! ( "lintcheck-logs/{}_logs.txt" , filename) ;
525
- let old_stats = read_stats_from_file ( & file) ;
562
+ let crates = read_crates ( & config. sources_toml_path ) ;
563
+ let old_stats = read_stats_from_file ( & config. lintcheck_results_path ) ;
526
564
527
565
let clippy_warnings: Vec < ClippyWarning > = if let Some ( only_one_crate) = clap_config. value_of ( "only" ) {
528
566
// if we don't have the specified crate in the .toml, throw an error
@@ -562,23 +600,7 @@ pub fn run(clap_config: &ArgMatches) {
562
600
// order to achive some kind of parallelism
563
601
564
602
// by default, use a single thread
565
- let num_cpus = match clap_config. value_of ( "threads" ) {
566
- Some ( threads) => {
567
- let threads: usize = threads
568
- . parse ( )
569
- . expect ( & format ! ( "Failed to parse '{}' to a digit" , threads) ) ;
570
- if threads == 0 {
571
- // automatic choice
572
- // Rayon seems to return thread count so half that for core count
573
- ( rayon:: current_num_threads ( ) / 2 ) as usize
574
- } else {
575
- threads
576
- }
577
- } ,
578
- // no -j passed, use a single thread
579
- None => 1 ,
580
- } ;
581
-
603
+ let num_cpus = config. max_jobs ;
582
604
let num_crates = crates. len ( ) ;
583
605
584
606
// check all crates (default)
@@ -612,15 +634,14 @@ pub fn run(clap_config: &ArgMatches) {
612
634
ices. iter ( )
613
635
. for_each ( |( cratename, msg) | text. push_str ( & format ! ( "{}: '{}'" , cratename, msg) ) ) ;
614
636
615
- println ! ( "Writing logs to {}" , file ) ;
616
- write ( & file , text) . unwrap ( ) ;
637
+ println ! ( "Writing logs to {}" , config . lintcheck_results_path . display ( ) ) ;
638
+ write ( & config . lintcheck_results_path , text) . unwrap ( ) ;
617
639
618
640
print_stats ( old_stats, new_stats) ;
619
641
}
620
642
621
643
/// read the previous stats from the lintcheck-log file
622
- fn read_stats_from_file ( file_path : & String ) -> HashMap < String , usize > {
623
- let file_path = PathBuf :: from ( file_path) ;
644
+ fn read_stats_from_file ( file_path : & PathBuf ) -> HashMap < String , usize > {
624
645
let file_content: String = match std:: fs:: read_to_string ( file_path) . ok ( ) {
625
646
Some ( content) => content,
626
647
None => {
0 commit comments