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.
|
|
|
|
|
2021-01-22 23:25:29 +00:00
|
|
|
#![cfg(feature = "lintcheck")]
|
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-23 14:00:51 +00:00
|
|
|
use std::{fmt, fs::write, path::PathBuf};
|
2020-12-18 21:53:45 +00:00
|
|
|
|
2020-12-27 15:13:42 +00:00
|
|
|
use clap::ArgMatches;
|
2020-12-23 12:02:02 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-12-23 14:00:51 +00:00
|
|
|
use serde_json::Value;
|
2020-12-23 12:02:02 +00:00
|
|
|
|
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 {
|
2021-02-05 22:13:59 +00:00
|
|
|
crates: HashMap<String, TomlCrate>,
|
2020-12-23 12:03:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2021-02-05 22:13:59 +00:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2020-12-23 12:02:02 +00:00
|
|
|
struct TomlCrate {
|
2020-12-23 00:21:31 +00:00
|
|
|
name: String,
|
2021-02-05 22:13:59 +00:00
|
|
|
versions: Option<Vec<String>>,
|
|
|
|
git_url: Option<String>,
|
|
|
|
git_hash: Option<String>,
|
2021-02-10 10:32:10 +00:00
|
|
|
path: Option<String>,
|
2020-12-23 00:21:31 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 10:32:10 +00:00
|
|
|
// represents an archive we download from crates.io, or a git repo, or a local repo
|
2020-12-22 12:07:55 +00:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
|
2021-02-06 11:02:42 +00:00
|
|
|
enum CrateSource {
|
|
|
|
CratesIo { name: String, version: String },
|
|
|
|
Git { name: String, url: String, commit: String },
|
2021-02-10 10:32:10 +00:00
|
|
|
Path { name: String, path: PathBuf },
|
2020-12-18 12:21:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// represents the extracted sourcecode of a crate
|
2021-02-06 11:04:31 +00:00
|
|
|
// we actually don't need to special-case git repos here because it does not matter for clippy, yay!
|
|
|
|
// (clippy only needs a simple path)
|
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 14:00:51 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct ClippyWarning {
|
|
|
|
crate_name: String,
|
|
|
|
crate_version: String,
|
|
|
|
file: String,
|
|
|
|
line: String,
|
|
|
|
column: String,
|
|
|
|
linttype: String,
|
|
|
|
message: String,
|
2021-02-10 11:50:36 +00:00
|
|
|
ice: bool,
|
2020-12-23 14:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for ClippyWarning {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
writeln!(
|
|
|
|
f,
|
2021-01-23 00:09:51 +00:00
|
|
|
r#"{}-{}/{}:{}:{} {} "{}""#,
|
2020-12-23 14:00:51 +00:00
|
|
|
&self.crate_name, &self.crate_version, &self.file, &self.line, &self.column, &self.linttype, &self.message
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 12:02:02 +00:00
|
|
|
impl CrateSource {
|
|
|
|
fn download_and_extract(&self) -> Crate {
|
2021-02-06 11:02:42 +00:00
|
|
|
match self {
|
|
|
|
CrateSource::CratesIo { name, version } => {
|
|
|
|
let extract_dir = PathBuf::from("target/lintcheck/crates");
|
|
|
|
let krate_download_dir = PathBuf::from("target/lintcheck/downloads");
|
|
|
|
|
|
|
|
// url to download the crate from crates.io
|
|
|
|
let url = format!("https://crates.io/api/v1/crates/{}/{}/download", name, version);
|
|
|
|
println!("Downloading and extracting {} {} from {}", name, version, url);
|
|
|
|
let _ = std::fs::create_dir("target/lintcheck/");
|
|
|
|
let _ = std::fs::create_dir(&krate_download_dir);
|
|
|
|
let _ = std::fs::create_dir(&extract_dir);
|
|
|
|
|
|
|
|
let krate_file_path = krate_download_dir.join(format!("{}-{}.crate.tar.gz", name, 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
|
|
|
|
let mut archive = tar::Archive::new(ungz_tar);
|
|
|
|
archive.unpack(&extract_dir).expect("Failed to extract!");
|
|
|
|
}
|
|
|
|
// crate is extracted, return a new Krate object which contains the path to the extracted
|
|
|
|
// sources that clippy can check
|
|
|
|
Crate {
|
|
|
|
version: version.clone(),
|
|
|
|
name: name.clone(),
|
|
|
|
path: extract_dir.join(format!("{}-{}/", name, version)),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
CrateSource::Git { name, url, commit } => {
|
|
|
|
let repo_path = {
|
2021-02-10 10:32:10 +00:00
|
|
|
let mut repo_path = PathBuf::from("target/lintcheck/crates");
|
2021-02-06 11:02:42 +00:00
|
|
|
// add a -git suffix in case we have the same crate from crates.io and a git repo
|
|
|
|
repo_path.push(format!("{}-git", name));
|
|
|
|
repo_path
|
|
|
|
};
|
|
|
|
// clone the repo if we have not done so
|
|
|
|
if !repo_path.is_dir() {
|
2021-02-06 10:36:06 +00:00
|
|
|
println!("Cloning {} and checking out {}", url, commit);
|
2021-02-06 11:02:42 +00:00
|
|
|
Command::new("git")
|
|
|
|
.arg("clone")
|
|
|
|
.arg(url)
|
|
|
|
.arg(&repo_path)
|
|
|
|
.output()
|
|
|
|
.expect("Failed to clone git repo!");
|
|
|
|
}
|
|
|
|
// check out the commit/branch/whatever
|
|
|
|
Command::new("git")
|
|
|
|
.arg("checkout")
|
|
|
|
.arg(commit)
|
|
|
|
.output()
|
|
|
|
.expect("Failed to check out commit");
|
|
|
|
|
|
|
|
Crate {
|
|
|
|
version: commit.clone(),
|
|
|
|
name: name.clone(),
|
|
|
|
path: repo_path,
|
|
|
|
}
|
|
|
|
},
|
2021-02-10 10:32:10 +00:00
|
|
|
CrateSource::Path { name, path } => {
|
|
|
|
use fs_extra::dir;
|
|
|
|
|
|
|
|
// simply copy the entire directory into our target dir
|
|
|
|
let copy_dest = PathBuf::from("target/lintcheck/crates/");
|
|
|
|
|
|
|
|
// the source path of the crate we copied, ${copy_dest}/crate_name
|
|
|
|
let crate_root = copy_dest.join(name); // .../crates/local_crate
|
|
|
|
|
|
|
|
if !crate_root.exists() {
|
|
|
|
println!("Copying {} to {}", path.display(), copy_dest.display());
|
|
|
|
|
|
|
|
dir::copy(path, ©_dest, &dir::CopyOptions::new()).expect(&format!(
|
|
|
|
"Failed to copy from {}, to {}",
|
|
|
|
path.display(),
|
|
|
|
crate_root.display()
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
println!(
|
|
|
|
"Not copying {} to {}, destination already exists",
|
|
|
|
path.display(),
|
|
|
|
crate_root.display()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Crate {
|
|
|
|
version: String::from("local"),
|
|
|
|
name: name.clone(),
|
|
|
|
path: crate_root,
|
|
|
|
}
|
|
|
|
},
|
2020-12-18 12:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 12:02:02 +00:00
|
|
|
impl Crate {
|
2020-12-23 14:31:18 +00:00
|
|
|
fn run_clippy_lints(&self, cargo_clippy_path: &PathBuf) -> Vec<ClippyWarning> {
|
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();
|
|
|
|
|
2021-01-22 23:25:29 +00:00
|
|
|
let shared_target_dir = clippy_project_root().join("target/lintcheck/shared_target_dir/");
|
2020-12-18 21:08:18 +00:00
|
|
|
|
2021-02-05 22:13:59 +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(&[
|
|
|
|
"--",
|
2020-12-23 14:00:51 +00:00
|
|
|
"--message-format=json",
|
2020-12-18 20:50:06 +00:00
|
|
|
"--",
|
|
|
|
"--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()
|
2021-02-05 22:13:59 +00:00
|
|
|
.unwrap_or_else(|error| {
|
2021-02-06 18:12:28 +00:00
|
|
|
panic!(
|
|
|
|
"Encountered error:\n{:?}\ncargo_clippy_path: {}\ncrate path:{}\n",
|
|
|
|
error,
|
|
|
|
&cargo_clippy_path.display(),
|
|
|
|
&self.path.display()
|
|
|
|
);
|
2021-02-05 22:13:59 +00:00
|
|
|
});
|
2020-12-23 14:00:51 +00:00
|
|
|
let stdout = String::from_utf8_lossy(&all_output.stdout);
|
|
|
|
let output_lines = stdout.lines();
|
|
|
|
let warnings: Vec<ClippyWarning> = output_lines
|
2020-12-18 17:34:09 +00:00
|
|
|
.into_iter()
|
2021-02-10 11:50:36 +00:00
|
|
|
// get all clippy warnings and ICEs
|
|
|
|
.filter(|line| line.contains("clippy::") || line.contains("internal compiler error: "))
|
2020-12-23 14:00:51 +00:00
|
|
|
.map(|json_msg| parse_json_message(json_msg, &self))
|
2020-12-18 17:34:09 +00:00
|
|
|
.collect();
|
2020-12-23 14:31:18 +00:00
|
|
|
warnings
|
2020-12-18 12:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_clippy() {
|
|
|
|
Command::new("cargo")
|
|
|
|
.arg("build")
|
|
|
|
.output()
|
|
|
|
.expect("Failed to build clippy!");
|
|
|
|
}
|
|
|
|
|
2021-01-22 23:25:29 +00:00
|
|
|
// get a list of CrateSources we want to check from a "lintcheck_crates.toml" file.
|
2021-02-09 15:27:56 +00:00
|
|
|
fn read_crates(toml_path: Option<&str>) -> (String, Vec<CrateSource>) {
|
2021-02-07 15:12:21 +00:00
|
|
|
let toml_path = PathBuf::from(toml_path.unwrap_or("clippy_dev/lintcheck_crates.toml"));
|
2021-02-09 15:27:56 +00:00
|
|
|
// save it so that we can use the name of the sources.toml as name for the logfile later.
|
|
|
|
let toml_filename = toml_path.file_stem().unwrap().to_str().unwrap().to_string();
|
2020-12-22 12:07:55 +00:00
|
|
|
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()
|
2021-02-05 22:13:59 +00:00
|
|
|
.map(|(_cratename, tomlcrate)| tomlcrate)
|
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| {
|
2021-02-10 10:32:10 +00:00
|
|
|
if let Some(ref path) = tk.path {
|
|
|
|
crate_sources.push(CrateSource::Path {
|
|
|
|
name: tk.name.clone(),
|
|
|
|
path: PathBuf::from(path),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-06 11:02:42 +00:00
|
|
|
// if we have multiple versions, save each one
|
2021-02-05 22:13:59 +00:00
|
|
|
if let Some(ref versions) = tk.versions {
|
|
|
|
versions.iter().for_each(|ver| {
|
2021-02-06 11:02:42 +00:00
|
|
|
crate_sources.push(CrateSource::CratesIo {
|
2021-02-05 22:13:59 +00:00
|
|
|
name: tk.name.clone(),
|
|
|
|
version: ver.to_string(),
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
2021-02-06 11:02:42 +00:00
|
|
|
// otherwise, we should have a git source
|
|
|
|
if tk.git_url.is_some() && tk.git_hash.is_some() {
|
|
|
|
crate_sources.push(CrateSource::Git {
|
|
|
|
name: tk.name.clone(),
|
|
|
|
url: tk.git_url.clone().unwrap(),
|
|
|
|
commit: tk.git_hash.clone().unwrap(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// if we have a version as well as a git data OR only one git data, something is funky
|
|
|
|
if tk.versions.is_some() && (tk.git_url.is_some() || tk.git_hash.is_some())
|
|
|
|
|| tk.git_hash.is_some() != tk.git_url.is_some()
|
|
|
|
{
|
2021-02-06 18:12:28 +00:00
|
|
|
eprintln!("tomlkrate: {:?}", tk);
|
2021-02-06 11:04:31 +00:00
|
|
|
if tk.git_hash.is_some() != tk.git_url.is_some() {
|
2021-02-10 10:32:10 +00:00
|
|
|
panic!("Error: Encountered TomlCrate with only one of git_hash and git_url!");
|
|
|
|
}
|
|
|
|
if tk.path.is_some() && (tk.git_hash.is_some() || tk.versions.is_some()) {
|
|
|
|
panic!("Error: TomlCrate can only have one of 'git_.*', 'version' or 'path' fields");
|
2021-02-06 11:04:31 +00:00
|
|
|
}
|
2021-02-06 11:02:42 +00:00
|
|
|
unreachable!("Failed to translate TomlCrate into CrateSource!");
|
|
|
|
}
|
2020-12-23 00:21:31 +00:00
|
|
|
});
|
2021-02-09 15:27:56 +00:00
|
|
|
(toml_filename, crate_sources)
|
2020-12-22 12:07:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 14:00:51 +00:00
|
|
|
// extract interesting data from a json lint message
|
|
|
|
fn parse_json_message(json_message: &str, krate: &Crate) -> ClippyWarning {
|
|
|
|
let jmsg: Value = serde_json::from_str(&json_message).unwrap_or_else(|e| panic!("Failed to parse json:\n{:?}", e));
|
|
|
|
|
|
|
|
ClippyWarning {
|
|
|
|
crate_name: krate.name.to_string(),
|
|
|
|
crate_version: krate.version.to_string(),
|
|
|
|
file: jmsg["message"]["spans"][0]["file_name"]
|
|
|
|
.to_string()
|
|
|
|
.trim_matches('"')
|
|
|
|
.into(),
|
|
|
|
line: jmsg["message"]["spans"][0]["line_start"]
|
|
|
|
.to_string()
|
|
|
|
.trim_matches('"')
|
|
|
|
.into(),
|
|
|
|
column: jmsg["message"]["spans"][0]["text"][0]["highlight_start"]
|
|
|
|
.to_string()
|
|
|
|
.trim_matches('"')
|
|
|
|
.into(),
|
|
|
|
linttype: jmsg["message"]["code"]["code"].to_string().trim_matches('"').into(),
|
|
|
|
message: jmsg["message"]["message"].to_string().trim_matches('"').into(),
|
2021-02-10 11:50:36 +00:00
|
|
|
ice: json_message.contains("internal compiler error: "),
|
2020-12-23 14:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 12:21:13 +00:00
|
|
|
// the main fn
|
2020-12-27 15:13:42 +00:00
|
|
|
pub fn run(clap_config: &ArgMatches) {
|
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-29 15:18:31 +00:00
|
|
|
let clippy_ver = std::process::Command::new("target/debug/cargo-clippy")
|
|
|
|
.arg("--version")
|
|
|
|
.output()
|
|
|
|
.map(|o| String::from_utf8_lossy(&o.stdout).into_owned())
|
|
|
|
.expect("could not get clippy version!");
|
|
|
|
|
2020-12-18 13:14:15 +00:00
|
|
|
// download and extract the crates, then run clippy on them and collect clippys warnings
|
2020-12-23 14:31:18 +00:00
|
|
|
// flatten into one big list of warnings
|
2020-12-22 12:07:55 +00:00
|
|
|
|
2021-02-09 15:27:56 +00:00
|
|
|
let (filename, crates) = read_crates(clap_config.value_of("crates-toml"));
|
2020-12-27 15:20:32 +00:00
|
|
|
|
2020-12-27 15:13:42 +00:00
|
|
|
let clippy_warnings: Vec<ClippyWarning> = if let Some(only_one_crate) = clap_config.value_of("only") {
|
2021-02-06 11:04:31 +00:00
|
|
|
// if we don't have the specified crate in the .toml, throw an error
|
|
|
|
if !crates.iter().any(|krate| {
|
|
|
|
let name = match krate {
|
|
|
|
CrateSource::CratesIo { name, .. } => name,
|
|
|
|
CrateSource::Git { name, .. } => name,
|
2021-02-10 10:32:10 +00:00
|
|
|
CrateSource::Path { name, .. } => name,
|
2021-02-06 11:04:31 +00:00
|
|
|
};
|
|
|
|
name == only_one_crate
|
|
|
|
}) {
|
2020-12-27 15:20:32 +00:00
|
|
|
eprintln!(
|
2021-01-22 23:25:29 +00:00
|
|
|
"ERROR: could not find crate '{}' in clippy_dev/lintcheck_crates.toml",
|
2020-12-27 15:20:32 +00:00
|
|
|
only_one_crate
|
|
|
|
);
|
|
|
|
std::process::exit(1);
|
2021-02-06 11:04:31 +00:00
|
|
|
}
|
2020-12-27 15:20:32 +00:00
|
|
|
|
|
|
|
// only check a single crate that was passed via cmdline
|
|
|
|
crates
|
2020-12-27 15:13:42 +00:00
|
|
|
.into_iter()
|
|
|
|
.map(|krate| krate.download_and_extract())
|
|
|
|
.filter(|krate| krate.name == only_one_crate)
|
|
|
|
.map(|krate| krate.run_clippy_lints(&cargo_clippy_path))
|
|
|
|
.flatten()
|
|
|
|
.collect()
|
|
|
|
} else {
|
2020-12-27 15:20:32 +00:00
|
|
|
// check all crates (default)
|
|
|
|
crates
|
2020-12-27 15:13:42 +00:00
|
|
|
.into_iter()
|
|
|
|
.map(|krate| krate.download_and_extract())
|
|
|
|
.map(|krate| krate.run_clippy_lints(&cargo_clippy_path))
|
|
|
|
.flatten()
|
|
|
|
.collect()
|
|
|
|
};
|
2020-12-18 17:34:09 +00:00
|
|
|
|
2020-12-23 14:59:16 +00:00
|
|
|
// generate some stats:
|
|
|
|
|
2021-02-10 11:50:36 +00:00
|
|
|
// grab crashes/ICEs, save the crate name and the ice message
|
|
|
|
let ices: Vec<(&String, &String)> = clippy_warnings
|
|
|
|
.iter()
|
|
|
|
.filter(|warning| warning.ice)
|
|
|
|
.map(|w| (&w.crate_name, &w.message))
|
|
|
|
.collect();
|
|
|
|
|
2020-12-23 14:59:16 +00:00
|
|
|
// count lint type occurrences
|
|
|
|
let mut counter: HashMap<&String, usize> = HashMap::new();
|
|
|
|
clippy_warnings
|
|
|
|
.iter()
|
|
|
|
.for_each(|wrn| *counter.entry(&wrn.linttype).or_insert(0) += 1);
|
|
|
|
|
|
|
|
// collect into a tupled list for sorting
|
|
|
|
let mut stats: Vec<(&&String, &usize)> = counter.iter().map(|(lint, count)| (lint, count)).collect();
|
2020-12-27 14:44:45 +00:00
|
|
|
// sort by "000{count} {clippy::lintname}"
|
|
|
|
// to not have a lint with 200 and 2 warnings take the same spot
|
|
|
|
stats.sort_by_key(|(lint, count)| format!("{:0>4}, {}", count, lint));
|
2020-12-23 14:59:16 +00:00
|
|
|
|
|
|
|
let stats_formatted: String = stats
|
|
|
|
.iter()
|
|
|
|
.map(|(lint, count)| format!("{} {}\n", lint, count))
|
|
|
|
.collect::<String>();
|
|
|
|
|
|
|
|
let mut all_msgs: Vec<String> = clippy_warnings.iter().map(|warning| warning.to_string()).collect();
|
|
|
|
all_msgs.sort();
|
|
|
|
all_msgs.push("\n\n\n\nStats\n\n".into());
|
|
|
|
all_msgs.push(stats_formatted);
|
2020-12-18 17:34:09 +00:00
|
|
|
|
2021-01-22 23:25:29 +00:00
|
|
|
// save the text into lintcheck-logs/logs.txt
|
2020-12-29 15:18:31 +00:00
|
|
|
let mut text = clippy_ver; // clippy version number on top
|
|
|
|
text.push_str(&format!("\n{}", all_msgs.join("")));
|
2021-02-10 11:50:36 +00:00
|
|
|
text.push_str("ICEs:\n");
|
|
|
|
ices.iter()
|
|
|
|
.for_each(|(cratename, msg)| text.push_str(&format!("{}: '{}'", cratename, msg)));
|
|
|
|
|
2021-02-09 15:27:56 +00:00
|
|
|
let file = format!("lintcheck-logs/{}_logs.txt", filename);
|
|
|
|
write(file, text).unwrap();
|
2020-12-18 12:21:13 +00:00
|
|
|
}
|