2020-12-23 12:02:02 +00:00
|
|
|
// Run clippy on a fixed set of crates and collect the warnings.
|
|
|
|
// This helps observing the impact clippy changs have on a set of real-world code.
|
|
|
|
//
|
|
|
|
// When a new lint is introduced, we can search the results for new warnings and check for false
|
|
|
|
// positives.
|
|
|
|
|
2020-12-18 21:53:45 +00:00
|
|
|
#![allow(clippy::filter_map)]
|
|
|
|
|
2020-12-18 21:08:18 +00:00
|
|
|
use crate::clippy_project_root;
|
2020-12-23 12:02:02 +00:00
|
|
|
|
2020-12-22 12:07:55 +00:00
|
|
|
use std::collections::HashMap;
|
2020-12-18 12:21:13 +00:00
|
|
|
use std::process::Command;
|
2020-12-22 12:07:55 +00:00
|
|
|
use std::{fs::write, path::PathBuf};
|
2020-12-18 21:53:45 +00:00
|
|
|
|
2020-12-23 12:02:02 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2020-12-23 12:03:19 +00:00
|
|
|
// use this to store the crates when interacting with the crates.toml file
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
struct CrateList {
|
|
|
|
crates: HashMap<String, Vec<String>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
// crate data we stored in the toml, can have multiple versions per crate
|
|
|
|
// A single TomlCrate is laster mapped to several CrateSources in that case
|
2020-12-23 12:02:02 +00:00
|
|
|
struct TomlCrate {
|
2020-12-23 00:21:31 +00:00
|
|
|
name: String,
|
|
|
|
versions: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2020-12-18 12:21:13 +00:00
|
|
|
// represents an archive we download from crates.io
|
2020-12-22 12:07:55 +00:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
|
2020-12-23 12:02:02 +00:00
|
|
|
struct CrateSource {
|
2020-12-18 12:21:13 +00:00
|
|
|
name: String,
|
2020-12-23 00:21:31 +00:00
|
|
|
version: String,
|
2020-12-18 12:21:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// represents the extracted sourcecode of a crate
|
2020-12-18 13:14:15 +00:00
|
|
|
#[derive(Debug)]
|
2020-12-23 12:02:02 +00:00
|
|
|
struct Crate {
|
2020-12-18 12:21:13 +00:00
|
|
|
version: String,
|
|
|
|
name: String,
|
2020-12-18 19:58:46 +00:00
|
|
|
// path to the extracted sources that clippy can check
|
2020-12-18 13:14:15 +00:00
|
|
|
path: PathBuf,
|
2020-12-18 12:21:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 12:02:02 +00:00
|
|
|
impl CrateSource {
|
|
|
|
fn download_and_extract(&self) -> Crate {
|
2020-12-18 13:14:15 +00:00
|
|
|
let extract_dir = PathBuf::from("target/crater/crates");
|
|
|
|
let krate_download_dir = PathBuf::from("target/crater/downloads");
|
|
|
|
|
2020-12-18 19:58:46 +00:00
|
|
|
// url to download the crate from crates.io
|
2020-12-18 13:14:15 +00:00
|
|
|
let url = format!(
|
|
|
|
"https://crates.io/api/v1/crates/{}/{}/download",
|
|
|
|
self.name, self.version
|
|
|
|
);
|
2020-12-18 19:58:46 +00:00
|
|
|
println!("Downloading and extracting {} {} from {}", self.name, self.version, url);
|
2020-12-18 15:53:18 +00:00
|
|
|
let _ = std::fs::create_dir("target/crater/");
|
|
|
|
let _ = std::fs::create_dir(&krate_download_dir);
|
|
|
|
let _ = std::fs::create_dir(&extract_dir);
|
2020-12-18 15:17:53 +00:00
|
|
|
|
2020-12-18 19:58:46 +00:00
|
|
|
let krate_file_path = krate_download_dir.join(format!("{}-{}.crate.tar.gz", &self.name, &self.version));
|
|
|
|
// don't download/extract if we already have done so
|
|
|
|
if !krate_file_path.is_file() {
|
|
|
|
// create a file path to download and write the crate data into
|
|
|
|
let mut krate_dest = std::fs::File::create(&krate_file_path).unwrap();
|
|
|
|
let mut krate_req = ureq::get(&url).call().unwrap().into_reader();
|
|
|
|
// copy the crate into the file
|
|
|
|
std::io::copy(&mut krate_req, &mut krate_dest).unwrap();
|
|
|
|
|
|
|
|
// unzip the tarball
|
|
|
|
let ungz_tar = flate2::read::GzDecoder::new(std::fs::File::open(&krate_file_path).unwrap());
|
|
|
|
// extract the tar archive
|
2020-12-23 12:03:19 +00:00
|
|
|
let mut archive = tar::Archive::new(ungz_tar);
|
|
|
|
archive.unpack(&extract_dir).expect("Failed to extract!");
|
2020-12-18 19:58:46 +00:00
|
|
|
}
|
|
|
|
// crate is extracted, return a new Krate object which contains the path to the extracted
|
|
|
|
// sources that clippy can check
|
2020-12-23 12:02:02 +00:00
|
|
|
Crate {
|
2020-12-18 13:14:15 +00:00
|
|
|
version: self.version.clone(),
|
|
|
|
name: self.name.clone(),
|
2020-12-18 17:01:45 +00:00
|
|
|
path: extract_dir.join(format!("{}-{}/", self.name, self.version)),
|
2020-12-18 12:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 12:02:02 +00:00
|
|
|
impl Crate {
|
2020-12-18 17:34:09 +00:00
|
|
|
fn run_clippy_lints(&self, cargo_clippy_path: &PathBuf) -> Vec<String> {
|
2020-12-18 19:58:46 +00:00
|
|
|
println!("Linting {} {}...", &self.name, &self.version);
|
2020-12-18 16:25:07 +00:00
|
|
|
let cargo_clippy_path = std::fs::canonicalize(cargo_clippy_path).unwrap();
|
|
|
|
|
2020-12-18 21:08:18 +00:00
|
|
|
let shared_target_dir = clippy_project_root().join("target/crater/shared_target_dir/");
|
|
|
|
|
2020-12-18 19:58:46 +00:00
|
|
|
let all_output = std::process::Command::new(cargo_clippy_path)
|
2020-12-18 21:08:18 +00:00
|
|
|
.env("CARGO_TARGET_DIR", shared_target_dir)
|
2020-12-18 19:58:46 +00:00
|
|
|
// lint warnings will look like this:
|
|
|
|
// src/cargo/ops/cargo_compile.rs:127:35: warning: usage of `FromIterator::from_iter`
|
2020-12-18 20:50:06 +00:00
|
|
|
.args(&[
|
|
|
|
"--",
|
|
|
|
"--message-format=short",
|
|
|
|
"--",
|
|
|
|
"--cap-lints=warn",
|
|
|
|
"-Wclippy::pedantic",
|
2020-12-18 21:08:18 +00:00
|
|
|
"-Wclippy::cargo",
|
2020-12-18 20:50:06 +00:00
|
|
|
])
|
2020-12-18 19:58:46 +00:00
|
|
|
.current_dir(&self.path)
|
2020-12-18 17:34:09 +00:00
|
|
|
.output()
|
|
|
|
.unwrap();
|
2020-12-18 19:58:46 +00:00
|
|
|
let stderr = String::from_utf8_lossy(&all_output.stderr);
|
|
|
|
let output_lines = stderr.lines();
|
|
|
|
let mut output: Vec<String> = output_lines
|
2020-12-18 17:34:09 +00:00
|
|
|
.into_iter()
|
|
|
|
.filter(|line| line.contains(": warning: "))
|
2020-12-18 19:58:46 +00:00
|
|
|
// prefix with the crate name and version
|
|
|
|
// cargo-0.49.0/src/cargo/ops/cargo_compile.rs:127:35: warning: usage of `FromIterator::from_iter`
|
|
|
|
.map(|line| format!("{}-{}/{}", self.name, self.version, line))
|
|
|
|
// remove the "warning: "
|
|
|
|
.map(|line| {
|
|
|
|
let remove_pat = "warning: ";
|
|
|
|
let pos = line
|
|
|
|
.find(&remove_pat)
|
|
|
|
.expect("clippy output did not contain \"warning: \"");
|
|
|
|
let mut new = line[0..pos].to_string();
|
|
|
|
new.push_str(&line[pos + remove_pat.len()..]);
|
2020-12-18 20:26:41 +00:00
|
|
|
new.push('\n');
|
2020-12-18 19:58:46 +00:00
|
|
|
new
|
|
|
|
})
|
2020-12-18 17:34:09 +00:00
|
|
|
.collect();
|
|
|
|
|
2020-12-23 12:03:19 +00:00
|
|
|
// sort messages alphabetically to avoid noise in the logs
|
2020-12-18 17:34:09 +00:00
|
|
|
output.sort();
|
2020-12-18 16:25:07 +00:00
|
|
|
output
|
2020-12-18 12:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_clippy() {
|
|
|
|
Command::new("cargo")
|
|
|
|
.arg("build")
|
|
|
|
.output()
|
|
|
|
.expect("Failed to build clippy!");
|
|
|
|
}
|
|
|
|
|
2020-12-23 12:02:02 +00:00
|
|
|
// get a list of CrateSources we want to check from a "crater_crates.toml" file.
|
|
|
|
fn read_crates() -> Vec<CrateSource> {
|
2020-12-22 12:07:55 +00:00
|
|
|
let toml_path = PathBuf::from("clippy_dev/crater_crates.toml");
|
|
|
|
let toml_content: String =
|
|
|
|
std::fs::read_to_string(&toml_path).unwrap_or_else(|_| panic!("Failed to read {}", toml_path.display()));
|
|
|
|
let crate_list: CrateList =
|
|
|
|
toml::from_str(&toml_content).unwrap_or_else(|e| panic!("Failed to parse {}: \n{}", toml_path.display(), e));
|
|
|
|
// parse the hashmap of the toml file into a list of crates
|
2020-12-23 12:02:02 +00:00
|
|
|
let tomlcrates: Vec<TomlCrate> = crate_list
|
2020-12-22 12:07:55 +00:00
|
|
|
.crates
|
2020-12-23 00:21:31 +00:00
|
|
|
.into_iter()
|
2020-12-23 12:02:02 +00:00
|
|
|
.map(|(name, versions)| TomlCrate { name, versions })
|
2020-12-23 00:21:31 +00:00
|
|
|
.collect();
|
|
|
|
|
2020-12-23 12:02:02 +00:00
|
|
|
// flatten TomlCrates into CrateSources (one TomlCrates may represent several versions of a crate =>
|
|
|
|
// multiple Cratesources)
|
|
|
|
let mut crate_sources = Vec::new();
|
|
|
|
tomlcrates.into_iter().for_each(|tk| {
|
2020-12-23 00:21:31 +00:00
|
|
|
tk.versions.iter().for_each(|ver| {
|
2020-12-23 12:02:02 +00:00
|
|
|
crate_sources.push(CrateSource {
|
2020-12-23 00:21:31 +00:00
|
|
|
name: tk.name.clone(),
|
|
|
|
version: ver.to_string(),
|
|
|
|
});
|
|
|
|
})
|
|
|
|
});
|
2020-12-23 12:02:02 +00:00
|
|
|
crate_sources
|
2020-12-22 12:07:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 12:21:13 +00:00
|
|
|
// the main fn
|
2020-12-18 13:28:59 +00:00
|
|
|
pub fn run() {
|
2020-12-18 12:21:13 +00:00
|
|
|
let cargo_clippy_path: PathBuf = PathBuf::from("target/debug/cargo-clippy");
|
|
|
|
|
2020-12-18 15:17:53 +00:00
|
|
|
println!("Compiling clippy...");
|
2020-12-18 12:21:13 +00:00
|
|
|
build_clippy();
|
2020-12-18 15:17:53 +00:00
|
|
|
println!("Done compiling");
|
|
|
|
|
2020-12-18 12:21:13 +00:00
|
|
|
// assert that clippy is found
|
|
|
|
assert!(
|
|
|
|
cargo_clippy_path.is_file(),
|
2020-12-18 15:53:18 +00:00
|
|
|
"target/debug/cargo-clippy binary not found! {}",
|
|
|
|
cargo_clippy_path.display()
|
2020-12-18 12:21:13 +00:00
|
|
|
);
|
|
|
|
|
2020-12-18 13:14:15 +00:00
|
|
|
// download and extract the crates, then run clippy on them and collect clippys warnings
|
2020-12-22 12:07:55 +00:00
|
|
|
|
|
|
|
let clippy_lint_results: Vec<Vec<String>> = read_crates()
|
2020-12-18 13:14:15 +00:00
|
|
|
.into_iter()
|
|
|
|
.map(|krate| krate.download_and_extract())
|
2020-12-18 16:25:07 +00:00
|
|
|
.map(|krate| krate.run_clippy_lints(&cargo_clippy_path))
|
2020-12-18 17:34:09 +00:00
|
|
|
.collect();
|
|
|
|
|
2020-12-22 12:07:55 +00:00
|
|
|
let mut all_warnings: Vec<String> = clippy_lint_results.into_iter().flatten().collect();
|
|
|
|
all_warnings.sort();
|
2020-12-18 17:34:09 +00:00
|
|
|
|
2020-12-18 20:26:41 +00:00
|
|
|
// save the text into mini-crater/logs.txt
|
|
|
|
let text = all_warnings.join("");
|
2020-12-22 12:07:55 +00:00
|
|
|
write("mini-crater/logs.txt", text).unwrap();
|
2020-12-18 12:21:13 +00:00
|
|
|
}
|