]> git.lizzy.rs Git - rust.git/blob - clippy_dev/src/crater.rs
remove duplicate code and other cleanup
[rust.git] / clippy_dev / src / crater.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 #![allow(clippy::filter_map)]
8
9 use crate::clippy_project_root;
10
11 use std::collections::HashMap;
12 use std::process::Command;
13 use std::{fs::write, path::PathBuf};
14
15 use serde::{Deserialize, Serialize};
16
17 // use this to store the crates when interacting with the crates.toml file
18 #[derive(Debug, Serialize, Deserialize)]
19 struct CrateList {
20     crates: HashMap<String, Vec<String>>,
21 }
22
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
25 struct TomlCrate {
26     name: String,
27     versions: Vec<String>,
28 }
29
30 // represents an archive we download from crates.io
31 #[derive(Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
32 struct CrateSource {
33     name: String,
34     version: String,
35 }
36
37 // represents the extracted sourcecode of a crate
38 #[derive(Debug)]
39 struct Crate {
40     version: String,
41     name: String,
42     // path to the extracted sources that clippy can check
43     path: PathBuf,
44 }
45
46 impl CrateSource {
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");
50
51         // url to download the crate from crates.io
52         let url = format!(
53             "https://crates.io/api/v1/crates/{}/{}/download",
54             self.name, self.version
55         );
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);
60
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();
69
70             // unzip the tarball
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!");
75         }
76         // crate is extracted, return a new Krate object which contains the path to the extracted
77         // sources that clippy can check
78         Crate {
79             version: self.version.clone(),
80             name: self.name.clone(),
81             path: extract_dir.join(format!("{}-{}/", self.name, self.version)),
82         }
83     }
84 }
85
86 impl Crate {
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();
90
91         let shared_target_dir = clippy_project_root().join("target/crater/shared_target_dir/");
92
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`
97             .args(&[
98                 "--",
99                 "--message-format=short",
100                 "--",
101                 "--cap-lints=warn",
102                 "-Wclippy::pedantic",
103                 "-Wclippy::cargo",
104             ])
105             .current_dir(&self.path)
106             .output()
107             .unwrap();
108         let stderr = String::from_utf8_lossy(&all_output.stderr);
109         let output_lines = stderr.lines();
110         let mut output: Vec<String> = output_lines
111             .into_iter()
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: "
117             .map(|line| {
118                 let remove_pat = "warning: ";
119                 let pos = line
120                     .find(&remove_pat)
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()..]);
124                 new.push('\n');
125                 new
126             })
127             .collect();
128
129         // sort messages alphabetically to avoid noise in the logs
130         output.sort();
131         output
132     }
133 }
134
135 fn build_clippy() {
136     Command::new("cargo")
137         .arg("build")
138         .output()
139         .expect("Failed to build clippy!");
140 }
141
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
151         .crates
152         .into_iter()
153         .map(|(name, versions)| TomlCrate { name, versions })
154         .collect();
155
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(),
164             });
165         })
166     });
167     crate_sources
168 }
169
170 // the main fn
171 pub fn run() {
172     let cargo_clippy_path: PathBuf = PathBuf::from("target/debug/cargo-clippy");
173
174     println!("Compiling clippy...");
175     build_clippy();
176     println!("Done compiling");
177
178     // assert that clippy is found
179     assert!(
180         cargo_clippy_path.is_file(),
181         "target/debug/cargo-clippy binary not found! {}",
182         cargo_clippy_path.display()
183     );
184
185     // download and extract the crates, then run clippy on them and collect clippys warnings
186
187     let clippy_lint_results: Vec<Vec<String>> = read_crates()
188         .into_iter()
189         .map(|krate| krate.download_and_extract())
190         .map(|krate| krate.run_clippy_lints(&cargo_clippy_path))
191         .collect();
192
193     let mut all_warnings: Vec<String> = clippy_lint_results.into_iter().flatten().collect();
194     all_warnings.sort();
195
196     // save the text into mini-crater/logs.txt
197     let text = all_warnings.join("");
198     write("mini-crater/logs.txt", text).unwrap();
199 }