]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/lintcheck/src/main.rs
Rollup merge of #87260 - antoyo:libgccjit-codegen, r=Mark-Simulacrum
[rust.git] / src / tools / clippy / lintcheck / src / main.rs
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
3 // testsuite).
4 //
5 // When a new lint is introduced, we can search the results for new warnings and check for false
6 // positives.
7
8 #![allow(clippy::collapsible_else_if)]
9
10 use std::ffi::OsStr;
11 use std::process::Command;
12 use std::sync::atomic::{AtomicUsize, Ordering};
13 use std::{collections::HashMap, io::ErrorKind};
14 use std::{
15     env, fmt,
16     fs::write,
17     path::{Path, PathBuf},
18 };
19
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};
25
26 #[cfg(not(windows))]
27 const CLIPPY_DRIVER_PATH: &str = "target/debug/clippy-driver";
28 #[cfg(not(windows))]
29 const CARGO_CLIPPY_PATH: &str = "target/debug/cargo-clippy";
30
31 #[cfg(windows)]
32 const CLIPPY_DRIVER_PATH: &str = "target/debug/clippy-driver.exe";
33 #[cfg(windows)]
34 const CARGO_CLIPPY_PATH: &str = "target/debug/cargo-clippy.exe";
35
36 const LINTCHECK_DOWNLOADS: &str = "target/lintcheck/downloads";
37 const LINTCHECK_SOURCES: &str = "target/lintcheck/sources";
38
39 /// List of sources to check, loaded from a .toml file
40 #[derive(Debug, Serialize, Deserialize)]
41 struct SourceList {
42     crates: HashMap<String, TomlCrate>,
43 }
44
45 /// A crate source stored inside the .toml
46 /// will be translated into on one of the `CrateSource` variants
47 #[derive(Debug, Serialize, Deserialize)]
48 struct TomlCrate {
49     name: String,
50     versions: Option<Vec<String>>,
51     git_url: Option<String>,
52     git_hash: Option<String>,
53     path: Option<String>,
54     options: Option<Vec<String>>,
55 }
56
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)]
60 enum CrateSource {
61     CratesIo {
62         name: String,
63         version: String,
64         options: Option<Vec<String>>,
65     },
66     Git {
67         name: String,
68         url: String,
69         commit: String,
70         options: Option<Vec<String>>,
71     },
72     Path {
73         name: String,
74         path: PathBuf,
75         options: Option<Vec<String>>,
76     },
77 }
78
79 /// Represents the actual source code of a crate that we ran "cargo clippy" on
80 #[derive(Debug)]
81 struct Crate {
82     version: String,
83     name: String,
84     // path to the extracted sources that clippy can check
85     path: PathBuf,
86     options: Option<Vec<String>>,
87 }
88
89 /// A single warning that clippy issued while checking a `Crate`
90 #[derive(Debug)]
91 struct ClippyWarning {
92     crate_name: String,
93     crate_version: String,
94     file: String,
95     line: String,
96     column: String,
97     linttype: String,
98     message: String,
99     is_ice: bool,
100 }
101
102 impl std::fmt::Display for ClippyWarning {
103     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104         writeln!(
105             f,
106             r#"target/lintcheck/sources/{}-{}/{}:{}:{} {} "{}""#,
107             &self.crate_name, &self.crate_version, &self.file, &self.line, &self.column, &self.linttype, &self.message
108         )
109     }
110 }
111
112 impl CrateSource {
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 {
117         match self {
118             CrateSource::CratesIo { name, version, options } => {
119                 let extract_dir = PathBuf::from(LINTCHECK_SOURCES);
120                 let krate_download_dir = PathBuf::from(LINTCHECK_DOWNLOADS);
121
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);
126
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();
135
136                     // unzip the tarball
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!");
141                 }
142                 // crate is extracted, return a new Krate object which contains the path to the extracted
143                 // sources that clippy can check
144                 Crate {
145                     version: version.clone(),
146                     name: name.clone(),
147                     path: extract_dir.join(format!("{}-{}/", name, version)),
148                     options: options.clone(),
149                 }
150             },
151             CrateSource::Git {
152                 name,
153                 url,
154                 commit,
155                 options,
156             } => {
157                 let repo_path = {
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));
161                     repo_path
162                 };
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")
167                         .arg("clone")
168                         .arg(url)
169                         .arg(&repo_path)
170                         .status()
171                         .expect("Failed to clone git repo!")
172                         .success()
173                     {
174                         eprintln!("Failed to clone {} into {}", url, repo_path.display())
175                     }
176                 }
177                 // check out the commit/branch/whatever
178                 if !Command::new("git")
179                     .arg("checkout")
180                     .arg(commit)
181                     .current_dir(&repo_path)
182                     .status()
183                     .expect("Failed to check out commit")
184                     .success()
185                 {
186                     eprintln!("Failed to checkout {} of repo at {}", commit, repo_path.display())
187                 }
188
189                 Crate {
190                     version: commit.clone(),
191                     name: name.clone(),
192                     path: repo_path,
193                     options: options.clone(),
194                 }
195             },
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();
204                 }
205
206                 println!("Copying {:?} to {:?}", path, dest_crate_root);
207
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"))
211                         .unwrap_or(false)
212                 }
213
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();
220
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();
225                     }
226                 }
227
228                 Crate {
229                     version: String::from("local"),
230                     name: name.clone(),
231                     path: dest_crate_root,
232                     options: options.clone(),
233                 }
234             },
235         }
236     }
237 }
238
239 impl Crate {
240     /// Run `cargo clippy` on the `Crate` and collect and return all the lint warnings that clippy
241     /// issued
242     fn run_clippy_lints(
243         &self,
244         cargo_clippy_path: &Path,
245         target_dir_index: &AtomicUsize,
246         thread_limit: usize,
247         total_crates_to_lint: usize,
248         fix: bool,
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;
255
256         if thread_limit == 1 {
257             println!(
258                 "{}/{} {}% Linting {} {}",
259                 index, total_crates_to_lint, perc, &self.name, &self.version
260             );
261         } else {
262             println!(
263                 "{}/{} {}% Linting {} {} in target dir {:?}",
264                 index, total_crates_to_lint, perc, &self.name, &self.version, thread_index
265             );
266         }
267
268         let cargo_clippy_path = std::fs::canonicalize(cargo_clippy_path).unwrap();
269
270         let shared_target_dir = clippy_project_root().join("target/lintcheck/shared_target_dir");
271
272         let mut args = if fix {
273             vec!["--fix", "--allow-no-vcs", "--", "--cap-lints=warn"]
274         } else {
275             vec!["--", "--message-format=json", "--", "--cap-lints=warn"]
276         };
277
278         if let Some(options) = &self.options {
279             for opt in options {
280                 args.push(opt);
281             }
282         } else {
283             args.extend(&["-Wclippy::pedantic", "-Wclippy::cargo"])
284         }
285
286         let all_output = std::process::Command::new(&cargo_clippy_path)
287             // use the looping index to create individual target dirs
288             .env(
289                 "CARGO_TARGET_DIR",
290                 shared_target_dir.join(format!("_{:?}", thread_index)),
291             )
292             // lint warnings will look like this:
293             // src/cargo/ops/cargo_compile.rs:127:35: warning: usage of `FromIterator::from_iter`
294             .args(&args)
295             .current_dir(&self.path)
296             .output()
297             .unwrap_or_else(|error| {
298                 panic!(
299                     "Encountered error:\n{:?}\ncargo_clippy_path: {}\ncrate path:{}\n",
300                     error,
301                     &cargo_clippy_path.display(),
302                     &self.path.display()
303                 );
304             });
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;
308
309         if !status.success() {
310             eprintln!(
311                 "\nWARNING: bad exit status after checking {} {} \n",
312                 self.name, self.version
313             );
314         }
315
316         if fix {
317             if let Some(stderr) = stderr
318                 .lines()
319                 .find(|line| line.contains("failed to automatically apply fixes suggested by rustc to crate"))
320             {
321                 let subcrate = &stderr[63..];
322                 println!(
323                     "ERROR: failed to apply some suggetion to {} / to (sub)crate {}",
324                     self.name, subcrate
325                 );
326             }
327             // fast path, we don't need the warnings anyway
328             return Vec::new();
329         }
330
331         let output_lines = stdout.lines();
332         let warnings: Vec<ClippyWarning> = output_lines
333             .into_iter()
334             // get all clippy warnings and ICEs
335             .filter(|line| filter_clippy_warnings(&line))
336             .map(|json_msg| parse_json_message(json_msg, &self))
337             .collect();
338
339         warnings
340     }
341 }
342
343 #[derive(Debug)]
344 struct LintcheckConfig {
345     // max number of jobs to spawn (default 1)
346     max_jobs: usize,
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
352     fix: bool,
353 }
354
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(|_| {
361             clap_config
362                 .value_of("crates-toml")
363                 .clone()
364                 .unwrap_or("lintcheck/lintcheck_crates.toml")
365                 .to_string()
366         });
367
368         let sources_toml_path = PathBuf::from(sources_toml);
369
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()));
374
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") {
379             Some(threads) => {
380                 let threads: usize = threads
381                     .parse()
382                     .unwrap_or_else(|_| panic!("Failed to parse '{}' to a digit", threads));
383                 if threads == 0 {
384                     // automatic choice
385                     // Rayon seems to return thread count so half that for core count
386                     (rayon::current_num_threads() / 2) as usize
387                 } else {
388                     threads
389                 }
390             },
391             // no -j passed, use a single thread
392             None => 1,
393         };
394         let fix: bool = clap_config.is_present("fix");
395
396         LintcheckConfig {
397             max_jobs,
398             sources_toml_path,
399             lintcheck_results_path,
400             fix,
401         }
402     }
403 }
404
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: ") {
411         return true;
412     }
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)
417
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")))
422     {
423         return true;
424     }
425     false
426 }
427
428 /// Builds clippy inside the repo to make sure we have a clippy executable we can use.
429 fn build_clippy() {
430     let status = Command::new("cargo")
431         .arg("build")
432         .status()
433         .expect("Failed to build clippy!");
434     if !status.success() {
435         eprintln!("Error: Failed to compile Clippy!");
436         std::process::exit(1);
437     }
438 }
439
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
448         .crates
449         .into_iter()
450         .map(|(_cratename, tomlcrate)| tomlcrate)
451         .collect();
452
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(),
462             });
463         }
464
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(),
472                 });
473             })
474         }
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(),
482             });
483         }
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()
487         {
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!");
491             }
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");
494             }
495             unreachable!("Failed to translate TomlCrate into CrateSource!");
496         }
497     });
498     // sort the crates
499     crate_sources.sort();
500
501     crate_sources
502 }
503
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));
507
508     let file: String = jmsg["message"]["spans"][0]["file_name"]
509         .to_string()
510         .trim_matches('"')
511         .into();
512
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
515         // the registry.
516         // don't show the full path in that case.
517
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())
526     } else {
527         file
528     };
529
530     ClippyWarning {
531         crate_name: krate.name.to_string(),
532         crate_version: krate.version.to_string(),
533         file,
534         line: jmsg["message"]["spans"][0]["line_start"]
535             .to_string()
536             .trim_matches('"')
537             .into(),
538         column: jmsg["message"]["spans"][0]["text"][0]["highlight_start"]
539             .to_string()
540             .trim_matches('"')
541             .into(),
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: "),
545     }
546 }
547
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();
552     clippy_warnings
553         .iter()
554         .for_each(|wrn| *counter.entry(&wrn.linttype).or_insert(0) += 1);
555
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));
561
562     let stats_string = stats
563         .iter()
564         .map(|(lint, count)| format!("{} {}\n", lint, count))
565         .collect::<String>();
566
567     (stats_string, counter)
568 }
569
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() {
574         return true;
575     }
576
577     let clippy_modified: std::time::SystemTime = {
578         let mut times = [CLIPPY_DRIVER_PATH, CARGO_CLIPPY_PATH].iter().map(|p| {
579             std::fs::metadata(p)
580                 .expect("failed to get metadata of file")
581                 .modified()
582                 .expect("failed to get modification date")
583         });
584         // the oldest modification of either of the binaries
585         std::cmp::max(times.next().unwrap(), times.next().unwrap())
586     };
587
588     let logs_modified: std::time::SystemTime = std::fs::metadata(lintcheck_logs_path)
589         .expect("failed to get metadata of file")
590         .modified()
591         .expect("failed to get modification date");
592
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
596 }
597
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");
602         }
603     }
604
605     false
606 }
607
608 /// lintchecks `main()` function
609 ///
610 /// # Panics
611 ///
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)
614 pub fn main() {
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);
619     }
620
621     let clap_config = &get_clap_config();
622
623     let config = LintcheckConfig::from_clap(clap_config);
624
625     println!("Compiling clippy...");
626     build_clippy();
627     println!("Done compiling");
628
629     // if the clippy bin is newer than our logs, throw away target dirs to force clippy to
630     // refresh the logs
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");
639             }
640         }
641     }
642
643     let cargo_clippy_path: PathBuf = PathBuf::from(CARGO_CLIPPY_PATH)
644         .canonicalize()
645         .expect("failed to canonicalize path to clippy binary");
646
647     // assert that clippy is found
648     assert!(
649         cargo_clippy_path.is_file(),
650         "target/debug/cargo-clippy binary not found! {}",
651         cargo_clippy_path.display()
652     );
653
654     let clippy_ver = std::process::Command::new(CARGO_CLIPPY_PATH)
655         .arg("--version")
656         .output()
657         .map(|o| String::from_utf8_lossy(&o.stdout).into_owned())
658         .expect("could not get clippy version!");
659
660     // download and extract the crates, then run clippy on them and collect clippys warnings
661     // flatten into one big list of warnings
662
663     let crates = read_crates(&config.sources_toml_path);
664     let old_stats = read_stats_from_file(&config.lintcheck_results_path);
665
666     let counter = AtomicUsize::new(1);
667
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, .. } => {
673                     name
674                 },
675             };
676             name == only_one_crate
677         }) {
678             eprintln!(
679                 "ERROR: could not find crate '{}' in lintcheck/lintcheck_crates.toml",
680                 only_one_crate
681             );
682             std::process::exit(1);
683         }
684
685         // only check a single crate that was passed via cmdline
686         crates
687             .into_iter()
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))
691             .collect()
692     } else {
693         if config.max_jobs > 1 {
694             // run parallel with rayon
695
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
701
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
704
705             // by default, use a single thread
706             let num_cpus = config.max_jobs;
707             let num_crates = crates.len();
708
709             // check all crates (default)
710             crates
711                 .into_par_iter()
712                 .map(|krate| krate.download_and_extract())
713                 .flat_map(|krate| {
714                     krate.run_clippy_lints(&cargo_clippy_path, &counter, num_cpus, num_crates, config.fix)
715                 })
716                 .collect()
717         } else {
718             // run sequential
719             let num_crates = crates.len();
720             crates
721                 .into_iter()
722                 .map(|krate| krate.download_and_extract())
723                 .flat_map(|krate| krate.run_clippy_lints(&cargo_clippy_path, &counter, 1, num_crates, config.fix))
724                 .collect()
725         }
726     };
727
728     // if we are in --fix mode, don't change the log files, terminate here
729     if config.fix {
730         return;
731     }
732
733     // generate some stats
734     let (stats_formatted, new_stats) = gather_stats(&clippy_warnings);
735
736     // grab crashes/ICEs, save the crate name and the ice message
737     let ices: Vec<(&String, &String)> = clippy_warnings
738         .iter()
739         .filter(|warning| warning.is_ice)
740         .map(|w| (&w.crate_name, &w.message))
741         .collect();
742
743     let mut all_msgs: Vec<String> = clippy_warnings.iter().map(ToString::to_string).collect();
744     all_msgs.sort();
745     all_msgs.push("\n\n\n\nStats:\n".into());
746     all_msgs.push(stats_formatted);
747
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");
752     ices.iter()
753         .for_each(|(cratename, msg)| text.push_str(&format!("{}: '{}'", cratename, msg)));
754
755     println!("Writing logs to {}", config.lintcheck_results_path.display());
756     write(&config.lintcheck_results_path, text).unwrap();
757
758     print_stats(old_stats, new_stats);
759 }
760
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,
765         None => {
766             return HashMap::new();
767         },
768     };
769
770     let lines: Vec<String> = file_content.lines().map(ToString::to_string).collect();
771
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();
775
776     let stats_lines = &lines[start + 1..end];
777
778     stats_lines
779         .iter()
780         .map(|line| {
781             let mut spl = line.split(' ');
782             (
783                 spl.next().unwrap().to_string(),
784                 spl.next().unwrap().parse::<usize>().unwrap(),
785             )
786         })
787         .collect::<HashMap<String, usize>>()
788 }
789
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
793         .iter()
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)>>();
797
798     let mut old_stats_deduped = old_stats;
799     let mut new_stats_deduped = new_stats;
800
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));
805     });
806
807     println!("\nStats:");
808
809     // list all new counts  (key is in new stats but not in old stats)
810     new_stats_deduped
811         .iter()
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);
815         });
816
817     // list all changed counts (key is in both maps but value differs)
818     new_stats_deduped
819         .iter()
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);
824         });
825
826     // list all gone counts (key is in old status but not in new stats)
827     old_stats_deduped
828         .iter()
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);
832         });
833 }
834
835 /// Create necessary directories to run the lintcheck tool.
836 ///
837 /// # Panics
838 ///
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");
844         }
845     });
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");
849         }
850     });
851     std::fs::create_dir(&extract_dir).unwrap_or_else(|err| {
852         if err.kind() != ErrorKind::AlreadyExists {
853             panic!("cannot create crate extraction dir");
854         }
855     });
856 }
857
858 fn get_clap_config<'a>() -> ArgMatches<'a> {
859     App::new("lintcheck")
860         .about("run clippy on a set of crates and check output")
861         .arg(
862             Arg::with_name("only")
863                 .takes_value(true)
864                 .value_name("CRATE")
865                 .long("only")
866                 .help("only process a single crate of the list"),
867         )
868         .arg(
869             Arg::with_name("crates-toml")
870                 .takes_value(true)
871                 .value_name("CRATES-SOURCES-TOML-PATH")
872                 .long("crates-toml")
873                 .help("set the path for a crates.toml where lintcheck should read the sources from"),
874         )
875         .arg(
876             Arg::with_name("threads")
877                 .takes_value(true)
878                 .value_name("N")
879                 .short("j")
880                 .long("jobs")
881                 .help("number of threads to use, 0 automatic choice"),
882         )
883         .arg(
884             Arg::with_name("fix")
885                 .long("--fix")
886                 .help("runs cargo clippy --fix and checks if all suggestions apply"),
887         )
888         .get_matches()
889 }
890
891 /// Returns the path to the Clippy project directory
892 ///
893 /// # Panics
894 ///
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
897 #[must_use]
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 {
904                 continue;
905             }
906         }
907
908         let content = result.unwrap();
909         if content.contains("[package]\nname = \"clippy\"") {
910             return path.to_path_buf();
911         }
912     }
913     panic!("error: Can't determine root of project. Please run inside a Clippy working dir.");
914 }
915
916 #[test]
917 fn lintcheck_test() {
918     let args = [
919         "run",
920         "--target-dir",
921         "lintcheck/target",
922         "--manifest-path",
923         "./lintcheck/Cargo.toml",
924         "--",
925         "--crates-toml",
926         "lintcheck/test_sources.toml",
927     ];
928     let status = std::process::Command::new("cargo")
929         .args(&args)
930         .current_dir("..") // repo root
931         .status();
932     //.output();
933
934     assert!(status.unwrap().success());
935 }