2020-12-23 13:02:02 +01:00
|
|
|
// Run clippy on a fixed set of crates and collect the warnings.
|
2021-03-09 14:06:42 +01:00
|
|
|
// This helps observing the impact clippy changes have on a set of real-world code (and not just our
|
|
|
|
// testsuite).
|
2020-12-23 13:02:02 +01:00
|
|
|
//
|
|
|
|
// When a new lint is introduced, we can search the results for new warnings and check for false
|
|
|
|
// positives.
|
|
|
|
|
2021-03-02 20:20:51 +01:00
|
|
|
#![allow(clippy::filter_map, clippy::collapsible_else_if)]
|
2020-12-18 22:53:45 +01:00
|
|
|
|
2021-03-11 14:29:00 +01:00
|
|
|
use std::ffi::OsStr;
|
2020-12-18 13:21:13 +01:00
|
|
|
use std::process::Command;
|
2021-02-18 19:09:12 +01:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2021-03-05 11:10:15 +01:00
|
|
|
use std::{collections::HashMap, io::ErrorKind};
|
2021-03-02 20:20:51 +01:00
|
|
|
use std::{
|
|
|
|
env, fmt,
|
|
|
|
fs::write,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
2020-12-18 22:53:45 +01:00
|
|
|
|
2021-03-09 14:40:59 +01:00
|
|
|
use clap::{App, Arg, ArgMatches};
|
2021-02-18 19:09:12 +01:00
|
|
|
use rayon::prelude::*;
|
2020-12-23 13:02:02 +01:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-12-23 15:00:51 +01:00
|
|
|
use serde_json::Value;
|
2020-12-23 13:02:02 +01:00
|
|
|
|
2021-02-27 12:05:27 +01:00
|
|
|
const CLIPPY_DRIVER_PATH: &str = "target/debug/clippy-driver";
|
|
|
|
const CARGO_CLIPPY_PATH: &str = "target/debug/cargo-clippy";
|
|
|
|
|
2021-02-28 12:36:56 +01:00
|
|
|
const LINTCHECK_DOWNLOADS: &str = "target/lintcheck/downloads";
|
|
|
|
const LINTCHECK_SOURCES: &str = "target/lintcheck/sources";
|
|
|
|
|
2021-02-14 18:51:53 +01:00
|
|
|
/// List of sources to check, loaded from a .toml file
|
2020-12-23 13:03:19 +01:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2021-02-14 18:51:53 +01:00
|
|
|
struct SourceList {
|
2021-02-05 23:13:59 +01:00
|
|
|
crates: HashMap<String, TomlCrate>,
|
2020-12-23 13:03:19 +01:00
|
|
|
}
|
|
|
|
|
2021-02-14 18:51:53 +01:00
|
|
|
/// A crate source stored inside the .toml
|
|
|
|
/// will be translated into on one of the `CrateSource` variants
|
2021-02-05 23:13:59 +01:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2020-12-23 13:02:02 +01:00
|
|
|
struct TomlCrate {
|
2020-12-23 01:21:31 +01:00
|
|
|
name: String,
|
2021-02-05 23:13:59 +01:00
|
|
|
versions: Option<Vec<String>>,
|
|
|
|
git_url: Option<String>,
|
|
|
|
git_hash: Option<String>,
|
2021-02-10 11:32:10 +01:00
|
|
|
path: Option<String>,
|
2021-02-16 13:38:01 +01:00
|
|
|
options: Option<Vec<String>>,
|
2020-12-23 01:21:31 +01:00
|
|
|
}
|
|
|
|
|
2021-02-14 18:51:53 +01:00
|
|
|
/// Represents an archive we download from crates.io, or a git repo, or a local repo/folder
|
|
|
|
/// Once processed (downloaded/extracted/cloned/copied...), this will be translated into a `Crate`
|
2021-02-19 22:16:53 +01:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Eq, Hash, PartialEq, Ord, PartialOrd)]
|
2021-02-06 12:02:42 +01:00
|
|
|
enum CrateSource {
|
2021-02-16 13:38:01 +01:00
|
|
|
CratesIo {
|
|
|
|
name: String,
|
|
|
|
version: String,
|
|
|
|
options: Option<Vec<String>>,
|
|
|
|
},
|
|
|
|
Git {
|
|
|
|
name: String,
|
|
|
|
url: String,
|
|
|
|
commit: String,
|
|
|
|
options: Option<Vec<String>>,
|
|
|
|
},
|
|
|
|
Path {
|
|
|
|
name: String,
|
|
|
|
path: PathBuf,
|
|
|
|
options: Option<Vec<String>>,
|
|
|
|
},
|
2020-12-18 13:21:13 +01:00
|
|
|
}
|
|
|
|
|
2021-02-14 18:51:53 +01:00
|
|
|
/// Represents the actual source code of a crate that we ran "cargo clippy" on
|
2020-12-18 14:14:15 +01:00
|
|
|
#[derive(Debug)]
|
2020-12-23 13:02:02 +01:00
|
|
|
struct Crate {
|
2020-12-18 13:21:13 +01:00
|
|
|
version: String,
|
|
|
|
name: String,
|
2020-12-18 20:58:46 +01:00
|
|
|
// path to the extracted sources that clippy can check
|
2020-12-18 14:14:15 +01:00
|
|
|
path: PathBuf,
|
2021-02-16 13:38:01 +01:00
|
|
|
options: Option<Vec<String>>,
|
2020-12-18 13:21:13 +01:00
|
|
|
}
|
|
|
|
|
2021-02-14 18:51:53 +01:00
|
|
|
/// A single warning that clippy issued while checking a `Crate`
|
2020-12-23 15:00:51 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct ClippyWarning {
|
|
|
|
crate_name: String,
|
|
|
|
crate_version: String,
|
|
|
|
file: String,
|
|
|
|
line: String,
|
|
|
|
column: String,
|
|
|
|
linttype: String,
|
|
|
|
message: String,
|
2021-02-14 18:37:08 +01:00
|
|
|
is_ice: bool,
|
2020-12-23 15:00:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for ClippyWarning {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
writeln!(
|
|
|
|
f,
|
2021-02-27 12:29:13 +01:00
|
|
|
r#"target/lintcheck/sources/{}-{}/{}:{}:{} {} "{}""#,
|
2020-12-23 15:00:51 +01:00
|
|
|
&self.crate_name, &self.crate_version, &self.file, &self.line, &self.column, &self.linttype, &self.message
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 13:02:02 +01:00
|
|
|
impl CrateSource {
|
2021-02-14 18:51:53 +01:00
|
|
|
/// Makes the sources available on the disk for clippy to check.
|
|
|
|
/// Clones a git repo and checks out the specified commit or downloads a crate from crates.io or
|
|
|
|
/// copies a local folder
|
2020-12-23 13:02:02 +01:00
|
|
|
fn download_and_extract(&self) -> Crate {
|
2021-02-06 12:02:42 +01:00
|
|
|
match self {
|
2021-02-16 13:38:01 +01:00
|
|
|
CrateSource::CratesIo { name, version, options } => {
|
2021-02-28 12:36:56 +01:00
|
|
|
let extract_dir = PathBuf::from(LINTCHECK_SOURCES);
|
|
|
|
let krate_download_dir = PathBuf::from(LINTCHECK_DOWNLOADS);
|
2021-02-06 12:02:42 +01:00
|
|
|
|
|
|
|
// 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);
|
2021-03-05 14:06:43 +01:00
|
|
|
create_dirs(&krate_download_dir, &extract_dir);
|
2021-02-06 12:02:42 +01:00
|
|
|
|
|
|
|
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)),
|
2021-02-16 13:38:01 +01:00
|
|
|
options: options.clone(),
|
2021-02-06 12:02:42 +01:00
|
|
|
}
|
|
|
|
},
|
2021-02-16 13:38:01 +01:00
|
|
|
CrateSource::Git {
|
|
|
|
name,
|
|
|
|
url,
|
|
|
|
commit,
|
|
|
|
options,
|
|
|
|
} => {
|
2021-02-06 12:02:42 +01:00
|
|
|
let repo_path = {
|
2021-02-28 12:36:56 +01:00
|
|
|
let mut repo_path = PathBuf::from(LINTCHECK_SOURCES);
|
2021-02-06 12:02:42 +01: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 11:36:06 +01:00
|
|
|
println!("Cloning {} and checking out {}", url, commit);
|
2021-02-15 22:46:58 +01:00
|
|
|
if !Command::new("git")
|
2021-02-06 12:02:42 +01:00
|
|
|
.arg("clone")
|
|
|
|
.arg(url)
|
|
|
|
.arg(&repo_path)
|
2021-02-15 22:46:58 +01:00
|
|
|
.status()
|
|
|
|
.expect("Failed to clone git repo!")
|
|
|
|
.success()
|
|
|
|
{
|
|
|
|
eprintln!("Failed to clone {} into {}", url, repo_path.display())
|
|
|
|
}
|
2021-02-06 12:02:42 +01:00
|
|
|
}
|
|
|
|
// check out the commit/branch/whatever
|
2021-02-15 22:46:58 +01:00
|
|
|
if !Command::new("git")
|
2021-02-06 12:02:42 +01:00
|
|
|
.arg("checkout")
|
|
|
|
.arg(commit)
|
2021-02-15 22:36:49 +01:00
|
|
|
.current_dir(&repo_path)
|
2021-02-15 22:46:58 +01:00
|
|
|
.status()
|
|
|
|
.expect("Failed to check out commit")
|
|
|
|
.success()
|
|
|
|
{
|
|
|
|
eprintln!("Failed to checkout {} of repo at {}", commit, repo_path.display())
|
|
|
|
}
|
2021-02-06 12:02:42 +01:00
|
|
|
|
|
|
|
Crate {
|
|
|
|
version: commit.clone(),
|
|
|
|
name: name.clone(),
|
|
|
|
path: repo_path,
|
2021-02-16 13:38:01 +01:00
|
|
|
options: options.clone(),
|
2021-02-06 12:02:42 +01:00
|
|
|
}
|
|
|
|
},
|
2021-02-16 13:38:01 +01:00
|
|
|
CrateSource::Path { name, path, options } => {
|
2021-02-10 11:32:10 +01:00
|
|
|
use fs_extra::dir;
|
|
|
|
|
|
|
|
// simply copy the entire directory into our target dir
|
2021-02-28 12:36:56 +01:00
|
|
|
let copy_dest = PathBuf::from(format!("{}/", LINTCHECK_SOURCES));
|
2021-02-10 11:32:10 +01:00
|
|
|
|
|
|
|
// the source path of the crate we copied, ${copy_dest}/crate_name
|
|
|
|
let crate_root = copy_dest.join(name); // .../crates/local_crate
|
|
|
|
|
2021-03-05 11:10:15 +01:00
|
|
|
if crate_root.exists() {
|
2021-02-10 11:32:10 +01:00
|
|
|
println!(
|
|
|
|
"Not copying {} to {}, destination already exists",
|
|
|
|
path.display(),
|
|
|
|
crate_root.display()
|
|
|
|
);
|
2021-03-05 11:10:15 +01:00
|
|
|
} else {
|
|
|
|
println!("Copying {} to {}", path.display(), copy_dest.display());
|
|
|
|
|
|
|
|
dir::copy(path, ©_dest, &dir::CopyOptions::new()).unwrap_or_else(|_| {
|
|
|
|
panic!("Failed to copy from {}, to {}", path.display(), crate_root.display())
|
|
|
|
});
|
2021-02-10 11:32:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Crate {
|
|
|
|
version: String::from("local"),
|
|
|
|
name: name.clone(),
|
|
|
|
path: crate_root,
|
2021-02-16 13:38:01 +01:00
|
|
|
options: options.clone(),
|
2021-02-10 11:32:10 +01:00
|
|
|
}
|
|
|
|
},
|
2020-12-18 13:21:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 13:02:02 +01:00
|
|
|
impl Crate {
|
2021-02-14 18:51:53 +01:00
|
|
|
/// Run `cargo clippy` on the `Crate` and collect and return all the lint warnings that clippy
|
|
|
|
/// issued
|
2021-02-18 19:09:12 +01:00
|
|
|
fn run_clippy_lints(
|
|
|
|
&self,
|
2021-03-02 20:20:51 +01:00
|
|
|
cargo_clippy_path: &Path,
|
2021-02-18 19:09:12 +01:00
|
|
|
target_dir_index: &AtomicUsize,
|
|
|
|
thread_limit: usize,
|
2021-02-19 22:06:50 +01:00
|
|
|
total_crates_to_lint: usize,
|
2021-03-05 09:30:12 +01:00
|
|
|
fix: bool,
|
2021-02-18 19:09:12 +01:00
|
|
|
) -> Vec<ClippyWarning> {
|
|
|
|
// advance the atomic index by one
|
2021-02-19 22:06:50 +01:00
|
|
|
let index = target_dir_index.fetch_add(1, Ordering::SeqCst);
|
2021-02-18 19:09:12 +01:00
|
|
|
// "loop" the index within 0..thread_limit
|
2021-03-05 11:10:15 +01:00
|
|
|
let thread_index = index % thread_limit;
|
|
|
|
let perc = (index * 100) / total_crates_to_lint;
|
2021-02-19 23:20:05 +01:00
|
|
|
|
|
|
|
if thread_limit == 1 {
|
|
|
|
println!(
|
|
|
|
"{}/{} {}% Linting {} {}",
|
|
|
|
index, total_crates_to_lint, perc, &self.name, &self.version
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
println!(
|
|
|
|
"{}/{} {}% Linting {} {} in target dir {:?}",
|
2021-03-05 11:10:15 +01:00
|
|
|
index, total_crates_to_lint, perc, &self.name, &self.version, thread_index
|
2021-02-19 23:20:05 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-18 17:25:07 +01:00
|
|
|
let cargo_clippy_path = std::fs::canonicalize(cargo_clippy_path).unwrap();
|
|
|
|
|
2021-02-18 19:09:12 +01:00
|
|
|
let shared_target_dir = clippy_project_root().join("target/lintcheck/shared_target_dir");
|
2020-12-18 22:08:18 +01:00
|
|
|
|
2021-03-05 09:30:12 +01:00
|
|
|
let mut args = if fix {
|
|
|
|
vec![
|
|
|
|
"-Zunstable-options",
|
|
|
|
"--fix",
|
|
|
|
"-Zunstable-options",
|
|
|
|
"--allow-no-vcs",
|
|
|
|
"--",
|
|
|
|
"--cap-lints=warn",
|
|
|
|
]
|
|
|
|
} else {
|
|
|
|
vec!["--", "--message-format=json", "--", "--cap-lints=warn"]
|
|
|
|
};
|
2021-02-16 13:38:01 +01:00
|
|
|
|
|
|
|
if let Some(options) = &self.options {
|
|
|
|
for opt in options {
|
|
|
|
args.push(opt);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
args.extend(&["-Wclippy::pedantic", "-Wclippy::cargo"])
|
|
|
|
}
|
|
|
|
|
2021-02-05 23:13:59 +01:00
|
|
|
let all_output = std::process::Command::new(&cargo_clippy_path)
|
2021-02-18 19:09:12 +01:00
|
|
|
// use the looping index to create individual target dirs
|
2021-02-19 22:06:50 +01:00
|
|
|
.env(
|
|
|
|
"CARGO_TARGET_DIR",
|
2021-03-05 11:10:15 +01:00
|
|
|
shared_target_dir.join(format!("_{:?}", thread_index)),
|
2021-02-19 22:06:50 +01:00
|
|
|
)
|
2020-12-18 20:58:46 +01:00
|
|
|
// lint warnings will look like this:
|
|
|
|
// src/cargo/ops/cargo_compile.rs:127:35: warning: usage of `FromIterator::from_iter`
|
2021-02-16 13:38:01 +01:00
|
|
|
.args(&args)
|
2020-12-18 20:58:46 +01:00
|
|
|
.current_dir(&self.path)
|
2020-12-18 18:34:09 +01:00
|
|
|
.output()
|
2021-02-05 23:13:59 +01:00
|
|
|
.unwrap_or_else(|error| {
|
2021-02-06 19:12:28 +01:00
|
|
|
panic!(
|
|
|
|
"Encountered error:\n{:?}\ncargo_clippy_path: {}\ncrate path:{}\n",
|
|
|
|
error,
|
|
|
|
&cargo_clippy_path.display(),
|
|
|
|
&self.path.display()
|
|
|
|
);
|
2021-02-05 23:13:59 +01:00
|
|
|
});
|
2020-12-23 15:00:51 +01:00
|
|
|
let stdout = String::from_utf8_lossy(&all_output.stdout);
|
2021-03-05 09:30:12 +01:00
|
|
|
let stderr = String::from_utf8_lossy(&all_output.stderr);
|
|
|
|
|
|
|
|
if fix {
|
|
|
|
if let Some(stderr) = stderr
|
|
|
|
.lines()
|
|
|
|
.find(|line| line.contains("failed to automatically apply fixes suggested by rustc to crate"))
|
|
|
|
{
|
|
|
|
let subcrate = &stderr[63..];
|
|
|
|
println!(
|
|
|
|
"ERROR: failed to apply some suggetion to {} / to (sub)crate {}",
|
|
|
|
self.name, subcrate
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// fast path, we don't need the warnings anyway
|
|
|
|
return Vec::new();
|
|
|
|
}
|
|
|
|
|
2020-12-23 15:00:51 +01:00
|
|
|
let output_lines = stdout.lines();
|
|
|
|
let warnings: Vec<ClippyWarning> = output_lines
|
2020-12-18 18:34:09 +01:00
|
|
|
.into_iter()
|
2021-02-10 12:50:36 +01:00
|
|
|
// get all clippy warnings and ICEs
|
2021-02-15 23:13:41 +01:00
|
|
|
.filter(|line| filter_clippy_warnings(&line))
|
2020-12-23 15:00:51 +01:00
|
|
|
.map(|json_msg| parse_json_message(json_msg, &self))
|
2020-12-18 18:34:09 +01:00
|
|
|
.collect();
|
2021-03-05 09:30:12 +01:00
|
|
|
|
2020-12-23 15:31:18 +01:00
|
|
|
warnings
|
2020-12-18 13:21:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-27 00:29:42 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct LintcheckConfig {
|
|
|
|
// max number of jobs to spawn (default 1)
|
|
|
|
max_jobs: usize,
|
|
|
|
// we read the sources to check from here
|
|
|
|
sources_toml_path: PathBuf,
|
|
|
|
// we save the clippy lint results here
|
|
|
|
lintcheck_results_path: PathBuf,
|
2021-03-05 09:30:12 +01:00
|
|
|
// whether to just run --fix and not collect all the warnings
|
|
|
|
fix: bool,
|
2021-02-27 00:29:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LintcheckConfig {
|
|
|
|
fn from_clap(clap_config: &ArgMatches) -> Self {
|
|
|
|
// first, check if we got anything passed via the LINTCHECK_TOML env var,
|
|
|
|
// if not, ask clap if we got any value for --crates-toml <foo>
|
2021-03-09 14:40:59 +01:00
|
|
|
// if not, use the default "lintcheck/lintcheck_crates.toml"
|
2021-03-02 20:20:51 +01:00
|
|
|
let sources_toml = env::var("LINTCHECK_TOML").unwrap_or_else(|_| {
|
2021-02-27 00:29:42 +01:00
|
|
|
clap_config
|
|
|
|
.value_of("crates-toml")
|
|
|
|
.clone()
|
2021-03-09 14:40:59 +01:00
|
|
|
.unwrap_or("lintcheck/lintcheck_crates.toml")
|
2021-03-02 20:20:51 +01:00
|
|
|
.to_string()
|
|
|
|
});
|
2021-02-27 00:29:42 +01:00
|
|
|
|
|
|
|
let sources_toml_path = PathBuf::from(sources_toml);
|
|
|
|
|
2021-02-28 13:59:47 +01:00
|
|
|
// for the path where we save the lint results, get the filename without extension (so for
|
|
|
|
// wasd.toml, use "wasd"...)
|
2021-02-27 00:29:42 +01:00
|
|
|
let filename: PathBuf = sources_toml_path.file_stem().unwrap().into();
|
|
|
|
let lintcheck_results_path = PathBuf::from(format!("lintcheck-logs/{}_logs.txt", filename.display()));
|
|
|
|
|
2021-02-27 12:05:27 +01:00
|
|
|
// look at the --threads arg, if 0 is passed, ask rayon rayon how many threads it would spawn and
|
|
|
|
// use half of that for the physical core count
|
|
|
|
// by default use a single thread
|
2021-02-27 00:29:42 +01:00
|
|
|
let max_jobs = match clap_config.value_of("threads") {
|
|
|
|
Some(threads) => {
|
|
|
|
let threads: usize = threads
|
|
|
|
.parse()
|
2021-03-02 20:20:51 +01:00
|
|
|
.unwrap_or_else(|_| panic!("Failed to parse '{}' to a digit", threads));
|
2021-02-27 00:29:42 +01:00
|
|
|
if threads == 0 {
|
|
|
|
// automatic choice
|
|
|
|
// Rayon seems to return thread count so half that for core count
|
|
|
|
(rayon::current_num_threads() / 2) as usize
|
|
|
|
} else {
|
|
|
|
threads
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// no -j passed, use a single thread
|
|
|
|
None => 1,
|
|
|
|
};
|
2021-03-05 09:30:12 +01:00
|
|
|
let fix: bool = clap_config.is_present("fix");
|
2021-02-27 00:29:42 +01:00
|
|
|
|
|
|
|
LintcheckConfig {
|
|
|
|
max_jobs,
|
|
|
|
sources_toml_path,
|
|
|
|
lintcheck_results_path,
|
2021-03-05 09:30:12 +01:00
|
|
|
fix,
|
2021-02-27 00:29:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 23:13:41 +01:00
|
|
|
/// takes a single json-formatted clippy warnings and returns true (we are interested in that line)
|
|
|
|
/// or false (we aren't)
|
|
|
|
fn filter_clippy_warnings(line: &str) -> bool {
|
|
|
|
// we want to collect ICEs because clippy might have crashed.
|
|
|
|
// these are summarized later
|
|
|
|
if line.contains("internal compiler error: ") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// in general, we want all clippy warnings
|
|
|
|
// however due to some kind of bug, sometimes there are absolute paths
|
|
|
|
// to libcore files inside the message
|
|
|
|
// or we end up with cargo-metadata output (https://github.com/rust-lang/rust-clippy/issues/6508)
|
|
|
|
|
|
|
|
// filter out these message to avoid unnecessary noise in the logs
|
|
|
|
if line.contains("clippy::")
|
|
|
|
&& !(line.contains("could not read cargo metadata")
|
|
|
|
|| (line.contains(".rustup") && line.contains("toolchains")))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2021-02-14 18:51:53 +01:00
|
|
|
/// Builds clippy inside the repo to make sure we have a clippy executable we can use.
|
2020-12-18 13:21:13 +01:00
|
|
|
fn build_clippy() {
|
2021-02-19 21:52:34 +01:00
|
|
|
let status = Command::new("cargo")
|
2020-12-18 13:21:13 +01:00
|
|
|
.arg("build")
|
2021-02-19 21:52:34 +01:00
|
|
|
.status()
|
2020-12-18 13:21:13 +01:00
|
|
|
.expect("Failed to build clippy!");
|
2021-02-19 21:52:34 +01:00
|
|
|
if !status.success() {
|
|
|
|
eprintln!("Error: Failed to compile Clippy!");
|
|
|
|
std::process::exit(1);
|
2021-02-16 16:58:00 +01:00
|
|
|
}
|
2020-12-18 13:21:13 +01:00
|
|
|
}
|
|
|
|
|
2021-02-14 18:51:53 +01:00
|
|
|
/// Read a `toml` file and return a list of `CrateSources` that we want to check with clippy
|
2021-03-02 20:20:51 +01:00
|
|
|
fn read_crates(toml_path: &Path) -> Vec<CrateSource> {
|
2020-12-22 13:07:55 +01:00
|
|
|
let toml_content: String =
|
|
|
|
std::fs::read_to_string(&toml_path).unwrap_or_else(|_| panic!("Failed to read {}", toml_path.display()));
|
2021-02-14 18:51:53 +01:00
|
|
|
let crate_list: SourceList =
|
2020-12-22 13:07:55 +01:00
|
|
|
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 13:02:02 +01:00
|
|
|
let tomlcrates: Vec<TomlCrate> = crate_list
|
2020-12-22 13:07:55 +01:00
|
|
|
.crates
|
2020-12-23 01:21:31 +01:00
|
|
|
.into_iter()
|
2021-02-05 23:13:59 +01:00
|
|
|
.map(|(_cratename, tomlcrate)| tomlcrate)
|
2020-12-23 01:21:31 +01:00
|
|
|
.collect();
|
|
|
|
|
2020-12-23 13:02:02 +01: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 11:32:10 +01:00
|
|
|
if let Some(ref path) = tk.path {
|
|
|
|
crate_sources.push(CrateSource::Path {
|
|
|
|
name: tk.name.clone(),
|
|
|
|
path: PathBuf::from(path),
|
2021-02-16 13:38:01 +01:00
|
|
|
options: tk.options.clone(),
|
2021-02-10 11:32:10 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-06 12:02:42 +01:00
|
|
|
// if we have multiple versions, save each one
|
2021-02-05 23:13:59 +01:00
|
|
|
if let Some(ref versions) = tk.versions {
|
|
|
|
versions.iter().for_each(|ver| {
|
2021-02-06 12:02:42 +01:00
|
|
|
crate_sources.push(CrateSource::CratesIo {
|
2021-02-05 23:13:59 +01:00
|
|
|
name: tk.name.clone(),
|
|
|
|
version: ver.to_string(),
|
2021-02-16 13:38:01 +01:00
|
|
|
options: tk.options.clone(),
|
2021-02-05 23:13:59 +01:00
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
2021-02-06 12:02:42 +01: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(),
|
2021-02-16 13:38:01 +01:00
|
|
|
options: tk.options.clone(),
|
2021-02-06 12:02:42 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
// 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 19:12:28 +01:00
|
|
|
eprintln!("tomlkrate: {:?}", tk);
|
2021-02-06 12:04:31 +01:00
|
|
|
if tk.git_hash.is_some() != tk.git_url.is_some() {
|
2021-02-10 11:32:10 +01: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 12:04:31 +01:00
|
|
|
}
|
2021-02-06 12:02:42 +01:00
|
|
|
unreachable!("Failed to translate TomlCrate into CrateSource!");
|
|
|
|
}
|
2020-12-23 01:21:31 +01:00
|
|
|
});
|
2021-02-19 22:16:53 +01:00
|
|
|
// sort the crates
|
|
|
|
crate_sources.sort();
|
|
|
|
|
2021-02-27 00:29:42 +01:00
|
|
|
crate_sources
|
2020-12-22 13:07:55 +01:00
|
|
|
}
|
|
|
|
|
2021-02-14 18:51:53 +01:00
|
|
|
/// Parse the json output of clippy and return a `ClippyWarning`
|
2020-12-23 15:00:51 +01:00
|
|
|
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));
|
|
|
|
|
2021-03-11 14:29:00 +01:00
|
|
|
let file: String = jmsg["message"]["spans"][0]["file_name"]
|
|
|
|
.to_string()
|
|
|
|
.trim_matches('"')
|
|
|
|
.into();
|
|
|
|
|
|
|
|
let file = if file.contains(".cargo") {
|
|
|
|
// if we deal with macros, a filename may show the origin of a macro which can be inside a dep from
|
|
|
|
// the registry.
|
|
|
|
// don't show the full path in that case.
|
|
|
|
|
|
|
|
// /home/matthias/.cargo/registry/src/github.com-1ecc6299db9ec823/syn-1.0.63/src/custom_keyword.rs
|
|
|
|
let path = PathBuf::from(file);
|
|
|
|
let mut piter = path.into_iter();
|
|
|
|
// consume all elements until we find ".cargo", so that "/home/matthias" is skipped
|
|
|
|
let _: Option<&OsStr> = piter.find(|x| x == &std::ffi::OsString::from(".cargo"));
|
|
|
|
// collect the remaining segments
|
|
|
|
let file = piter.collect::<PathBuf>();
|
|
|
|
format!("{}", file.display())
|
|
|
|
} else {
|
|
|
|
file
|
|
|
|
};
|
|
|
|
|
2020-12-23 15:00:51 +01:00
|
|
|
ClippyWarning {
|
|
|
|
crate_name: krate.name.to_string(),
|
|
|
|
crate_version: krate.version.to_string(),
|
2021-03-11 14:29:00 +01:00
|
|
|
file,
|
2020-12-23 15:00:51 +01:00
|
|
|
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-14 18:37:08 +01:00
|
|
|
is_ice: json_message.contains("internal compiler error: "),
|
2020-12-23 15:00:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 18:59:32 +01:00
|
|
|
/// Generate a short list of occuring lints-types and their count
|
2021-02-23 21:23:36 +01:00
|
|
|
fn gather_stats(clippy_warnings: &[ClippyWarning]) -> (String, HashMap<&String, usize>) {
|
2021-02-14 18:59:32 +01: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();
|
|
|
|
// 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));
|
|
|
|
|
2021-02-23 21:23:36 +01:00
|
|
|
let stats_string = stats
|
2021-02-14 18:59:32 +01:00
|
|
|
.iter()
|
|
|
|
.map(|(lint, count)| format!("{} {}\n", lint, count))
|
2021-02-23 21:23:36 +01:00
|
|
|
.collect::<String>();
|
|
|
|
|
|
|
|
(stats_string, counter)
|
2021-02-14 18:59:32 +01:00
|
|
|
}
|
|
|
|
|
2021-02-23 00:40:50 +01:00
|
|
|
/// check if the latest modification of the logfile is older than the modification date of the
|
|
|
|
/// clippy binary, if this is true, we should clean the lintchec shared target directory and recheck
|
2021-03-02 20:20:51 +01:00
|
|
|
fn lintcheck_needs_rerun(lintcheck_logs_path: &Path) -> bool {
|
2021-03-04 22:33:22 +01:00
|
|
|
if !lintcheck_logs_path.exists() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-23 00:40:50 +01:00
|
|
|
let clippy_modified: std::time::SystemTime = {
|
2021-02-27 12:05:27 +01:00
|
|
|
let mut times = [CLIPPY_DRIVER_PATH, CARGO_CLIPPY_PATH].iter().map(|p| {
|
|
|
|
std::fs::metadata(p)
|
|
|
|
.expect("failed to get metadata of file")
|
|
|
|
.modified()
|
|
|
|
.expect("failed to get modification date")
|
|
|
|
});
|
2021-02-23 21:23:36 +01:00
|
|
|
// the oldest modification of either of the binaries
|
2021-02-28 02:07:01 +01:00
|
|
|
std::cmp::max(times.next().unwrap(), times.next().unwrap())
|
2021-02-23 00:40:50 +01:00
|
|
|
};
|
|
|
|
|
2021-02-28 02:07:01 +01:00
|
|
|
let logs_modified: std::time::SystemTime = std::fs::metadata(lintcheck_logs_path)
|
2021-02-23 00:40:50 +01:00
|
|
|
.expect("failed to get metadata of file")
|
|
|
|
.modified()
|
|
|
|
.expect("failed to get modification date");
|
|
|
|
|
2021-02-28 02:07:01 +01:00
|
|
|
// time is represented in seconds since X
|
|
|
|
// logs_modified 2 and clippy_modified 5 means clippy binary is older and we need to recheck
|
|
|
|
logs_modified < clippy_modified
|
2021-02-23 00:40:50 +01:00
|
|
|
}
|
|
|
|
|
2021-03-09 14:06:42 +01:00
|
|
|
fn is_in_clippy_root() -> bool {
|
|
|
|
if let Ok(pb) = std::env::current_dir() {
|
|
|
|
if let Some(file) = pb.file_name() {
|
|
|
|
return file == PathBuf::from("rust-clippy");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2021-02-14 18:51:53 +01:00
|
|
|
/// lintchecks `main()` function
|
2021-03-05 11:10:15 +01:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
2021-03-09 14:06:42 +01:00
|
|
|
/// This function panics if the clippy binaries don't exist
|
|
|
|
/// or if lintcheck is executed from the wrong directory (aka none-repo-root)
|
2021-03-06 10:06:52 +01:00
|
|
|
pub fn main() {
|
2021-03-09 14:06:42 +01:00
|
|
|
// assert that we launch lintcheck from the repo root (via cargo dev-lintcheck)
|
|
|
|
if !is_in_clippy_root() {
|
2021-03-09 14:40:59 +01:00
|
|
|
eprintln!("lintcheck needs to be run from clippys repo root!\nUse `cargo lintcheck` alternatively.");
|
2021-03-09 14:06:42 +01:00
|
|
|
std::process::exit(3);
|
|
|
|
}
|
|
|
|
|
2021-03-06 10:06:52 +01:00
|
|
|
let clap_config = &get_clap_config();
|
|
|
|
|
2021-02-27 00:29:42 +01:00
|
|
|
let config = LintcheckConfig::from_clap(clap_config);
|
|
|
|
|
2020-12-18 16:17:53 +01:00
|
|
|
println!("Compiling clippy...");
|
2020-12-18 13:21:13 +01:00
|
|
|
build_clippy();
|
2020-12-18 16:17:53 +01:00
|
|
|
println!("Done compiling");
|
|
|
|
|
2021-02-23 00:40:50 +01:00
|
|
|
// if the clippy bin is newer than our logs, throw away target dirs to force clippy to
|
|
|
|
// refresh the logs
|
2021-02-28 02:07:01 +01:00
|
|
|
if lintcheck_needs_rerun(&config.lintcheck_results_path) {
|
2021-02-23 00:40:50 +01:00
|
|
|
let shared_target_dir = "target/lintcheck/shared_target_dir";
|
2021-03-02 20:20:51 +01:00
|
|
|
// if we get an Err here, the shared target dir probably does simply not exist
|
|
|
|
if let Ok(metadata) = std::fs::metadata(&shared_target_dir) {
|
|
|
|
if metadata.is_dir() {
|
|
|
|
println!("Clippy is newer than lint check logs, clearing lintcheck shared target dir...");
|
|
|
|
std::fs::remove_dir_all(&shared_target_dir)
|
|
|
|
.expect("failed to remove target/lintcheck/shared_target_dir");
|
|
|
|
}
|
2021-02-23 00:40:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-27 12:05:27 +01:00
|
|
|
let cargo_clippy_path: PathBuf = PathBuf::from(CARGO_CLIPPY_PATH)
|
2021-02-16 16:58:00 +01:00
|
|
|
.canonicalize()
|
|
|
|
.expect("failed to canonicalize path to clippy binary");
|
|
|
|
|
2020-12-18 13:21:13 +01:00
|
|
|
// assert that clippy is found
|
|
|
|
assert!(
|
|
|
|
cargo_clippy_path.is_file(),
|
2020-12-18 16:53:18 +01:00
|
|
|
"target/debug/cargo-clippy binary not found! {}",
|
|
|
|
cargo_clippy_path.display()
|
2020-12-18 13:21:13 +01:00
|
|
|
);
|
|
|
|
|
2021-02-27 12:05:27 +01:00
|
|
|
let clippy_ver = std::process::Command::new(CARGO_CLIPPY_PATH)
|
2020-12-29 16:18:31 +01:00
|
|
|
.arg("--version")
|
|
|
|
.output()
|
|
|
|
.map(|o| String::from_utf8_lossy(&o.stdout).into_owned())
|
|
|
|
.expect("could not get clippy version!");
|
|
|
|
|
2020-12-18 14:14:15 +01:00
|
|
|
// download and extract the crates, then run clippy on them and collect clippys warnings
|
2020-12-23 15:31:18 +01:00
|
|
|
// flatten into one big list of warnings
|
2020-12-22 13:07:55 +01:00
|
|
|
|
2021-02-27 00:29:42 +01:00
|
|
|
let crates = read_crates(&config.sources_toml_path);
|
|
|
|
let old_stats = read_stats_from_file(&config.lintcheck_results_path);
|
2020-12-27 16:20:32 +01:00
|
|
|
|
2021-02-28 02:07:01 +01:00
|
|
|
let counter = AtomicUsize::new(1);
|
|
|
|
|
2020-12-27 16:13:42 +01:00
|
|
|
let clippy_warnings: Vec<ClippyWarning> = if let Some(only_one_crate) = clap_config.value_of("only") {
|
2021-02-06 12:04:31 +01:00
|
|
|
// if we don't have the specified crate in the .toml, throw an error
|
|
|
|
if !crates.iter().any(|krate| {
|
|
|
|
let name = match krate {
|
2021-03-05 11:10:15 +01:00
|
|
|
CrateSource::CratesIo { name, .. } | CrateSource::Git { name, .. } | CrateSource::Path { name, .. } => {
|
|
|
|
name
|
|
|
|
},
|
2021-02-06 12:04:31 +01:00
|
|
|
};
|
|
|
|
name == only_one_crate
|
|
|
|
}) {
|
2020-12-27 16:20:32 +01:00
|
|
|
eprintln!(
|
2021-03-09 14:40:59 +01:00
|
|
|
"ERROR: could not find crate '{}' in lintcheck/lintcheck_crates.toml",
|
2020-12-27 16:20:32 +01:00
|
|
|
only_one_crate
|
|
|
|
);
|
|
|
|
std::process::exit(1);
|
2021-02-06 12:04:31 +01:00
|
|
|
}
|
2020-12-27 16:20:32 +01:00
|
|
|
|
|
|
|
// only check a single crate that was passed via cmdline
|
|
|
|
crates
|
2020-12-27 16:13:42 +01:00
|
|
|
.into_iter()
|
|
|
|
.map(|krate| krate.download_and_extract())
|
|
|
|
.filter(|krate| krate.name == only_one_crate)
|
2021-03-05 09:30:12 +01:00
|
|
|
.flat_map(|krate| krate.run_clippy_lints(&cargo_clippy_path, &AtomicUsize::new(0), 1, 1, config.fix))
|
2020-12-27 16:13:42 +01:00
|
|
|
.collect()
|
|
|
|
} else {
|
2021-02-27 01:34:45 +01:00
|
|
|
if config.max_jobs > 1 {
|
|
|
|
// run parallel with rayon
|
|
|
|
|
|
|
|
// Ask rayon for thread count. Assume that half of that is the number of physical cores
|
|
|
|
// Use one target dir for each core so that we can run N clippys in parallel.
|
|
|
|
// We need to use different target dirs because cargo would lock them for a single build otherwise,
|
|
|
|
// killing the parallelism. However this also means that deps will only be reused half/a
|
|
|
|
// quarter of the time which might result in a longer wall clock runtime
|
|
|
|
|
|
|
|
// This helps when we check many small crates with dep-trees that don't have a lot of branches in
|
|
|
|
// order to achive some kind of parallelism
|
|
|
|
|
|
|
|
// by default, use a single thread
|
|
|
|
let num_cpus = config.max_jobs;
|
|
|
|
let num_crates = crates.len();
|
|
|
|
|
|
|
|
// check all crates (default)
|
|
|
|
crates
|
|
|
|
.into_par_iter()
|
|
|
|
.map(|krate| krate.download_and_extract())
|
2021-03-05 09:30:12 +01:00
|
|
|
.flat_map(|krate| {
|
|
|
|
krate.run_clippy_lints(&cargo_clippy_path, &counter, num_cpus, num_crates, config.fix)
|
|
|
|
})
|
2021-02-27 01:34:45 +01:00
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
// run sequential
|
|
|
|
let num_crates = crates.len();
|
|
|
|
crates
|
|
|
|
.into_iter()
|
|
|
|
.map(|krate| krate.download_and_extract())
|
2021-03-05 09:30:12 +01:00
|
|
|
.flat_map(|krate| krate.run_clippy_lints(&cargo_clippy_path, &counter, 1, num_crates, config.fix))
|
2021-02-27 01:34:45 +01:00
|
|
|
.collect()
|
|
|
|
}
|
2020-12-27 16:13:42 +01:00
|
|
|
};
|
2020-12-18 18:34:09 +01:00
|
|
|
|
2021-03-05 09:30:12 +01:00
|
|
|
// if we are in --fix mode, don't change the log files, terminate here
|
|
|
|
if config.fix {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-14 18:59:32 +01:00
|
|
|
// generate some stats
|
2021-02-23 21:23:36 +01:00
|
|
|
let (stats_formatted, new_stats) = gather_stats(&clippy_warnings);
|
2020-12-23 15:59:16 +01:00
|
|
|
|
2021-02-10 12:50:36 +01:00
|
|
|
// grab crashes/ICEs, save the crate name and the ice message
|
|
|
|
let ices: Vec<(&String, &String)> = clippy_warnings
|
|
|
|
.iter()
|
2021-02-14 18:37:08 +01:00
|
|
|
.filter(|warning| warning.is_ice)
|
2021-02-10 12:50:36 +01:00
|
|
|
.map(|w| (&w.crate_name, &w.message))
|
|
|
|
.collect();
|
|
|
|
|
2021-03-05 11:10:15 +01:00
|
|
|
let mut all_msgs: Vec<String> = clippy_warnings.iter().map(ToString::to_string).collect();
|
2020-12-23 15:59:16 +01:00
|
|
|
all_msgs.sort();
|
2021-02-23 21:23:36 +01:00
|
|
|
all_msgs.push("\n\n\n\nStats:\n".into());
|
2020-12-23 15:59:16 +01:00
|
|
|
all_msgs.push(stats_formatted);
|
2020-12-18 18:34:09 +01:00
|
|
|
|
2021-01-23 00:25:29 +01:00
|
|
|
// save the text into lintcheck-logs/logs.txt
|
2020-12-29 16:18:31 +01:00
|
|
|
let mut text = clippy_ver; // clippy version number on top
|
|
|
|
text.push_str(&format!("\n{}", all_msgs.join("")));
|
2021-02-10 12:50:36 +01:00
|
|
|
text.push_str("ICEs:\n");
|
|
|
|
ices.iter()
|
|
|
|
.for_each(|(cratename, msg)| text.push_str(&format!("{}: '{}'", cratename, msg)));
|
|
|
|
|
2021-02-27 00:29:42 +01:00
|
|
|
println!("Writing logs to {}", config.lintcheck_results_path.display());
|
|
|
|
write(&config.lintcheck_results_path, text).unwrap();
|
2021-02-23 21:23:36 +01:00
|
|
|
|
|
|
|
print_stats(old_stats, new_stats);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// read the previous stats from the lintcheck-log file
|
2021-03-02 20:20:51 +01:00
|
|
|
fn read_stats_from_file(file_path: &Path) -> HashMap<String, usize> {
|
2021-02-23 21:23:36 +01:00
|
|
|
let file_content: String = match std::fs::read_to_string(file_path).ok() {
|
|
|
|
Some(content) => content,
|
|
|
|
None => {
|
|
|
|
return HashMap::new();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-03-05 11:10:15 +01:00
|
|
|
let lines: Vec<String> = file_content.lines().map(ToString::to_string).collect();
|
2021-02-23 21:23:36 +01:00
|
|
|
|
|
|
|
// search for the beginning "Stats:" and the end "ICEs:" of the section we want
|
|
|
|
let start = lines.iter().position(|line| line == "Stats:").unwrap();
|
|
|
|
let end = lines.iter().position(|line| line == "ICEs:").unwrap();
|
|
|
|
|
2021-03-05 11:10:15 +01:00
|
|
|
let stats_lines = &lines[start + 1..end];
|
2021-02-23 21:23:36 +01:00
|
|
|
|
|
|
|
stats_lines
|
2021-03-02 20:20:51 +01:00
|
|
|
.iter()
|
2021-02-23 21:23:36 +01:00
|
|
|
.map(|line| {
|
2021-03-02 20:20:51 +01:00
|
|
|
let mut spl = line.split(' ');
|
2021-02-23 21:23:36 +01:00
|
|
|
(
|
|
|
|
spl.next().unwrap().to_string(),
|
|
|
|
spl.next().unwrap().parse::<usize>().unwrap(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<HashMap<String, usize>>()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// print how lint counts changed between runs
|
|
|
|
fn print_stats(old_stats: HashMap<String, usize>, new_stats: HashMap<&String, usize>) {
|
|
|
|
let same_in_both_hashmaps = old_stats
|
|
|
|
.iter()
|
|
|
|
.filter(|(old_key, old_val)| new_stats.get::<&String>(&old_key) == Some(old_val))
|
|
|
|
.map(|(k, v)| (k.to_string(), *v))
|
|
|
|
.collect::<Vec<(String, usize)>>();
|
|
|
|
|
|
|
|
let mut old_stats_deduped = old_stats;
|
|
|
|
let mut new_stats_deduped = new_stats;
|
|
|
|
|
|
|
|
// remove duplicates from both hashmaps
|
|
|
|
same_in_both_hashmaps.iter().for_each(|(k, v)| {
|
|
|
|
assert!(old_stats_deduped.remove(k) == Some(*v));
|
|
|
|
assert!(new_stats_deduped.remove(k) == Some(*v));
|
|
|
|
});
|
|
|
|
|
|
|
|
println!("\nStats:");
|
|
|
|
|
|
|
|
// list all new counts (key is in new stats but not in old stats)
|
|
|
|
new_stats_deduped
|
|
|
|
.iter()
|
|
|
|
.filter(|(new_key, _)| old_stats_deduped.get::<str>(&new_key).is_none())
|
|
|
|
.for_each(|(new_key, new_value)| {
|
|
|
|
println!("{} 0 => {}", new_key, new_value);
|
|
|
|
});
|
|
|
|
|
|
|
|
// list all changed counts (key is in both maps but value differs)
|
|
|
|
new_stats_deduped
|
|
|
|
.iter()
|
|
|
|
.filter(|(new_key, _new_val)| old_stats_deduped.get::<str>(&new_key).is_some())
|
|
|
|
.for_each(|(new_key, new_val)| {
|
|
|
|
let old_val = old_stats_deduped.get::<str>(&new_key).unwrap();
|
|
|
|
println!("{} {} => {}", new_key, old_val, new_val);
|
|
|
|
});
|
|
|
|
|
|
|
|
// list all gone counts (key is in old status but not in new stats)
|
|
|
|
old_stats_deduped
|
|
|
|
.iter()
|
|
|
|
.filter(|(old_key, _)| new_stats_deduped.get::<&String>(&old_key).is_none())
|
|
|
|
.for_each(|(old_key, old_value)| {
|
|
|
|
println!("{} {} => 0", old_key, old_value);
|
|
|
|
});
|
2020-12-18 13:21:13 +01:00
|
|
|
}
|
2021-03-04 22:33:22 +01:00
|
|
|
|
2021-03-05 14:06:43 +01:00
|
|
|
/// Create necessary directories to run the lintcheck tool.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// This function panics if creating one of the dirs fails.
|
|
|
|
fn create_dirs(krate_download_dir: &Path, extract_dir: &Path) {
|
|
|
|
std::fs::create_dir("target/lintcheck/").unwrap_or_else(|err| {
|
|
|
|
if err.kind() != ErrorKind::AlreadyExists {
|
|
|
|
panic!("cannot create lintcheck target dir");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
std::fs::create_dir(&krate_download_dir).unwrap_or_else(|err| {
|
|
|
|
if err.kind() != ErrorKind::AlreadyExists {
|
|
|
|
panic!("cannot create crate download dir");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
std::fs::create_dir(&extract_dir).unwrap_or_else(|err| {
|
|
|
|
if err.kind() != ErrorKind::AlreadyExists {
|
|
|
|
panic!("cannot create crate extraction dir");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-06 10:06:52 +01:00
|
|
|
fn get_clap_config<'a>() -> ArgMatches<'a> {
|
2021-03-09 14:40:59 +01:00
|
|
|
App::new("lintcheck")
|
2021-03-06 10:06:52 +01:00
|
|
|
.about("run clippy on a set of crates and check output")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("only")
|
|
|
|
.takes_value(true)
|
|
|
|
.value_name("CRATE")
|
|
|
|
.long("only")
|
|
|
|
.help("only process a single crate of the list"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("crates-toml")
|
|
|
|
.takes_value(true)
|
|
|
|
.value_name("CRATES-SOURCES-TOML-PATH")
|
|
|
|
.long("crates-toml")
|
|
|
|
.help("set the path for a crates.toml where lintcheck should read the sources from"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("threads")
|
|
|
|
.takes_value(true)
|
|
|
|
.value_name("N")
|
|
|
|
.short("j")
|
|
|
|
.long("jobs")
|
|
|
|
.help("number of threads to use, 0 automatic choice"),
|
|
|
|
)
|
2021-03-11 15:18:56 +01:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("fix")
|
|
|
|
.long("--fix")
|
|
|
|
.help("runs cargo clippy --fix and checks if all suggestions apply"),
|
|
|
|
)
|
2021-03-09 14:40:59 +01:00
|
|
|
.get_matches()
|
2021-03-06 10:06:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the path to the Clippy project directory
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if the current directory could not be retrieved, there was an error reading any of the
|
|
|
|
/// Cargo.toml files or ancestor directory is the clippy root directory
|
|
|
|
#[must_use]
|
|
|
|
pub fn clippy_project_root() -> PathBuf {
|
|
|
|
let current_dir = std::env::current_dir().unwrap();
|
|
|
|
for path in current_dir.ancestors() {
|
|
|
|
let result = std::fs::read_to_string(path.join("Cargo.toml"));
|
|
|
|
if let Err(err) = &result {
|
|
|
|
if err.kind() == std::io::ErrorKind::NotFound {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let content = result.unwrap();
|
|
|
|
if content.contains("[package]\nname = \"clippy\"") {
|
|
|
|
return path.to_path_buf();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic!("error: Can't determine root of project. Please run inside a Clippy working dir.");
|
|
|
|
}
|
|
|
|
|
2021-03-04 22:33:22 +01:00
|
|
|
#[test]
|
|
|
|
fn lintcheck_test() {
|
|
|
|
let args = [
|
|
|
|
"run",
|
|
|
|
"--target-dir",
|
2021-03-09 14:40:59 +01:00
|
|
|
"lintcheck/target",
|
2021-03-04 22:33:22 +01:00
|
|
|
"--manifest-path",
|
2021-03-09 14:40:59 +01:00
|
|
|
"./lintcheck/Cargo.toml",
|
2021-03-04 22:33:22 +01:00
|
|
|
"--",
|
|
|
|
"--crates-toml",
|
2021-03-09 14:40:59 +01:00
|
|
|
"lintcheck/test_sources.toml",
|
2021-03-04 22:33:22 +01:00
|
|
|
];
|
|
|
|
let status = std::process::Command::new("cargo")
|
|
|
|
.args(&args)
|
2021-03-09 14:40:59 +01:00
|
|
|
.current_dir("..") // repo root
|
2021-03-04 22:33:22 +01:00
|
|
|
.status();
|
2021-03-09 14:40:59 +01:00
|
|
|
//.output();
|
2021-03-04 22:33:22 +01:00
|
|
|
|
|
|
|
assert!(status.unwrap().success());
|
|
|
|
}
|