Skip to content

Commit d931d1b

Browse files
committed
lintcheck: refactor: introduce a basic LintcheckConfig struct which holds the job limit and paths to the sources and log files
1 parent ac2f041 commit d931d1b

File tree

1 file changed

+66
-45
lines changed

1 file changed

+66
-45
lines changed

clippy_dev/src/lintcheck.rs

+66-45
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,61 @@ impl Crate {
287287
}
288288
}
289289

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+
290345
/// takes a single json-formatted clippy warnings and returns true (we are interested in that line)
291346
/// or false (we aren't)
292347
fn filter_clippy_warnings(line: &str) -> bool {
@@ -310,19 +365,6 @@ fn filter_clippy_warnings(line: &str) -> bool {
310365
false
311366
}
312367

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-
326368
/// Builds clippy inside the repo to make sure we have a clippy executable we can use.
327369
fn build_clippy() {
328370
let status = Command::new("cargo")
@@ -336,9 +378,7 @@ fn build_clippy() {
336378
}
337379

338380
/// 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> {
342382
let toml_content: String =
343383
std::fs::read_to_string(&toml_path).unwrap_or_else(|_| panic!("Failed to read {}", toml_path.display()));
344384
let crate_list: SourceList =
@@ -398,7 +438,7 @@ fn read_crates(toml_path: &PathBuf) -> (String, Vec<CrateSource>) {
398438
// sort the crates
399439
crate_sources.sort();
400440

401-
(toml_filename, crate_sources)
441+
crate_sources
402442
}
403443

404444
/// Parse the json output of clippy and return a `ClippyWarning`
@@ -476,16 +516,15 @@ fn lintcheck_needs_rerun(toml_path: &PathBuf) -> bool {
476516

477517
/// lintchecks `main()` function
478518
pub fn run(clap_config: &ArgMatches) {
519+
let config = LintcheckConfig::from_clap(clap_config);
520+
479521
println!("Compiling clippy...");
480522
build_clippy();
481523
println!("Done compiling");
482524

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-
486525
// if the clippy bin is newer than our logs, throw away target dirs to force clippy to
487526
// refresh the logs
488-
if lintcheck_needs_rerun(&toml_path) {
527+
if lintcheck_needs_rerun(&config.sources_toml_path) {
489528
let shared_target_dir = "target/lintcheck/shared_target_dir";
490529
match std::fs::metadata(&shared_target_dir) {
491530
Ok(metadata) => {
@@ -520,9 +559,8 @@ pub fn run(clap_config: &ArgMatches) {
520559
// download and extract the crates, then run clippy on them and collect clippys warnings
521560
// flatten into one big list of warnings
522561

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);
526564

527565
let clippy_warnings: Vec<ClippyWarning> = if let Some(only_one_crate) = clap_config.value_of("only") {
528566
// if we don't have the specified crate in the .toml, throw an error
@@ -562,23 +600,7 @@ pub fn run(clap_config: &ArgMatches) {
562600
// order to achive some kind of parallelism
563601

564602
// 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;
582604
let num_crates = crates.len();
583605

584606
// check all crates (default)
@@ -612,15 +634,14 @@ pub fn run(clap_config: &ArgMatches) {
612634
ices.iter()
613635
.for_each(|(cratename, msg)| text.push_str(&format!("{}: '{}'", cratename, msg)));
614636

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();
617639

618640
print_stats(old_stats, new_stats);
619641
}
620642

621643
/// 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> {
624645
let file_content: String = match std::fs::read_to_string(file_path).ok() {
625646
Some(content) => content,
626647
None => {

0 commit comments

Comments
 (0)