]> git.lizzy.rs Git - rust.git/blob - clippy_dev/src/lintcheck.rs
Auto merge of #6834 - hyd-dev:clippy-args, r=phansch,flip1995,oli-obk
[rust.git] / clippy_dev / src / lintcheck.rs
1 // Run clippy on a fixed set of crates and collect the warnings.
2 // This helps observing the impact clippy changs have on a set of real-world code.
3 //
4 // When a new lint is introduced, we can search the results for new warnings and check for false
5 // positives.
6
7 #![cfg(feature = "lintcheck")]
8 #![allow(clippy::filter_map, clippy::collapsible_else_if)]
9
10 use crate::clippy_project_root;
11
12 use std::process::Command;
13 use std::sync::atomic::{AtomicUsize, Ordering};
14 use std::{collections::HashMap, io::ErrorKind};
15 use std::{
16     env, fmt,
17     fs::write,
18     path::{Path, PathBuf},
19 };
20
21 use clap::ArgMatches;
22 use rayon::prelude::*;
23 use serde::{Deserialize, Serialize};
24 use serde_json::Value;
25
26 const CLIPPY_DRIVER_PATH: &str = "target/debug/clippy-driver";
27 const CARGO_CLIPPY_PATH: &str = "target/debug/cargo-clippy";
28
29 const LINTCHECK_DOWNLOADS: &str = "target/lintcheck/downloads";
30 const LINTCHECK_SOURCES: &str = "target/lintcheck/sources";
31
32 /// List of sources to check, loaded from a .toml file
33 #[derive(Debug, Serialize, Deserialize)]
34 struct SourceList {
35     crates: HashMap<String, TomlCrate>,
36 }
37
38 /// A crate source stored inside the .toml
39 /// will be translated into on one of the `CrateSource` variants
40 #[derive(Debug, Serialize, Deserialize)]
41 struct TomlCrate {
42     name: String,
43     versions: Option<Vec<String>>,
44     git_url: Option<String>,
45     git_hash: Option<String>,
46     path: Option<String>,
47     options: Option<Vec<String>>,
48 }
49
50 /// Represents an archive we download from crates.io, or a git repo, or a local repo/folder
51 /// Once processed (downloaded/extracted/cloned/copied...), this will be translated into a `Crate`
52 #[derive(Debug, Serialize, Deserialize, Eq, Hash, PartialEq, Ord, PartialOrd)]
53 enum CrateSource {
54     CratesIo {
55         name: String,
56         version: String,
57         options: Option<Vec<String>>,
58     },
59     Git {
60         name: String,
61         url: String,
62         commit: String,
63         options: Option<Vec<String>>,
64     },
65     Path {
66         name: String,
67         path: PathBuf,
68         options: Option<Vec<String>>,
69     },
70 }
71
72 /// Represents the actual source code of a crate that we ran "cargo clippy" on
73 #[derive(Debug)]
74 struct Crate {
75     version: String,
76     name: String,
77     // path to the extracted sources that clippy can check
78     path: PathBuf,
79     options: Option<Vec<String>>,
80 }
81
82 /// A single warning that clippy issued while checking a `Crate`
83 #[derive(Debug)]
84 struct ClippyWarning {
85     crate_name: String,
86     crate_version: String,
87     file: String,
88     line: String,
89     column: String,
90     linttype: String,
91     message: String,
92     is_ice: bool,
93 }
94
95 impl std::fmt::Display for ClippyWarning {
96     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97         writeln!(
98             f,
99             r#"target/lintcheck/sources/{}-{}/{}:{}:{} {} "{}""#,
100             &self.crate_name, &self.crate_version, &self.file, &self.line, &self.column, &self.linttype, &self.message
101         )
102     }
103 }
104
105 impl CrateSource {
106     /// Makes the sources available on the disk for clippy to check.
107     /// Clones a git repo and checks out the specified commit or downloads a crate from crates.io or
108     /// copies a local folder
109     fn download_and_extract(&self) -> Crate {
110         match self {
111             CrateSource::CratesIo { name, version, options } => {
112                 let extract_dir = PathBuf::from(LINTCHECK_SOURCES);
113                 let krate_download_dir = PathBuf::from(LINTCHECK_DOWNLOADS);
114
115                 // url to download the crate from crates.io
116                 let url = format!("https://crates.io/api/v1/crates/{}/{}/download", name, version);
117                 println!("Downloading and extracting {} {} from {}", name, version, url);
118                 create_dirs(&krate_download_dir, &extract_dir);
119
120                 let krate_file_path = krate_download_dir.join(format!("{}-{}.crate.tar.gz", name, version));
121                 // don't download/extract if we already have done so
122                 if !krate_file_path.is_file() {
123                     // create a file path to download and write the crate data into
124                     let mut krate_dest = std::fs::File::create(&krate_file_path).unwrap();
125                     let mut krate_req = ureq::get(&url).call().unwrap().into_reader();
126                     // copy the crate into the file
127                     std::io::copy(&mut krate_req, &mut krate_dest).unwrap();
128
129                     // unzip the tarball
130                     let ungz_tar = flate2::read::GzDecoder::new(std::fs::File::open(&krate_file_path).unwrap());
131                     // extract the tar archive
132                     let mut archive = tar::Archive::new(ungz_tar);
133                     archive.unpack(&extract_dir).expect("Failed to extract!");
134                 }
135                 // crate is extracted, return a new Krate object which contains the path to the extracted
136                 // sources that clippy can check
137                 Crate {
138                     version: version.clone(),
139                     name: name.clone(),
140                     path: extract_dir.join(format!("{}-{}/", name, version)),
141                     options: options.clone(),
142                 }
143             },
144             CrateSource::Git {
145                 name,
146                 url,
147                 commit,
148                 options,
149             } => {
150                 let repo_path = {
151                     let mut repo_path = PathBuf::from(LINTCHECK_SOURCES);
152                     // add a -git suffix in case we have the same crate from crates.io and a git repo
153                     repo_path.push(format!("{}-git", name));
154                     repo_path
155                 };
156                 // clone the repo if we have not done so
157                 if !repo_path.is_dir() {
158                     println!("Cloning {} and checking out {}", url, commit);
159                     if !Command::new("git")
160                         .arg("clone")
161                         .arg(url)
162                         .arg(&repo_path)
163                         .status()
164                         .expect("Failed to clone git repo!")
165                         .success()
166                     {
167                         eprintln!("Failed to clone {} into {}", url, repo_path.display())
168                     }
169                 }
170                 // check out the commit/branch/whatever
171                 if !Command::new("git")
172                     .arg("checkout")
173                     .arg(commit)
174                     .current_dir(&repo_path)
175                     .status()
176                     .expect("Failed to check out commit")
177                     .success()
178                 {
179                     eprintln!("Failed to checkout {} of repo at {}", commit, repo_path.display())
180                 }
181
182                 Crate {
183                     version: commit.clone(),
184                     name: name.clone(),
185                     path: repo_path,
186                     options: options.clone(),
187                 }
188             },
189             CrateSource::Path { name, path, options } => {
190                 use fs_extra::dir;
191
192                 // simply copy the entire directory into our target dir
193                 let copy_dest = PathBuf::from(format!("{}/", LINTCHECK_SOURCES));
194
195                 // the source path of the crate we copied,  ${copy_dest}/crate_name
196                 let crate_root = copy_dest.join(name); // .../crates/local_crate
197
198                 if crate_root.exists() {
199                     println!(
200                         "Not copying {} to {}, destination already exists",
201                         path.display(),
202                         crate_root.display()
203                     );
204                 } else {
205                     println!("Copying {} to {}", path.display(), copy_dest.display());
206
207                     dir::copy(path, &copy_dest, &dir::CopyOptions::new()).unwrap_or_else(|_| {
208                         panic!("Failed to copy from {}, to  {}", path.display(), crate_root.display())
209                     });
210                 }
211
212                 Crate {
213                     version: String::from("local"),
214                     name: name.clone(),
215                     path: crate_root,
216                     options: options.clone(),
217                 }
218             },
219         }
220     }
221 }
222
223 impl Crate {
224     /// Run `cargo clippy` on the `Crate` and collect and return all the lint warnings that clippy
225     /// issued
226     fn run_clippy_lints(
227         &self,
228         cargo_clippy_path: &Path,
229         target_dir_index: &AtomicUsize,
230         thread_limit: usize,
231         total_crates_to_lint: usize,
232         fix: bool,
233     ) -> Vec<ClippyWarning> {
234         // advance the atomic index by one
235         let index = target_dir_index.fetch_add(1, Ordering::SeqCst);
236         // "loop" the index within 0..thread_limit
237         let thread_index = index % thread_limit;
238         let perc = (index * 100) / total_crates_to_lint;
239
240         if thread_limit == 1 {
241             println!(
242                 "{}/{} {}% Linting {} {}",
243                 index, total_crates_to_lint, perc, &self.name, &self.version
244             );
245         } else {
246             println!(
247                 "{}/{} {}% Linting {} {} in target dir {:?}",
248                 index, total_crates_to_lint, perc, &self.name, &self.version, thread_index
249             );
250         }
251
252         let cargo_clippy_path = std::fs::canonicalize(cargo_clippy_path).unwrap();
253
254         let shared_target_dir = clippy_project_root().join("target/lintcheck/shared_target_dir");
255
256         let mut args = if fix {
257             vec![
258                 "-Zunstable-options",
259                 "--fix",
260                 "-Zunstable-options",
261                 "--allow-no-vcs",
262                 "--",
263                 "--cap-lints=warn",
264             ]
265         } else {
266             vec!["--", "--message-format=json", "--", "--cap-lints=warn"]
267         };
268
269         if let Some(options) = &self.options {
270             for opt in options {
271                 args.push(opt);
272             }
273         } else {
274             args.extend(&["-Wclippy::pedantic", "-Wclippy::cargo"])
275         }
276
277         let all_output = std::process::Command::new(&cargo_clippy_path)
278             // use the looping index to create individual target dirs
279             .env(
280                 "CARGO_TARGET_DIR",
281                 shared_target_dir.join(format!("_{:?}", thread_index)),
282             )
283             // lint warnings will look like this:
284             // src/cargo/ops/cargo_compile.rs:127:35: warning: usage of `FromIterator::from_iter`
285             .args(&args)
286             .current_dir(&self.path)
287             .output()
288             .unwrap_or_else(|error| {
289                 panic!(
290                     "Encountered error:\n{:?}\ncargo_clippy_path: {}\ncrate path:{}\n",
291                     error,
292                     &cargo_clippy_path.display(),
293                     &self.path.display()
294                 );
295             });
296         let stdout = String::from_utf8_lossy(&all_output.stdout);
297         let stderr = String::from_utf8_lossy(&all_output.stderr);
298
299         if fix {
300             if let Some(stderr) = stderr
301                 .lines()
302                 .find(|line| line.contains("failed to automatically apply fixes suggested by rustc to crate"))
303             {
304                 let subcrate = &stderr[63..];
305                 println!(
306                     "ERROR: failed to apply some suggetion to {} / to (sub)crate {}",
307                     self.name, subcrate
308                 );
309             }
310             // fast path, we don't need the warnings anyway
311             return Vec::new();
312         }
313
314         let output_lines = stdout.lines();
315         let warnings: Vec<ClippyWarning> = output_lines
316             .into_iter()
317             // get all clippy warnings and ICEs
318             .filter(|line| filter_clippy_warnings(&line))
319             .map(|json_msg| parse_json_message(json_msg, &self))
320             .collect();
321
322         warnings
323     }
324 }
325
326 #[derive(Debug)]
327 struct LintcheckConfig {
328     // max number of jobs to spawn (default 1)
329     max_jobs: usize,
330     // we read the sources to check from here
331     sources_toml_path: PathBuf,
332     // we save the clippy lint results here
333     lintcheck_results_path: PathBuf,
334     // whether to just run --fix and not collect all the warnings
335     fix: bool,
336 }
337
338 impl LintcheckConfig {
339     fn from_clap(clap_config: &ArgMatches) -> Self {
340         // first, check if we got anything passed via the LINTCHECK_TOML env var,
341         // if not, ask clap if we got any value for --crates-toml  <foo>
342         // if not, use the default "clippy_dev/lintcheck_crates.toml"
343         let sources_toml = env::var("LINTCHECK_TOML").unwrap_or_else(|_| {
344             clap_config
345                 .value_of("crates-toml")
346                 .clone()
347                 .unwrap_or("clippy_dev/lintcheck_crates.toml")
348                 .to_string()
349         });
350
351         let sources_toml_path = PathBuf::from(sources_toml);
352
353         // for the path where we save the lint results, get the filename without extension (so for
354         // wasd.toml, use "wasd"...)
355         let filename: PathBuf = sources_toml_path.file_stem().unwrap().into();
356         let lintcheck_results_path = PathBuf::from(format!("lintcheck-logs/{}_logs.txt", filename.display()));
357
358         // look at the --threads arg, if 0 is passed, ask rayon rayon how many threads it would spawn and
359         // use half of that for the physical core count
360         // by default use a single thread
361         let max_jobs = match clap_config.value_of("threads") {
362             Some(threads) => {
363                 let threads: usize = threads
364                     .parse()
365                     .unwrap_or_else(|_| panic!("Failed to parse '{}' to a digit", threads));
366                 if threads == 0 {
367                     // automatic choice
368                     // Rayon seems to return thread count so half that for core count
369                     (rayon::current_num_threads() / 2) as usize
370                 } else {
371                     threads
372                 }
373             },
374             // no -j passed, use a single thread
375             None => 1,
376         };
377         let fix: bool = clap_config.is_present("fix");
378
379         LintcheckConfig {
380             max_jobs,
381             sources_toml_path,
382             lintcheck_results_path,
383             fix,
384         }
385     }
386 }
387
388 /// takes a single json-formatted clippy warnings and returns true (we are interested in that line)
389 /// or false (we aren't)
390 fn filter_clippy_warnings(line: &str) -> bool {
391     // we want to collect ICEs because clippy might have crashed.
392     // these are summarized later
393     if line.contains("internal compiler error: ") {
394         return true;
395     }
396     // in general, we want all clippy warnings
397     // however due to some kind of bug, sometimes there are absolute paths
398     // to libcore files inside the message
399     // or we end up with cargo-metadata output (https://github.com/rust-lang/rust-clippy/issues/6508)
400
401     // filter out these message to avoid unnecessary noise in the logs
402     if line.contains("clippy::")
403         && !(line.contains("could not read cargo metadata")
404             || (line.contains(".rustup") && line.contains("toolchains")))
405     {
406         return true;
407     }
408     false
409 }
410
411 /// Builds clippy inside the repo to make sure we have a clippy executable we can use.
412 fn build_clippy() {
413     let status = Command::new("cargo")
414         .arg("build")
415         .status()
416         .expect("Failed to build clippy!");
417     if !status.success() {
418         eprintln!("Error: Failed to compile Clippy!");
419         std::process::exit(1);
420     }
421 }
422
423 /// Read a `toml` file and return a list of `CrateSources` that we want to check with clippy
424 fn read_crates(toml_path: &Path) -> Vec<CrateSource> {
425     let toml_content: String =
426         std::fs::read_to_string(&toml_path).unwrap_or_else(|_| panic!("Failed to read {}", toml_path.display()));
427     let crate_list: SourceList =
428         toml::from_str(&toml_content).unwrap_or_else(|e| panic!("Failed to parse {}: \n{}", toml_path.display(), e));
429     // parse the hashmap of the toml file into a list of crates
430     let tomlcrates: Vec<TomlCrate> = crate_list
431         .crates
432         .into_iter()
433         .map(|(_cratename, tomlcrate)| tomlcrate)
434         .collect();
435
436     // flatten TomlCrates into CrateSources (one TomlCrates may represent several versions of a crate =>
437     // multiple Cratesources)
438     let mut crate_sources = Vec::new();
439     tomlcrates.into_iter().for_each(|tk| {
440         if let Some(ref path) = tk.path {
441             crate_sources.push(CrateSource::Path {
442                 name: tk.name.clone(),
443                 path: PathBuf::from(path),
444                 options: tk.options.clone(),
445             });
446         }
447
448         // if we have multiple versions, save each one
449         if let Some(ref versions) = tk.versions {
450             versions.iter().for_each(|ver| {
451                 crate_sources.push(CrateSource::CratesIo {
452                     name: tk.name.clone(),
453                     version: ver.to_string(),
454                     options: tk.options.clone(),
455                 });
456             })
457         }
458         // otherwise, we should have a git source
459         if tk.git_url.is_some() && tk.git_hash.is_some() {
460             crate_sources.push(CrateSource::Git {
461                 name: tk.name.clone(),
462                 url: tk.git_url.clone().unwrap(),
463                 commit: tk.git_hash.clone().unwrap(),
464                 options: tk.options.clone(),
465             });
466         }
467         // if we have a version as well as a git data OR only one git data, something is funky
468         if tk.versions.is_some() && (tk.git_url.is_some() || tk.git_hash.is_some())
469             || tk.git_hash.is_some() != tk.git_url.is_some()
470         {
471             eprintln!("tomlkrate: {:?}", tk);
472             if tk.git_hash.is_some() != tk.git_url.is_some() {
473                 panic!("Error: Encountered TomlCrate with only one of git_hash and git_url!");
474             }
475             if tk.path.is_some() && (tk.git_hash.is_some() || tk.versions.is_some()) {
476                 panic!("Error: TomlCrate can only have one of 'git_.*', 'version' or 'path' fields");
477             }
478             unreachable!("Failed to translate TomlCrate into CrateSource!");
479         }
480     });
481     // sort the crates
482     crate_sources.sort();
483
484     crate_sources
485 }
486
487 /// Parse the json output of clippy and return a `ClippyWarning`
488 fn parse_json_message(json_message: &str, krate: &Crate) -> ClippyWarning {
489     let jmsg: Value = serde_json::from_str(&json_message).unwrap_or_else(|e| panic!("Failed to parse json:\n{:?}", e));
490
491     ClippyWarning {
492         crate_name: krate.name.to_string(),
493         crate_version: krate.version.to_string(),
494         file: jmsg["message"]["spans"][0]["file_name"]
495             .to_string()
496             .trim_matches('"')
497             .into(),
498         line: jmsg["message"]["spans"][0]["line_start"]
499             .to_string()
500             .trim_matches('"')
501             .into(),
502         column: jmsg["message"]["spans"][0]["text"][0]["highlight_start"]
503             .to_string()
504             .trim_matches('"')
505             .into(),
506         linttype: jmsg["message"]["code"]["code"].to_string().trim_matches('"').into(),
507         message: jmsg["message"]["message"].to_string().trim_matches('"').into(),
508         is_ice: json_message.contains("internal compiler error: "),
509     }
510 }
511
512 /// Generate a short list of occuring lints-types and their count
513 fn gather_stats(clippy_warnings: &[ClippyWarning]) -> (String, HashMap<&String, usize>) {
514     // count lint type occurrences
515     let mut counter: HashMap<&String, usize> = HashMap::new();
516     clippy_warnings
517         .iter()
518         .for_each(|wrn| *counter.entry(&wrn.linttype).or_insert(0) += 1);
519
520     // collect into a tupled list for sorting
521     let mut stats: Vec<(&&String, &usize)> = counter.iter().map(|(lint, count)| (lint, count)).collect();
522     // sort by "000{count} {clippy::lintname}"
523     // to not have a lint with 200 and 2 warnings take the same spot
524     stats.sort_by_key(|(lint, count)| format!("{:0>4}, {}", count, lint));
525
526     let stats_string = stats
527         .iter()
528         .map(|(lint, count)| format!("{} {}\n", lint, count))
529         .collect::<String>();
530
531     (stats_string, counter)
532 }
533
534 /// check if the latest modification of the logfile is older than the modification date of the
535 /// clippy binary, if this is true, we should clean the lintchec shared target directory and recheck
536 fn lintcheck_needs_rerun(lintcheck_logs_path: &Path) -> bool {
537     if !lintcheck_logs_path.exists() {
538         return true;
539     }
540
541     let clippy_modified: std::time::SystemTime = {
542         let mut times = [CLIPPY_DRIVER_PATH, CARGO_CLIPPY_PATH].iter().map(|p| {
543             std::fs::metadata(p)
544                 .expect("failed to get metadata of file")
545                 .modified()
546                 .expect("failed to get modification date")
547         });
548         // the oldest modification of either of the binaries
549         std::cmp::max(times.next().unwrap(), times.next().unwrap())
550     };
551
552     let logs_modified: std::time::SystemTime = std::fs::metadata(lintcheck_logs_path)
553         .expect("failed to get metadata of file")
554         .modified()
555         .expect("failed to get modification date");
556
557     // time is represented in seconds since X
558     // logs_modified 2 and clippy_modified 5 means clippy binary is older and we need to recheck
559     logs_modified < clippy_modified
560 }
561
562 /// lintchecks `main()` function
563 ///
564 /// # Panics
565 ///
566 /// This function panics if the clippy binaries don't exist.
567 pub fn run(clap_config: &ArgMatches) {
568     let config = LintcheckConfig::from_clap(clap_config);
569
570     println!("Compiling clippy...");
571     build_clippy();
572     println!("Done compiling");
573
574     // if the clippy bin is newer than our logs, throw away target dirs to force clippy to
575     // refresh the logs
576     if lintcheck_needs_rerun(&config.lintcheck_results_path) {
577         let shared_target_dir = "target/lintcheck/shared_target_dir";
578         // if we get an Err here, the shared target dir probably does simply not exist
579         if let Ok(metadata) = std::fs::metadata(&shared_target_dir) {
580             if metadata.is_dir() {
581                 println!("Clippy is newer than lint check logs, clearing lintcheck shared target dir...");
582                 std::fs::remove_dir_all(&shared_target_dir)
583                     .expect("failed to remove target/lintcheck/shared_target_dir");
584             }
585         }
586     }
587
588     let cargo_clippy_path: PathBuf = PathBuf::from(CARGO_CLIPPY_PATH)
589         .canonicalize()
590         .expect("failed to canonicalize path to clippy binary");
591
592     // assert that clippy is found
593     assert!(
594         cargo_clippy_path.is_file(),
595         "target/debug/cargo-clippy binary not found! {}",
596         cargo_clippy_path.display()
597     );
598
599     let clippy_ver = std::process::Command::new(CARGO_CLIPPY_PATH)
600         .arg("--version")
601         .output()
602         .map(|o| String::from_utf8_lossy(&o.stdout).into_owned())
603         .expect("could not get clippy version!");
604
605     // download and extract the crates, then run clippy on them and collect clippys warnings
606     // flatten into one big list of warnings
607
608     let crates = read_crates(&config.sources_toml_path);
609     let old_stats = read_stats_from_file(&config.lintcheck_results_path);
610
611     let counter = AtomicUsize::new(1);
612
613     let clippy_warnings: Vec<ClippyWarning> = if let Some(only_one_crate) = clap_config.value_of("only") {
614         // if we don't have the specified crate in the .toml, throw an error
615         if !crates.iter().any(|krate| {
616             let name = match krate {
617                 CrateSource::CratesIo { name, .. } | CrateSource::Git { name, .. } | CrateSource::Path { name, .. } => {
618                     name
619                 },
620             };
621             name == only_one_crate
622         }) {
623             eprintln!(
624                 "ERROR: could not find crate '{}' in clippy_dev/lintcheck_crates.toml",
625                 only_one_crate
626             );
627             std::process::exit(1);
628         }
629
630         // only check a single crate that was passed via cmdline
631         crates
632             .into_iter()
633             .map(|krate| krate.download_and_extract())
634             .filter(|krate| krate.name == only_one_crate)
635             .flat_map(|krate| krate.run_clippy_lints(&cargo_clippy_path, &AtomicUsize::new(0), 1, 1, config.fix))
636             .collect()
637     } else {
638         if config.max_jobs > 1 {
639             // run parallel with rayon
640
641             // Ask rayon for thread count. Assume that half of that is the number of physical cores
642             // Use one target dir for each core so that we can run N clippys in parallel.
643             // We need to use different target dirs because cargo would lock them for a single build otherwise,
644             // killing the parallelism. However this also means that deps will only be reused half/a
645             // quarter of the time which might result in a longer wall clock runtime
646
647             // This helps when we check many small crates with dep-trees that don't have a lot of branches in
648             // order to achive some kind of parallelism
649
650             // by default, use a single thread
651             let num_cpus = config.max_jobs;
652             let num_crates = crates.len();
653
654             // check all crates (default)
655             crates
656                 .into_par_iter()
657                 .map(|krate| krate.download_and_extract())
658                 .flat_map(|krate| {
659                     krate.run_clippy_lints(&cargo_clippy_path, &counter, num_cpus, num_crates, config.fix)
660                 })
661                 .collect()
662         } else {
663             // run sequential
664             let num_crates = crates.len();
665             crates
666                 .into_iter()
667                 .map(|krate| krate.download_and_extract())
668                 .flat_map(|krate| krate.run_clippy_lints(&cargo_clippy_path, &counter, 1, num_crates, config.fix))
669                 .collect()
670         }
671     };
672
673     // if we are in --fix mode, don't change the log files, terminate here
674     if config.fix {
675         return;
676     }
677
678     // generate some stats
679     let (stats_formatted, new_stats) = gather_stats(&clippy_warnings);
680
681     // grab crashes/ICEs, save the crate name and the ice message
682     let ices: Vec<(&String, &String)> = clippy_warnings
683         .iter()
684         .filter(|warning| warning.is_ice)
685         .map(|w| (&w.crate_name, &w.message))
686         .collect();
687
688     let mut all_msgs: Vec<String> = clippy_warnings.iter().map(ToString::to_string).collect();
689     all_msgs.sort();
690     all_msgs.push("\n\n\n\nStats:\n".into());
691     all_msgs.push(stats_formatted);
692
693     // save the text into lintcheck-logs/logs.txt
694     let mut text = clippy_ver; // clippy version number on top
695     text.push_str(&format!("\n{}", all_msgs.join("")));
696     text.push_str("ICEs:\n");
697     ices.iter()
698         .for_each(|(cratename, msg)| text.push_str(&format!("{}: '{}'", cratename, msg)));
699
700     println!("Writing logs to {}", config.lintcheck_results_path.display());
701     write(&config.lintcheck_results_path, text).unwrap();
702
703     print_stats(old_stats, new_stats);
704 }
705
706 /// read the previous stats from the lintcheck-log file
707 fn read_stats_from_file(file_path: &Path) -> HashMap<String, usize> {
708     let file_content: String = match std::fs::read_to_string(file_path).ok() {
709         Some(content) => content,
710         None => {
711             return HashMap::new();
712         },
713     };
714
715     let lines: Vec<String> = file_content.lines().map(ToString::to_string).collect();
716
717     // search for the beginning "Stats:" and the end "ICEs:" of the section we want
718     let start = lines.iter().position(|line| line == "Stats:").unwrap();
719     let end = lines.iter().position(|line| line == "ICEs:").unwrap();
720
721     let stats_lines = &lines[start + 1..end];
722
723     stats_lines
724         .iter()
725         .map(|line| {
726             let mut spl = line.split(' ');
727             (
728                 spl.next().unwrap().to_string(),
729                 spl.next().unwrap().parse::<usize>().unwrap(),
730             )
731         })
732         .collect::<HashMap<String, usize>>()
733 }
734
735 /// print how lint counts changed between runs
736 fn print_stats(old_stats: HashMap<String, usize>, new_stats: HashMap<&String, usize>) {
737     let same_in_both_hashmaps = old_stats
738         .iter()
739         .filter(|(old_key, old_val)| new_stats.get::<&String>(&old_key) == Some(old_val))
740         .map(|(k, v)| (k.to_string(), *v))
741         .collect::<Vec<(String, usize)>>();
742
743     let mut old_stats_deduped = old_stats;
744     let mut new_stats_deduped = new_stats;
745
746     // remove duplicates from both hashmaps
747     same_in_both_hashmaps.iter().for_each(|(k, v)| {
748         assert!(old_stats_deduped.remove(k) == Some(*v));
749         assert!(new_stats_deduped.remove(k) == Some(*v));
750     });
751
752     println!("\nStats:");
753
754     // list all new counts  (key is in new stats but not in old stats)
755     new_stats_deduped
756         .iter()
757         .filter(|(new_key, _)| old_stats_deduped.get::<str>(&new_key).is_none())
758         .for_each(|(new_key, new_value)| {
759             println!("{} 0 => {}", new_key, new_value);
760         });
761
762     // list all changed counts (key is in both maps but value differs)
763     new_stats_deduped
764         .iter()
765         .filter(|(new_key, _new_val)| old_stats_deduped.get::<str>(&new_key).is_some())
766         .for_each(|(new_key, new_val)| {
767             let old_val = old_stats_deduped.get::<str>(&new_key).unwrap();
768             println!("{} {} => {}", new_key, old_val, new_val);
769         });
770
771     // list all gone counts (key is in old status but not in new stats)
772     old_stats_deduped
773         .iter()
774         .filter(|(old_key, _)| new_stats_deduped.get::<&String>(&old_key).is_none())
775         .for_each(|(old_key, old_value)| {
776             println!("{} {} => 0", old_key, old_value);
777         });
778 }
779
780 /// Create necessary directories to run the lintcheck tool.
781 ///
782 /// # Panics
783 ///
784 /// This function panics if creating one of the dirs fails.
785 fn create_dirs(krate_download_dir: &Path, extract_dir: &Path) {
786     std::fs::create_dir("target/lintcheck/").unwrap_or_else(|err| {
787         if err.kind() != ErrorKind::AlreadyExists {
788             panic!("cannot create lintcheck target dir");
789         }
790     });
791     std::fs::create_dir(&krate_download_dir).unwrap_or_else(|err| {
792         if err.kind() != ErrorKind::AlreadyExists {
793             panic!("cannot create crate download dir");
794         }
795     });
796     std::fs::create_dir(&extract_dir).unwrap_or_else(|err| {
797         if err.kind() != ErrorKind::AlreadyExists {
798             panic!("cannot create crate extraction dir");
799         }
800     });
801 }
802
803 #[test]
804 fn lintcheck_test() {
805     let args = [
806         "run",
807         "--target-dir",
808         "clippy_dev/target",
809         "--package",
810         "clippy_dev",
811         "--bin",
812         "clippy_dev",
813         "--manifest-path",
814         "clippy_dev/Cargo.toml",
815         "--features",
816         "lintcheck",
817         "--",
818         "lintcheck",
819         "--crates-toml",
820         "clippy_dev/test_sources.toml",
821     ];
822     let status = std::process::Command::new("cargo")
823         .args(&args)
824         .current_dir("../" /* repo root */)
825         .status();
826
827     assert!(status.unwrap().success());
828 }