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.
4 // When a new lint is introduced, we can search the results for new warnings and check for false
7 #![allow(clippy::filter_map)]
9 use crate::clippy_project_root;
11 use std::collections::HashMap;
12 use std::process::Command;
13 use std::{fs::write, path::PathBuf};
15 use serde::{Deserialize, Serialize};
17 // use this to store the crates when interacting with the crates.toml file
18 #[derive(Debug, Serialize, Deserialize)]
20 crates: HashMap<String, Vec<String>>,
23 // crate data we stored in the toml, can have multiple versions per crate
24 // A single TomlCrate is laster mapped to several CrateSources in that case
27 versions: Vec<String>,
30 // represents an archive we download from crates.io
31 #[derive(Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
37 // represents the extracted sourcecode of a crate
42 // path to the extracted sources that clippy can check
47 fn download_and_extract(&self) -> Crate {
48 let extract_dir = PathBuf::from("target/crater/crates");
49 let krate_download_dir = PathBuf::from("target/crater/downloads");
51 // url to download the crate from crates.io
53 "https://crates.io/api/v1/crates/{}/{}/download",
54 self.name, self.version
56 println!("Downloading and extracting {} {} from {}", self.name, self.version, url);
57 let _ = std::fs::create_dir("target/crater/");
58 let _ = std::fs::create_dir(&krate_download_dir);
59 let _ = std::fs::create_dir(&extract_dir);
61 let krate_file_path = krate_download_dir.join(format!("{}-{}.crate.tar.gz", &self.name, &self.version));
62 // don't download/extract if we already have done so
63 if !krate_file_path.is_file() {
64 // create a file path to download and write the crate data into
65 let mut krate_dest = std::fs::File::create(&krate_file_path).unwrap();
66 let mut krate_req = ureq::get(&url).call().unwrap().into_reader();
67 // copy the crate into the file
68 std::io::copy(&mut krate_req, &mut krate_dest).unwrap();
71 let ungz_tar = flate2::read::GzDecoder::new(std::fs::File::open(&krate_file_path).unwrap());
72 // extract the tar archive
73 let mut archive = tar::Archive::new(ungz_tar);
74 archive.unpack(&extract_dir).expect("Failed to extract!");
76 // crate is extracted, return a new Krate object which contains the path to the extracted
77 // sources that clippy can check
79 version: self.version.clone(),
80 name: self.name.clone(),
81 path: extract_dir.join(format!("{}-{}/", self.name, self.version)),
87 fn run_clippy_lints(&self, cargo_clippy_path: &PathBuf) -> Vec<String> {
88 println!("Linting {} {}...", &self.name, &self.version);
89 let cargo_clippy_path = std::fs::canonicalize(cargo_clippy_path).unwrap();
91 let shared_target_dir = clippy_project_root().join("target/crater/shared_target_dir/");
93 let all_output = std::process::Command::new(cargo_clippy_path)
94 .env("CARGO_TARGET_DIR", shared_target_dir)
95 // lint warnings will look like this:
96 // src/cargo/ops/cargo_compile.rs:127:35: warning: usage of `FromIterator::from_iter`
99 "--message-format=short",
102 "-Wclippy::pedantic",
105 .current_dir(&self.path)
108 let stderr = String::from_utf8_lossy(&all_output.stderr);
109 let output_lines = stderr.lines();
110 let mut output: Vec<String> = output_lines
112 .filter(|line| line.contains(": warning: "))
113 // prefix with the crate name and version
114 // cargo-0.49.0/src/cargo/ops/cargo_compile.rs:127:35: warning: usage of `FromIterator::from_iter`
115 .map(|line| format!("{}-{}/{}", self.name, self.version, line))
116 // remove the "warning: "
118 let remove_pat = "warning: ";
121 .expect("clippy output did not contain \"warning: \"");
122 let mut new = line[0..pos].to_string();
123 new.push_str(&line[pos + remove_pat.len()..]);
129 // sort messages alphabetically to avoid noise in the logs
136 Command::new("cargo")
139 .expect("Failed to build clippy!");
142 // get a list of CrateSources we want to check from a "crater_crates.toml" file.
143 fn read_crates() -> Vec<CrateSource> {
144 let toml_path = PathBuf::from("clippy_dev/crater_crates.toml");
145 let toml_content: String =
146 std::fs::read_to_string(&toml_path).unwrap_or_else(|_| panic!("Failed to read {}", toml_path.display()));
147 let crate_list: CrateList =
148 toml::from_str(&toml_content).unwrap_or_else(|e| panic!("Failed to parse {}: \n{}", toml_path.display(), e));
149 // parse the hashmap of the toml file into a list of crates
150 let tomlcrates: Vec<TomlCrate> = crate_list
153 .map(|(name, versions)| TomlCrate { name, versions })
156 // flatten TomlCrates into CrateSources (one TomlCrates may represent several versions of a crate =>
157 // multiple Cratesources)
158 let mut crate_sources = Vec::new();
159 tomlcrates.into_iter().for_each(|tk| {
160 tk.versions.iter().for_each(|ver| {
161 crate_sources.push(CrateSource {
162 name: tk.name.clone(),
163 version: ver.to_string(),
172 let cargo_clippy_path: PathBuf = PathBuf::from("target/debug/cargo-clippy");
174 println!("Compiling clippy...");
176 println!("Done compiling");
178 // assert that clippy is found
180 cargo_clippy_path.is_file(),
181 "target/debug/cargo-clippy binary not found! {}",
182 cargo_clippy_path.display()
185 // download and extract the crates, then run clippy on them and collect clippys warnings
187 let clippy_lint_results: Vec<Vec<String>> = read_crates()
189 .map(|krate| krate.download_and_extract())
190 .map(|krate| krate.run_clippy_lints(&cargo_clippy_path))
193 let mut all_warnings: Vec<String> = clippy_lint_results.into_iter().flatten().collect();
196 // save the text into mini-crater/logs.txt
197 let text = all_warnings.join("");
198 write("mini-crater/logs.txt", text).unwrap();