1 // Run clippy on a fixed set of crates and collect the warnings.
2 // This helps observing the impact clippy changes have on a set of real-world code (and not just our
5 // When a new lint is introduced, we can search the results for new warnings and check for false
8 #![allow(clippy::collapsible_else_if)]
11 use std::process::Command;
12 use std::sync::atomic::{AtomicUsize, Ordering};
13 use std::{collections::HashMap, io::ErrorKind};
17 path::{Path, PathBuf},
20 use clap::{App, Arg, ArgMatches};
21 use rayon::prelude::*;
22 use serde::{Deserialize, Serialize};
23 use serde_json::Value;
24 use walkdir::{DirEntry, WalkDir};
27 const CLIPPY_DRIVER_PATH: &str = "target/debug/clippy-driver";
29 const CARGO_CLIPPY_PATH: &str = "target/debug/cargo-clippy";
32 const CLIPPY_DRIVER_PATH: &str = "target/debug/clippy-driver.exe";
34 const CARGO_CLIPPY_PATH: &str = "target/debug/cargo-clippy.exe";
36 const LINTCHECK_DOWNLOADS: &str = "target/lintcheck/downloads";
37 const LINTCHECK_SOURCES: &str = "target/lintcheck/sources";
39 /// List of sources to check, loaded from a .toml file
40 #[derive(Debug, Serialize, Deserialize)]
42 crates: HashMap<String, TomlCrate>,
45 /// A crate source stored inside the .toml
46 /// will be translated into on one of the `CrateSource` variants
47 #[derive(Debug, Serialize, Deserialize)]
50 versions: Option<Vec<String>>,
51 git_url: Option<String>,
52 git_hash: Option<String>,
54 options: Option<Vec<String>>,
57 /// Represents an archive we download from crates.io, or a git repo, or a local repo/folder
58 /// Once processed (downloaded/extracted/cloned/copied...), this will be translated into a `Crate`
59 #[derive(Debug, Serialize, Deserialize, Eq, Hash, PartialEq, Ord, PartialOrd)]
64 options: Option<Vec<String>>,
70 options: Option<Vec<String>>,
75 options: Option<Vec<String>>,
79 /// Represents the actual source code of a crate that we ran "cargo clippy" on
84 // path to the extracted sources that clippy can check
86 options: Option<Vec<String>>,
89 /// A single warning that clippy issued while checking a `Crate`
91 struct ClippyWarning {
93 crate_version: String,
102 impl std::fmt::Display for ClippyWarning {
103 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106 r#"target/lintcheck/sources/{}-{}/{}:{}:{} {} "{}""#,
107 &self.crate_name, &self.crate_version, &self.file, &self.line, &self.column, &self.linttype, &self.message
113 /// Makes the sources available on the disk for clippy to check.
114 /// Clones a git repo and checks out the specified commit or downloads a crate from crates.io or
115 /// copies a local folder
116 fn download_and_extract(&self) -> Crate {
118 CrateSource::CratesIo { name, version, options } => {
119 let extract_dir = PathBuf::from(LINTCHECK_SOURCES);
120 let krate_download_dir = PathBuf::from(LINTCHECK_DOWNLOADS);
122 // url to download the crate from crates.io
123 let url = format!("https://crates.io/api/v1/crates/{}/{}/download", name, version);
124 println!("Downloading and extracting {} {} from {}", name, version, url);
125 create_dirs(&krate_download_dir, &extract_dir);
127 let krate_file_path = krate_download_dir.join(format!("{}-{}.crate.tar.gz", name, version));
128 // don't download/extract if we already have done so
129 if !krate_file_path.is_file() {
130 // create a file path to download and write the crate data into
131 let mut krate_dest = std::fs::File::create(&krate_file_path).unwrap();
132 let mut krate_req = ureq::get(&url).call().unwrap().into_reader();
133 // copy the crate into the file
134 std::io::copy(&mut krate_req, &mut krate_dest).unwrap();
137 let ungz_tar = flate2::read::GzDecoder::new(std::fs::File::open(&krate_file_path).unwrap());
138 // extract the tar archive
139 let mut archive = tar::Archive::new(ungz_tar);
140 archive.unpack(&extract_dir).expect("Failed to extract!");
142 // crate is extracted, return a new Krate object which contains the path to the extracted
143 // sources that clippy can check
145 version: version.clone(),
147 path: extract_dir.join(format!("{}-{}/", name, version)),
148 options: options.clone(),
158 let mut repo_path = PathBuf::from(LINTCHECK_SOURCES);
159 // add a -git suffix in case we have the same crate from crates.io and a git repo
160 repo_path.push(format!("{}-git", name));
163 // clone the repo if we have not done so
164 if !repo_path.is_dir() {
165 println!("Cloning {} and checking out {}", url, commit);
166 if !Command::new("git")
171 .expect("Failed to clone git repo!")
174 eprintln!("Failed to clone {} into {}", url, repo_path.display())
177 // check out the commit/branch/whatever
178 if !Command::new("git")
181 .current_dir(&repo_path)
183 .expect("Failed to check out commit")
186 eprintln!("Failed to checkout {} of repo at {}", commit, repo_path.display())
190 version: commit.clone(),
193 options: options.clone(),
196 CrateSource::Path { name, path, options } => {
197 // copy path into the dest_crate_root but skip directories that contain a CACHEDIR.TAG file.
198 // The target/ directory contains a CACHEDIR.TAG file so it is the most commonly skipped directory
199 // as a result of this filter.
200 let dest_crate_root = PathBuf::from(LINTCHECK_SOURCES).join(name);
201 if dest_crate_root.exists() {
202 println!("Deleting existing directory at {:?}", dest_crate_root);
203 std::fs::remove_dir_all(&dest_crate_root).unwrap();
206 println!("Copying {:?} to {:?}", path, dest_crate_root);
208 fn is_cache_dir(entry: &DirEntry) -> bool {
209 std::fs::read(entry.path().join("CACHEDIR.TAG"))
210 .map(|x| x.starts_with(b"Signature: 8a477f597d28d172789f06886806bc55"))
214 for entry in WalkDir::new(path).into_iter().filter_entry(|e| !is_cache_dir(e)) {
215 let entry = entry.unwrap();
216 let entry_path = entry.path();
217 let relative_entry_path = entry_path.strip_prefix(path).unwrap();
218 let dest_path = dest_crate_root.join(relative_entry_path);
219 let metadata = entry_path.symlink_metadata().unwrap();
221 if metadata.is_dir() {
222 std::fs::create_dir(dest_path).unwrap();
223 } else if metadata.is_file() {
224 std::fs::copy(entry_path, dest_path).unwrap();
229 version: String::from("local"),
231 path: dest_crate_root,
232 options: options.clone(),
240 /// Run `cargo clippy` on the `Crate` and collect and return all the lint warnings that clippy
244 cargo_clippy_path: &Path,
245 target_dir_index: &AtomicUsize,
247 total_crates_to_lint: usize,
249 ) -> Vec<ClippyWarning> {
250 // advance the atomic index by one
251 let index = target_dir_index.fetch_add(1, Ordering::SeqCst);
252 // "loop" the index within 0..thread_limit
253 let thread_index = index % thread_limit;
254 let perc = (index * 100) / total_crates_to_lint;
256 if thread_limit == 1 {
258 "{}/{} {}% Linting {} {}",
259 index, total_crates_to_lint, perc, &self.name, &self.version
263 "{}/{} {}% Linting {} {} in target dir {:?}",
264 index, total_crates_to_lint, perc, &self.name, &self.version, thread_index
268 let cargo_clippy_path = std::fs::canonicalize(cargo_clippy_path).unwrap();
270 let shared_target_dir = clippy_project_root().join("target/lintcheck/shared_target_dir");
272 let mut args = if fix {
273 vec!["--fix", "--allow-no-vcs", "--", "--cap-lints=warn"]
275 vec!["--", "--message-format=json", "--", "--cap-lints=warn"]
278 if let Some(options) = &self.options {
283 args.extend(&["-Wclippy::pedantic", "-Wclippy::cargo"])
286 let all_output = std::process::Command::new(&cargo_clippy_path)
287 // use the looping index to create individual target dirs
290 shared_target_dir.join(format!("_{:?}", thread_index)),
292 // lint warnings will look like this:
293 // src/cargo/ops/cargo_compile.rs:127:35: warning: usage of `FromIterator::from_iter`
295 .current_dir(&self.path)
297 .unwrap_or_else(|error| {
299 "Encountered error:\n{:?}\ncargo_clippy_path: {}\ncrate path:{}\n",
301 &cargo_clippy_path.display(),
305 let stdout = String::from_utf8_lossy(&all_output.stdout);
306 let stderr = String::from_utf8_lossy(&all_output.stderr);
307 let status = &all_output.status;
309 if !status.success() {
311 "\nWARNING: bad exit status after checking {} {} \n",
312 self.name, self.version
317 if let Some(stderr) = stderr
319 .find(|line| line.contains("failed to automatically apply fixes suggested by rustc to crate"))
321 let subcrate = &stderr[63..];
323 "ERROR: failed to apply some suggetion to {} / to (sub)crate {}",
327 // fast path, we don't need the warnings anyway
331 let output_lines = stdout.lines();
332 let warnings: Vec<ClippyWarning> = output_lines
334 // get all clippy warnings and ICEs
335 .filter(|line| filter_clippy_warnings(&line))
336 .map(|json_msg| parse_json_message(json_msg, &self))
344 struct LintcheckConfig {
345 // max number of jobs to spawn (default 1)
347 // we read the sources to check from here
348 sources_toml_path: PathBuf,
349 // we save the clippy lint results here
350 lintcheck_results_path: PathBuf,
351 // whether to just run --fix and not collect all the warnings
355 impl LintcheckConfig {
356 fn from_clap(clap_config: &ArgMatches) -> Self {
357 // first, check if we got anything passed via the LINTCHECK_TOML env var,
358 // if not, ask clap if we got any value for --crates-toml <foo>
359 // if not, use the default "lintcheck/lintcheck_crates.toml"
360 let sources_toml = env::var("LINTCHECK_TOML").unwrap_or_else(|_| {
362 .value_of("crates-toml")
364 .unwrap_or("lintcheck/lintcheck_crates.toml")
368 let sources_toml_path = PathBuf::from(sources_toml);
370 // for the path where we save the lint results, get the filename without extension (so for
371 // wasd.toml, use "wasd"...)
372 let filename: PathBuf = sources_toml_path.file_stem().unwrap().into();
373 let lintcheck_results_path = PathBuf::from(format!("lintcheck-logs/{}_logs.txt", filename.display()));
375 // look at the --threads arg, if 0 is passed, ask rayon rayon how many threads it would spawn and
376 // use half of that for the physical core count
377 // by default use a single thread
378 let max_jobs = match clap_config.value_of("threads") {
380 let threads: usize = threads
382 .unwrap_or_else(|_| panic!("Failed to parse '{}' to a digit", threads));
385 // Rayon seems to return thread count so half that for core count
386 (rayon::current_num_threads() / 2) as usize
391 // no -j passed, use a single thread
394 let fix: bool = clap_config.is_present("fix");
399 lintcheck_results_path,
405 /// takes a single json-formatted clippy warnings and returns true (we are interested in that line)
406 /// or false (we aren't)
407 fn filter_clippy_warnings(line: &str) -> bool {
408 // we want to collect ICEs because clippy might have crashed.
409 // these are summarized later
410 if line.contains("internal compiler error: ") {
413 // in general, we want all clippy warnings
414 // however due to some kind of bug, sometimes there are absolute paths
415 // to libcore files inside the message
416 // or we end up with cargo-metadata output (https://github.com/rust-lang/rust-clippy/issues/6508)
418 // filter out these message to avoid unnecessary noise in the logs
419 if line.contains("clippy::")
420 && !(line.contains("could not read cargo metadata")
421 || (line.contains(".rustup") && line.contains("toolchains")))
428 /// Builds clippy inside the repo to make sure we have a clippy executable we can use.
430 let status = Command::new("cargo")
433 .expect("Failed to build clippy!");
434 if !status.success() {
435 eprintln!("Error: Failed to compile Clippy!");
436 std::process::exit(1);
440 /// Read a `toml` file and return a list of `CrateSources` that we want to check with clippy
441 fn read_crates(toml_path: &Path) -> Vec<CrateSource> {
442 let toml_content: String =
443 std::fs::read_to_string(&toml_path).unwrap_or_else(|_| panic!("Failed to read {}", toml_path.display()));
444 let crate_list: SourceList =
445 toml::from_str(&toml_content).unwrap_or_else(|e| panic!("Failed to parse {}: \n{}", toml_path.display(), e));
446 // parse the hashmap of the toml file into a list of crates
447 let tomlcrates: Vec<TomlCrate> = crate_list
450 .map(|(_cratename, tomlcrate)| tomlcrate)
453 // flatten TomlCrates into CrateSources (one TomlCrates may represent several versions of a crate =>
454 // multiple Cratesources)
455 let mut crate_sources = Vec::new();
456 tomlcrates.into_iter().for_each(|tk| {
457 if let Some(ref path) = tk.path {
458 crate_sources.push(CrateSource::Path {
459 name: tk.name.clone(),
460 path: PathBuf::from(path),
461 options: tk.options.clone(),
465 // if we have multiple versions, save each one
466 if let Some(ref versions) = tk.versions {
467 versions.iter().for_each(|ver| {
468 crate_sources.push(CrateSource::CratesIo {
469 name: tk.name.clone(),
470 version: ver.to_string(),
471 options: tk.options.clone(),
475 // otherwise, we should have a git source
476 if tk.git_url.is_some() && tk.git_hash.is_some() {
477 crate_sources.push(CrateSource::Git {
478 name: tk.name.clone(),
479 url: tk.git_url.clone().unwrap(),
480 commit: tk.git_hash.clone().unwrap(),
481 options: tk.options.clone(),
484 // if we have a version as well as a git data OR only one git data, something is funky
485 if tk.versions.is_some() && (tk.git_url.is_some() || tk.git_hash.is_some())
486 || tk.git_hash.is_some() != tk.git_url.is_some()
488 eprintln!("tomlkrate: {:?}", tk);
489 if tk.git_hash.is_some() != tk.git_url.is_some() {
490 panic!("Error: Encountered TomlCrate with only one of git_hash and git_url!");
492 if tk.path.is_some() && (tk.git_hash.is_some() || tk.versions.is_some()) {
493 panic!("Error: TomlCrate can only have one of 'git_.*', 'version' or 'path' fields");
495 unreachable!("Failed to translate TomlCrate into CrateSource!");
499 crate_sources.sort();
504 /// Parse the json output of clippy and return a `ClippyWarning`
505 fn parse_json_message(json_message: &str, krate: &Crate) -> ClippyWarning {
506 let jmsg: Value = serde_json::from_str(&json_message).unwrap_or_else(|e| panic!("Failed to parse json:\n{:?}", e));
508 let file: String = jmsg["message"]["spans"][0]["file_name"]
513 let file = if file.contains(".cargo") {
514 // if we deal with macros, a filename may show the origin of a macro which can be inside a dep from
516 // don't show the full path in that case.
518 // /home/matthias/.cargo/registry/src/github.com-1ecc6299db9ec823/syn-1.0.63/src/custom_keyword.rs
519 let path = PathBuf::from(file);
520 let mut piter = path.iter();
521 // consume all elements until we find ".cargo", so that "/home/matthias" is skipped
522 let _: Option<&OsStr> = piter.find(|x| x == &std::ffi::OsString::from(".cargo"));
523 // collect the remaining segments
524 let file = piter.collect::<PathBuf>();
525 format!("{}", file.display())
531 crate_name: krate.name.to_string(),
532 crate_version: krate.version.to_string(),
534 line: jmsg["message"]["spans"][0]["line_start"]
538 column: jmsg["message"]["spans"][0]["text"][0]["highlight_start"]
542 linttype: jmsg["message"]["code"]["code"].to_string().trim_matches('"').into(),
543 message: jmsg["message"]["message"].to_string().trim_matches('"').into(),
544 is_ice: json_message.contains("internal compiler error: "),
548 /// Generate a short list of occuring lints-types and their count
549 fn gather_stats(clippy_warnings: &[ClippyWarning]) -> (String, HashMap<&String, usize>) {
550 // count lint type occurrences
551 let mut counter: HashMap<&String, usize> = HashMap::new();
554 .for_each(|wrn| *counter.entry(&wrn.linttype).or_insert(0) += 1);
556 // collect into a tupled list for sorting
557 let mut stats: Vec<(&&String, &usize)> = counter.iter().map(|(lint, count)| (lint, count)).collect();
558 // sort by "000{count} {clippy::lintname}"
559 // to not have a lint with 200 and 2 warnings take the same spot
560 stats.sort_by_key(|(lint, count)| format!("{:0>4}, {}", count, lint));
562 let stats_string = stats
564 .map(|(lint, count)| format!("{} {}\n", lint, count))
565 .collect::<String>();
567 (stats_string, counter)
570 /// check if the latest modification of the logfile is older than the modification date of the
571 /// clippy binary, if this is true, we should clean the lintchec shared target directory and recheck
572 fn lintcheck_needs_rerun(lintcheck_logs_path: &Path) -> bool {
573 if !lintcheck_logs_path.exists() {
577 let clippy_modified: std::time::SystemTime = {
578 let mut times = [CLIPPY_DRIVER_PATH, CARGO_CLIPPY_PATH].iter().map(|p| {
580 .expect("failed to get metadata of file")
582 .expect("failed to get modification date")
584 // the oldest modification of either of the binaries
585 std::cmp::max(times.next().unwrap(), times.next().unwrap())
588 let logs_modified: std::time::SystemTime = std::fs::metadata(lintcheck_logs_path)
589 .expect("failed to get metadata of file")
591 .expect("failed to get modification date");
593 // time is represented in seconds since X
594 // logs_modified 2 and clippy_modified 5 means clippy binary is older and we need to recheck
595 logs_modified < clippy_modified
598 fn is_in_clippy_root() -> bool {
599 if let Ok(pb) = std::env::current_dir() {
600 if let Some(file) = pb.file_name() {
601 return file == PathBuf::from("rust-clippy");
608 /// lintchecks `main()` function
612 /// This function panics if the clippy binaries don't exist
613 /// or if lintcheck is executed from the wrong directory (aka none-repo-root)
615 // assert that we launch lintcheck from the repo root (via cargo lintcheck)
616 if !is_in_clippy_root() {
617 eprintln!("lintcheck needs to be run from clippys repo root!\nUse `cargo lintcheck` alternatively.");
618 std::process::exit(3);
621 let clap_config = &get_clap_config();
623 let config = LintcheckConfig::from_clap(clap_config);
625 println!("Compiling clippy...");
627 println!("Done compiling");
629 // if the clippy bin is newer than our logs, throw away target dirs to force clippy to
631 if lintcheck_needs_rerun(&config.lintcheck_results_path) {
632 let shared_target_dir = "target/lintcheck/shared_target_dir";
633 // if we get an Err here, the shared target dir probably does simply not exist
634 if let Ok(metadata) = std::fs::metadata(&shared_target_dir) {
635 if metadata.is_dir() {
636 println!("Clippy is newer than lint check logs, clearing lintcheck shared target dir...");
637 std::fs::remove_dir_all(&shared_target_dir)
638 .expect("failed to remove target/lintcheck/shared_target_dir");
643 let cargo_clippy_path: PathBuf = PathBuf::from(CARGO_CLIPPY_PATH)
645 .expect("failed to canonicalize path to clippy binary");
647 // assert that clippy is found
649 cargo_clippy_path.is_file(),
650 "target/debug/cargo-clippy binary not found! {}",
651 cargo_clippy_path.display()
654 let clippy_ver = std::process::Command::new(CARGO_CLIPPY_PATH)
657 .map(|o| String::from_utf8_lossy(&o.stdout).into_owned())
658 .expect("could not get clippy version!");
660 // download and extract the crates, then run clippy on them and collect clippys warnings
661 // flatten into one big list of warnings
663 let crates = read_crates(&config.sources_toml_path);
664 let old_stats = read_stats_from_file(&config.lintcheck_results_path);
666 let counter = AtomicUsize::new(1);
668 let clippy_warnings: Vec<ClippyWarning> = if let Some(only_one_crate) = clap_config.value_of("only") {
669 // if we don't have the specified crate in the .toml, throw an error
670 if !crates.iter().any(|krate| {
671 let name = match krate {
672 CrateSource::CratesIo { name, .. } | CrateSource::Git { name, .. } | CrateSource::Path { name, .. } => {
676 name == only_one_crate
679 "ERROR: could not find crate '{}' in lintcheck/lintcheck_crates.toml",
682 std::process::exit(1);
685 // only check a single crate that was passed via cmdline
688 .map(|krate| krate.download_and_extract())
689 .filter(|krate| krate.name == only_one_crate)
690 .flat_map(|krate| krate.run_clippy_lints(&cargo_clippy_path, &AtomicUsize::new(0), 1, 1, config.fix))
693 if config.max_jobs > 1 {
694 // run parallel with rayon
696 // Ask rayon for thread count. Assume that half of that is the number of physical cores
697 // Use one target dir for each core so that we can run N clippys in parallel.
698 // We need to use different target dirs because cargo would lock them for a single build otherwise,
699 // killing the parallelism. However this also means that deps will only be reused half/a
700 // quarter of the time which might result in a longer wall clock runtime
702 // This helps when we check many small crates with dep-trees that don't have a lot of branches in
703 // order to achive some kind of parallelism
705 // by default, use a single thread
706 let num_cpus = config.max_jobs;
707 let num_crates = crates.len();
709 // check all crates (default)
712 .map(|krate| krate.download_and_extract())
714 krate.run_clippy_lints(&cargo_clippy_path, &counter, num_cpus, num_crates, config.fix)
719 let num_crates = crates.len();
722 .map(|krate| krate.download_and_extract())
723 .flat_map(|krate| krate.run_clippy_lints(&cargo_clippy_path, &counter, 1, num_crates, config.fix))
728 // if we are in --fix mode, don't change the log files, terminate here
733 // generate some stats
734 let (stats_formatted, new_stats) = gather_stats(&clippy_warnings);
736 // grab crashes/ICEs, save the crate name and the ice message
737 let ices: Vec<(&String, &String)> = clippy_warnings
739 .filter(|warning| warning.is_ice)
740 .map(|w| (&w.crate_name, &w.message))
743 let mut all_msgs: Vec<String> = clippy_warnings.iter().map(ToString::to_string).collect();
745 all_msgs.push("\n\n\n\nStats:\n".into());
746 all_msgs.push(stats_formatted);
748 // save the text into lintcheck-logs/logs.txt
749 let mut text = clippy_ver; // clippy version number on top
750 text.push_str(&format!("\n{}", all_msgs.join("")));
751 text.push_str("ICEs:\n");
753 .for_each(|(cratename, msg)| text.push_str(&format!("{}: '{}'", cratename, msg)));
755 println!("Writing logs to {}", config.lintcheck_results_path.display());
756 write(&config.lintcheck_results_path, text).unwrap();
758 print_stats(old_stats, new_stats);
761 /// read the previous stats from the lintcheck-log file
762 fn read_stats_from_file(file_path: &Path) -> HashMap<String, usize> {
763 let file_content: String = match std::fs::read_to_string(file_path).ok() {
764 Some(content) => content,
766 return HashMap::new();
770 let lines: Vec<String> = file_content.lines().map(ToString::to_string).collect();
772 // search for the beginning "Stats:" and the end "ICEs:" of the section we want
773 let start = lines.iter().position(|line| line == "Stats:").unwrap();
774 let end = lines.iter().position(|line| line == "ICEs:").unwrap();
776 let stats_lines = &lines[start + 1..end];
781 let mut spl = line.split(' ');
783 spl.next().unwrap().to_string(),
784 spl.next().unwrap().parse::<usize>().unwrap(),
787 .collect::<HashMap<String, usize>>()
790 /// print how lint counts changed between runs
791 fn print_stats(old_stats: HashMap<String, usize>, new_stats: HashMap<&String, usize>) {
792 let same_in_both_hashmaps = old_stats
794 .filter(|(old_key, old_val)| new_stats.get::<&String>(&old_key) == Some(old_val))
795 .map(|(k, v)| (k.to_string(), *v))
796 .collect::<Vec<(String, usize)>>();
798 let mut old_stats_deduped = old_stats;
799 let mut new_stats_deduped = new_stats;
801 // remove duplicates from both hashmaps
802 same_in_both_hashmaps.iter().for_each(|(k, v)| {
803 assert!(old_stats_deduped.remove(k) == Some(*v));
804 assert!(new_stats_deduped.remove(k) == Some(*v));
807 println!("\nStats:");
809 // list all new counts (key is in new stats but not in old stats)
812 .filter(|(new_key, _)| old_stats_deduped.get::<str>(&new_key).is_none())
813 .for_each(|(new_key, new_value)| {
814 println!("{} 0 => {}", new_key, new_value);
817 // list all changed counts (key is in both maps but value differs)
820 .filter(|(new_key, _new_val)| old_stats_deduped.get::<str>(&new_key).is_some())
821 .for_each(|(new_key, new_val)| {
822 let old_val = old_stats_deduped.get::<str>(&new_key).unwrap();
823 println!("{} {} => {}", new_key, old_val, new_val);
826 // list all gone counts (key is in old status but not in new stats)
829 .filter(|(old_key, _)| new_stats_deduped.get::<&String>(&old_key).is_none())
830 .for_each(|(old_key, old_value)| {
831 println!("{} {} => 0", old_key, old_value);
835 /// Create necessary directories to run the lintcheck tool.
839 /// This function panics if creating one of the dirs fails.
840 fn create_dirs(krate_download_dir: &Path, extract_dir: &Path) {
841 std::fs::create_dir("target/lintcheck/").unwrap_or_else(|err| {
842 if err.kind() != ErrorKind::AlreadyExists {
843 panic!("cannot create lintcheck target dir");
846 std::fs::create_dir(&krate_download_dir).unwrap_or_else(|err| {
847 if err.kind() != ErrorKind::AlreadyExists {
848 panic!("cannot create crate download dir");
851 std::fs::create_dir(&extract_dir).unwrap_or_else(|err| {
852 if err.kind() != ErrorKind::AlreadyExists {
853 panic!("cannot create crate extraction dir");
858 fn get_clap_config<'a>() -> ArgMatches<'a> {
859 App::new("lintcheck")
860 .about("run clippy on a set of crates and check output")
862 Arg::with_name("only")
866 .help("only process a single crate of the list"),
869 Arg::with_name("crates-toml")
871 .value_name("CRATES-SOURCES-TOML-PATH")
873 .help("set the path for a crates.toml where lintcheck should read the sources from"),
876 Arg::with_name("threads")
881 .help("number of threads to use, 0 automatic choice"),
884 Arg::with_name("fix")
886 .help("runs cargo clippy --fix and checks if all suggestions apply"),
891 /// Returns the path to the Clippy project directory
895 /// Panics if the current directory could not be retrieved, there was an error reading any of the
896 /// Cargo.toml files or ancestor directory is the clippy root directory
898 pub fn clippy_project_root() -> PathBuf {
899 let current_dir = std::env::current_dir().unwrap();
900 for path in current_dir.ancestors() {
901 let result = std::fs::read_to_string(path.join("Cargo.toml"));
902 if let Err(err) = &result {
903 if err.kind() == std::io::ErrorKind::NotFound {
908 let content = result.unwrap();
909 if content.contains("[package]\nname = \"clippy\"") {
910 return path.to_path_buf();
913 panic!("error: Can't determine root of project. Please run inside a Clippy working dir.");
917 fn lintcheck_test() {
923 "./lintcheck/Cargo.toml",
926 "lintcheck/test_sources.toml",
928 let status = std::process::Command::new("cargo")
930 .current_dir("..") // repo root
934 assert!(status.unwrap().success());