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 #![cfg(feature = "lintcheck")]
8 #![allow(clippy::filter_map)]
10 use crate::clippy_project_root;
12 use std::collections::HashMap;
13 use std::process::Command;
14 use std::{fmt, fs::write, path::PathBuf};
17 use serde::{Deserialize, Serialize};
18 use serde_json::Value;
20 // use this to store the crates when interacting with the crates.toml file
21 #[derive(Debug, Serialize, Deserialize)]
23 crates: HashMap<String, Vec<String>>,
26 // crate data we stored in the toml, can have multiple versions per crate
27 // A single TomlCrate is laster mapped to several CrateSources in that case
30 versions: Vec<String>,
33 // represents an archive we download from crates.io
34 #[derive(Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
40 // represents the extracted sourcecode of a crate
45 // path to the extracted sources that clippy can check
50 struct ClippyWarning {
52 crate_version: String,
60 impl std::fmt::Display for ClippyWarning {
61 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 r#"{}-{}/{}:{}:{} {} "{}""#,
65 &self.crate_name, &self.crate_version, &self.file, &self.line, &self.column, &self.linttype, &self.message
71 fn download_and_extract(&self) -> Crate {
72 let extract_dir = PathBuf::from("target/lintcheck/crates");
73 let krate_download_dir = PathBuf::from("target/lintcheck/downloads");
75 // url to download the crate from crates.io
77 "https://crates.io/api/v1/crates/{}/{}/download",
78 self.name, self.version
80 println!("Downloading and extracting {} {} from {}", self.name, self.version, url);
81 let _ = std::fs::create_dir("target/lintcheck/");
82 let _ = std::fs::create_dir(&krate_download_dir);
83 let _ = std::fs::create_dir(&extract_dir);
85 let krate_file_path = krate_download_dir.join(format!("{}-{}.crate.tar.gz", &self.name, &self.version));
86 // don't download/extract if we already have done so
87 if !krate_file_path.is_file() {
88 // create a file path to download and write the crate data into
89 let mut krate_dest = std::fs::File::create(&krate_file_path).unwrap();
90 let mut krate_req = ureq::get(&url).call().unwrap().into_reader();
91 // copy the crate into the file
92 std::io::copy(&mut krate_req, &mut krate_dest).unwrap();
95 let ungz_tar = flate2::read::GzDecoder::new(std::fs::File::open(&krate_file_path).unwrap());
96 // extract the tar archive
97 let mut archive = tar::Archive::new(ungz_tar);
98 archive.unpack(&extract_dir).expect("Failed to extract!");
100 // crate is extracted, return a new Krate object which contains the path to the extracted
101 // sources that clippy can check
103 version: self.version.clone(),
104 name: self.name.clone(),
105 path: extract_dir.join(format!("{}-{}/", self.name, self.version)),
111 fn run_clippy_lints(&self, cargo_clippy_path: &PathBuf) -> Vec<ClippyWarning> {
112 println!("Linting {} {}...", &self.name, &self.version);
113 let cargo_clippy_path = std::fs::canonicalize(cargo_clippy_path).unwrap();
115 let shared_target_dir = clippy_project_root().join("target/lintcheck/shared_target_dir/");
117 let all_output = std::process::Command::new(cargo_clippy_path)
118 .env("CARGO_TARGET_DIR", shared_target_dir)
119 // lint warnings will look like this:
120 // src/cargo/ops/cargo_compile.rs:127:35: warning: usage of `FromIterator::from_iter`
123 "--message-format=json",
126 "-Wclippy::pedantic",
129 .current_dir(&self.path)
132 let stdout = String::from_utf8_lossy(&all_output.stdout);
133 let output_lines = stdout.lines();
134 //dbg!(&output_lines);
135 let warnings: Vec<ClippyWarning> = output_lines
137 // get all clippy warnings
138 .filter(|line| line.contains("clippy::"))
139 .map(|json_msg| parse_json_message(json_msg, &self))
146 Command::new("cargo")
149 .expect("Failed to build clippy!");
152 // get a list of CrateSources we want to check from a "lintcheck_crates.toml" file.
153 fn read_crates() -> Vec<CrateSource> {
154 let toml_path = PathBuf::from("clippy_dev/lintcheck_crates.toml");
155 let toml_content: String =
156 std::fs::read_to_string(&toml_path).unwrap_or_else(|_| panic!("Failed to read {}", toml_path.display()));
157 let crate_list: CrateList =
158 toml::from_str(&toml_content).unwrap_or_else(|e| panic!("Failed to parse {}: \n{}", toml_path.display(), e));
159 // parse the hashmap of the toml file into a list of crates
160 let tomlcrates: Vec<TomlCrate> = crate_list
163 .map(|(name, versions)| TomlCrate { name, versions })
166 // flatten TomlCrates into CrateSources (one TomlCrates may represent several versions of a crate =>
167 // multiple Cratesources)
168 let mut crate_sources = Vec::new();
169 tomlcrates.into_iter().for_each(|tk| {
170 tk.versions.iter().for_each(|ver| {
171 crate_sources.push(CrateSource {
172 name: tk.name.clone(),
173 version: ver.to_string(),
180 // extract interesting data from a json lint message
181 fn parse_json_message(json_message: &str, krate: &Crate) -> ClippyWarning {
182 let jmsg: Value = serde_json::from_str(&json_message).unwrap_or_else(|e| panic!("Failed to parse json:\n{:?}", e));
185 crate_name: krate.name.to_string(),
186 crate_version: krate.version.to_string(),
187 file: jmsg["message"]["spans"][0]["file_name"]
191 line: jmsg["message"]["spans"][0]["line_start"]
195 column: jmsg["message"]["spans"][0]["text"][0]["highlight_start"]
199 linttype: jmsg["message"]["code"]["code"].to_string().trim_matches('"').into(),
200 message: jmsg["message"]["message"].to_string().trim_matches('"').into(),
205 pub fn run(clap_config: &ArgMatches) {
206 let cargo_clippy_path: PathBuf = PathBuf::from("target/debug/cargo-clippy");
208 println!("Compiling clippy...");
210 println!("Done compiling");
212 // assert that clippy is found
214 cargo_clippy_path.is_file(),
215 "target/debug/cargo-clippy binary not found! {}",
216 cargo_clippy_path.display()
219 let clippy_ver = std::process::Command::new("target/debug/cargo-clippy")
222 .map(|o| String::from_utf8_lossy(&o.stdout).into_owned())
223 .expect("could not get clippy version!");
225 // download and extract the crates, then run clippy on them and collect clippys warnings
226 // flatten into one big list of warnings
228 let crates = read_crates();
230 let clippy_warnings: Vec<ClippyWarning> = if let Some(only_one_crate) = clap_config.value_of("only") {
231 // if we don't have the specified crated in the .toml, throw an error
232 if !crates.iter().any(|krate| krate.name == only_one_crate) {
234 "ERROR: could not find crate '{}' in clippy_dev/lintcheck_crates.toml",
237 std::process::exit(1);
240 // only check a single crate that was passed via cmdline
243 .map(|krate| krate.download_and_extract())
244 .filter(|krate| krate.name == only_one_crate)
245 .map(|krate| krate.run_clippy_lints(&cargo_clippy_path))
249 // check all crates (default)
252 .map(|krate| krate.download_and_extract())
253 .map(|krate| krate.run_clippy_lints(&cargo_clippy_path))
258 // generate some stats:
260 // count lint type occurrences
261 let mut counter: HashMap<&String, usize> = HashMap::new();
264 .for_each(|wrn| *counter.entry(&wrn.linttype).or_insert(0) += 1);
266 // collect into a tupled list for sorting
267 let mut stats: Vec<(&&String, &usize)> = counter.iter().map(|(lint, count)| (lint, count)).collect();
268 // sort by "000{count} {clippy::lintname}"
269 // to not have a lint with 200 and 2 warnings take the same spot
270 stats.sort_by_key(|(lint, count)| format!("{:0>4}, {}", count, lint));
272 let stats_formatted: String = stats
274 .map(|(lint, count)| format!("{} {}\n", lint, count))
275 .collect::<String>();
277 let mut all_msgs: Vec<String> = clippy_warnings.iter().map(|warning| warning.to_string()).collect();
279 all_msgs.push("\n\n\n\nStats\n\n".into());
280 all_msgs.push(stats_formatted);
282 // save the text into lintcheck-logs/logs.txt
283 let mut text = clippy_ver; // clippy version number on top
284 text.push_str(&format!("\n{}", all_msgs.join("")));
285 write("lintcheck-logs/logs.txt", text).unwrap();