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::{fmt, fs::write, path::PathBuf};
16 use serde::{Deserialize, Serialize};
17 use serde_json::Value;
19 // use this to store the crates when interacting with the crates.toml file
20 #[derive(Debug, Serialize, Deserialize)]
22 crates: HashMap<String, Vec<String>>,
25 // crate data we stored in the toml, can have multiple versions per crate
26 // A single TomlCrate is laster mapped to several CrateSources in that case
29 versions: Vec<String>,
32 // represents an archive we download from crates.io
33 #[derive(Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
39 // represents the extracted sourcecode of a crate
44 // path to the extracted sources that clippy can check
49 struct ClippyWarning {
51 crate_version: String,
59 impl std::fmt::Display for ClippyWarning {
60 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63 r#"{}/{}/{}:{}:{} {} "{}""#,
64 &self.crate_name, &self.crate_version, &self.file, &self.line, &self.column, &self.linttype, &self.message
70 fn download_and_extract(&self) -> Crate {
71 let extract_dir = PathBuf::from("target/crater/crates");
72 let krate_download_dir = PathBuf::from("target/crater/downloads");
74 // url to download the crate from crates.io
76 "https://crates.io/api/v1/crates/{}/{}/download",
77 self.name, self.version
79 println!("Downloading and extracting {} {} from {}", self.name, self.version, url);
80 let _ = std::fs::create_dir("target/crater/");
81 let _ = std::fs::create_dir(&krate_download_dir);
82 let _ = std::fs::create_dir(&extract_dir);
84 let krate_file_path = krate_download_dir.join(format!("{}-{}.crate.tar.gz", &self.name, &self.version));
85 // don't download/extract if we already have done so
86 if !krate_file_path.is_file() {
87 // create a file path to download and write the crate data into
88 let mut krate_dest = std::fs::File::create(&krate_file_path).unwrap();
89 let mut krate_req = ureq::get(&url).call().unwrap().into_reader();
90 // copy the crate into the file
91 std::io::copy(&mut krate_req, &mut krate_dest).unwrap();
94 let ungz_tar = flate2::read::GzDecoder::new(std::fs::File::open(&krate_file_path).unwrap());
95 // extract the tar archive
96 let mut archive = tar::Archive::new(ungz_tar);
97 archive.unpack(&extract_dir).expect("Failed to extract!");
99 // crate is extracted, return a new Krate object which contains the path to the extracted
100 // sources that clippy can check
102 version: self.version.clone(),
103 name: self.name.clone(),
104 path: extract_dir.join(format!("{}-{}/", self.name, self.version)),
110 fn run_clippy_lints(&self, cargo_clippy_path: &PathBuf) -> Vec<ClippyWarning> {
111 println!("Linting {} {}...", &self.name, &self.version);
112 let cargo_clippy_path = std::fs::canonicalize(cargo_clippy_path).unwrap();
114 let shared_target_dir = clippy_project_root().join("target/crater/shared_target_dir/");
116 let all_output = std::process::Command::new(cargo_clippy_path)
117 .env("CARGO_TARGET_DIR", shared_target_dir)
118 // lint warnings will look like this:
119 // src/cargo/ops/cargo_compile.rs:127:35: warning: usage of `FromIterator::from_iter`
122 "--message-format=json",
125 "-Wclippy::pedantic",
128 .current_dir(&self.path)
131 let stdout = String::from_utf8_lossy(&all_output.stdout);
132 let output_lines = stdout.lines();
133 //dbg!(&output_lines);
134 let warnings: Vec<ClippyWarning> = output_lines
136 // get all clippy warnings
137 .filter(|line| line.contains("clippy::"))
138 .map(|json_msg| parse_json_message(json_msg, &self))
145 Command::new("cargo")
148 .expect("Failed to build clippy!");
151 // get a list of CrateSources we want to check from a "crater_crates.toml" file.
152 fn read_crates() -> Vec<CrateSource> {
153 let toml_path = PathBuf::from("clippy_dev/crater_crates.toml");
154 let toml_content: String =
155 std::fs::read_to_string(&toml_path).unwrap_or_else(|_| panic!("Failed to read {}", toml_path.display()));
156 let crate_list: CrateList =
157 toml::from_str(&toml_content).unwrap_or_else(|e| panic!("Failed to parse {}: \n{}", toml_path.display(), e));
158 // parse the hashmap of the toml file into a list of crates
159 let tomlcrates: Vec<TomlCrate> = crate_list
162 .map(|(name, versions)| TomlCrate { name, versions })
165 // flatten TomlCrates into CrateSources (one TomlCrates may represent several versions of a crate =>
166 // multiple Cratesources)
167 let mut crate_sources = Vec::new();
168 tomlcrates.into_iter().for_each(|tk| {
169 tk.versions.iter().for_each(|ver| {
170 crate_sources.push(CrateSource {
171 name: tk.name.clone(),
172 version: ver.to_string(),
179 // extract interesting data from a json lint message
180 fn parse_json_message(json_message: &str, krate: &Crate) -> ClippyWarning {
181 let jmsg: Value = serde_json::from_str(&json_message).unwrap_or_else(|e| panic!("Failed to parse json:\n{:?}", e));
184 crate_name: krate.name.to_string(),
185 crate_version: krate.version.to_string(),
186 file: jmsg["message"]["spans"][0]["file_name"]
190 line: jmsg["message"]["spans"][0]["line_start"]
194 column: jmsg["message"]["spans"][0]["text"][0]["highlight_start"]
198 linttype: jmsg["message"]["code"]["code"].to_string().trim_matches('"').into(),
199 message: jmsg["message"]["message"].to_string().trim_matches('"').into(),
204 pub fn run(clap_config: &ArgMatches) {
205 let cargo_clippy_path: PathBuf = PathBuf::from("target/debug/cargo-clippy");
207 println!("Compiling clippy...");
209 println!("Done compiling");
211 // assert that clippy is found
213 cargo_clippy_path.is_file(),
214 "target/debug/cargo-clippy binary not found! {}",
215 cargo_clippy_path.display()
218 // download and extract the crates, then run clippy on them and collect clippys warnings
219 // flatten into one big list of warnings
221 let clippy_warnings: Vec<ClippyWarning> = if let Some(only_one_crate) = clap_config.value_of("only") {
222 // only check a single
225 .map(|krate| krate.download_and_extract())
226 .filter(|krate| krate.name == only_one_crate)
227 .map(|krate| krate.run_clippy_lints(&cargo_clippy_path))
233 .map(|krate| krate.download_and_extract())
234 .map(|krate| krate.run_clippy_lints(&cargo_clippy_path))
239 // generate some stats:
241 // count lint type occurrences
242 let mut counter: HashMap<&String, usize> = HashMap::new();
245 .for_each(|wrn| *counter.entry(&wrn.linttype).or_insert(0) += 1);
247 // collect into a tupled list for sorting
248 let mut stats: Vec<(&&String, &usize)> = counter.iter().map(|(lint, count)| (lint, count)).collect();
249 // sort by "000{count} {clippy::lintname}"
250 // to not have a lint with 200 and 2 warnings take the same spot
251 stats.sort_by_key(|(lint, count)| format!("{:0>4}, {}", count, lint));
253 let stats_formatted: String = stats
255 .map(|(lint, count)| format!("{} {}\n", lint, count))
256 .collect::<String>();
258 let mut all_msgs: Vec<String> = clippy_warnings.iter().map(|warning| warning.to_string()).collect();
260 all_msgs.push("\n\n\n\nStats\n\n".into());
261 all_msgs.push(stats_formatted);
263 // save the text into mini-crater/logs.txt
264 let text = all_msgs.join("");
265 write("mini-crater/logs.txt", text).unwrap();