1 use crate::clippy_project_root;
2 use std::path::PathBuf;
3 use std::process::Command;
4 // represents an archive we download from crates.io
11 // represents the extracted sourcecode of a crate
16 // path to the extracted sources that clippy can check
21 fn new(name: &str, version: &str) -> Self {
23 version: version.into(),
28 fn download_and_extract(&self) -> Krate {
29 let extract_dir = PathBuf::from("target/crater/crates");
30 let krate_download_dir = PathBuf::from("target/crater/downloads");
32 // url to download the crate from crates.io
34 "https://crates.io/api/v1/crates/{}/{}/download",
35 self.name, self.version
37 println!("Downloading and extracting {} {} from {}", self.name, self.version, url);
38 let _ = std::fs::create_dir("target/crater/");
39 let _ = std::fs::create_dir(&krate_download_dir);
40 let _ = std::fs::create_dir(&extract_dir);
42 let krate_file_path = krate_download_dir.join(format!("{}-{}.crate.tar.gz", &self.name, &self.version));
43 // don't download/extract if we already have done so
44 if !krate_file_path.is_file() {
45 // create a file path to download and write the crate data into
46 let mut krate_dest = std::fs::File::create(&krate_file_path).unwrap();
47 let mut krate_req = ureq::get(&url).call().unwrap().into_reader();
48 // copy the crate into the file
49 std::io::copy(&mut krate_req, &mut krate_dest).unwrap();
52 let ungz_tar = flate2::read::GzDecoder::new(std::fs::File::open(&krate_file_path).unwrap());
53 // extract the tar archive
54 let mut archiv = tar::Archive::new(ungz_tar);
55 archiv.unpack(&extract_dir).expect("Failed to extract!");
58 let ungz_tar = flate2::read::GzDecoder::new(std::fs::File::open(&krate_file_path).unwrap());
59 // extract the tar archive
60 let mut archiv = tar::Archive::new(ungz_tar);
61 archiv.unpack(&extract_dir).expect("Failed to extract!");
63 // crate is extracted, return a new Krate object which contains the path to the extracted
64 // sources that clippy can check
66 version: self.version.clone(),
67 name: self.name.clone(),
68 path: extract_dir.join(format!("{}-{}/", self.name, self.version)),
74 fn run_clippy_lints(&self, cargo_clippy_path: &PathBuf) -> Vec<String> {
75 println!("Linting {} {}...", &self.name, &self.version);
76 let cargo_clippy_path = std::fs::canonicalize(cargo_clippy_path).unwrap();
78 let shared_target_dir = clippy_project_root().join("target/crater/shared_target_dir/");
80 let all_output = std::process::Command::new(cargo_clippy_path)
81 .env("CARGO_TARGET_DIR", shared_target_dir)
82 // lint warnings will look like this:
83 // src/cargo/ops/cargo_compile.rs:127:35: warning: usage of `FromIterator::from_iter`
86 "--message-format=short",
92 .current_dir(&self.path)
95 let stderr = String::from_utf8_lossy(&all_output.stderr);
96 let output_lines = stderr.lines();
97 let mut output: Vec<String> = output_lines
99 .filter(|line| line.contains(": warning: "))
100 // prefix with the crate name and version
101 // cargo-0.49.0/src/cargo/ops/cargo_compile.rs:127:35: warning: usage of `FromIterator::from_iter`
102 .map(|line| format!("{}-{}/{}", self.name, self.version, line))
103 // remove the "warning: "
105 let remove_pat = "warning: ";
108 .expect("clippy output did not contain \"warning: \"");
109 let mut new = line[0..pos].to_string();
110 new.push_str(&line[pos + remove_pat.len()..]);
116 // sort messages alphabtically to avoid noise in the logs
123 Command::new("cargo")
126 .expect("Failed to build clippy!");
131 let cargo_clippy_path: PathBuf = PathBuf::from("target/debug/cargo-clippy");
133 // crates we want to check:
134 let krates: Vec<KrateSource> = vec![
135 // some of these are form cargotest
136 KrateSource::new("cargo", "0.49.0"),
137 KrateSource::new("iron", "0.6.1"),
138 KrateSource::new("ripgrep", "12.1.1"),
139 //KrateSource::new("tokei", "12.0.4"),
140 KrateSource::new("xsv", "0.13.0"),
141 KrateSource::new("serde", "1.0.118"),
142 KrateSource::new("rayon", "1.5.0"),
143 // top 10 crates.io dls
144 KrateSource::new("rand", "0.7.3"),
145 KrateSource::new("syn", "1.0.54"),
146 KrateSource::new("libc", "0.2.81"),
147 KrateSource::new("quote", "1.0.7"),
148 KrateSource::new("rand_core", "0.6.0"),
149 KrateSource::new("unicode-xid", "0.2.1"),
150 KrateSource::new("proc-macro2", "1.0.24"),
151 KrateSource::new("bitflags", "1.2.1"),
152 KrateSource::new("log", "0.4.11"),
153 KrateSource::new("regex", "1.4.2"),
156 println!("Compiling clippy...");
158 println!("Done compiling");
160 // assert that clippy is found
162 cargo_clippy_path.is_file(),
163 "target/debug/cargo-clippy binary not found! {}",
164 cargo_clippy_path.display()
167 // download and extract the crates, then run clippy on them and collect clippys warnings
168 let clippy_lint_results: Vec<Vec<String>> = krates
170 .map(|krate| krate.download_and_extract())
171 .map(|krate| krate.run_clippy_lints(&cargo_clippy_path))
174 let all_warnings: Vec<String> = clippy_lint_results.into_iter().flatten().collect();
176 // save the text into mini-crater/logs.txt
177 let text = all_warnings.join("");
178 std::fs::write("mini-crater/logs.txt", text).unwrap();