1 // Run clippy on a fixed set of crates and collect the warnings.
2 // This helps observing the impact clippy changes have on a set of real-world code (and not just our
5 // When a new lint is introduced, we can search the results for new warnings and check for false
8 #![allow(clippy::collapsible_else_if)]
11 use std
::fmt
::Write
as _
;
12 use std
::process
::Command
;
13 use std
::sync
::atomic
::{AtomicUsize, Ordering}
;
14 use std
::{collections::HashMap, io::ErrorKind}
;
18 path
::{Path, PathBuf}
,
23 use clap
::{App, Arg, ArgMatches}
;
24 use rayon
::prelude
::*;
25 use serde
::{Deserialize, Serialize}
;
26 use serde_json
::Value
;
27 use walkdir
::{DirEntry, WalkDir}
;
30 const CLIPPY_DRIVER_PATH
: &str = "target/debug/clippy-driver";
32 const CARGO_CLIPPY_PATH
: &str = "target/debug/cargo-clippy";
35 const CLIPPY_DRIVER_PATH
: &str = "target/debug/clippy-driver.exe";
37 const CARGO_CLIPPY_PATH
: &str = "target/debug/cargo-clippy.exe";
39 const LINTCHECK_DOWNLOADS
: &str = "target/lintcheck/downloads";
40 const LINTCHECK_SOURCES
: &str = "target/lintcheck/sources";
42 /// List of sources to check, loaded from a .toml file
43 #[derive(Debug, Serialize, Deserialize)]
45 crates
: HashMap
<String
, TomlCrate
>,
48 /// A crate source stored inside the .toml
49 /// will be translated into on one of the `CrateSource` variants
50 #[derive(Debug, Serialize, Deserialize)]
53 versions
: Option
<Vec
<String
>>,
54 git_url
: Option
<String
>,
55 git_hash
: Option
<String
>,
57 options
: Option
<Vec
<String
>>,
60 /// Represents an archive we download from crates.io, or a git repo, or a local repo/folder
61 /// Once processed (downloaded/extracted/cloned/copied...), this will be translated into a `Crate`
62 #[derive(Debug, Serialize, Deserialize, Eq, Hash, PartialEq, Ord, PartialOrd)]
67 options
: Option
<Vec
<String
>>,
73 options
: Option
<Vec
<String
>>,
78 options
: Option
<Vec
<String
>>,
82 /// Represents the actual source code of a crate that we ran "cargo clippy" on
87 // path to the extracted sources that clippy can check
89 options
: Option
<Vec
<String
>>,
92 /// A single warning that clippy issued while checking a `Crate`
94 struct ClippyWarning
{
96 crate_version
: String
,
107 fn to_output(&self, markdown
: bool
) -> String
{
108 let file
= format
!("{}-{}/{}", &self.crate_name
, &self.crate_version
, &self.file
);
109 let file_with_pos
= format
!("{}:{}:{}", &file
, &self.line
, &self.column
);
111 let lint
= format
!("`{}`", self.linttype
);
113 let mut output
= String
::from("| ");
116 "[`{}`](../target/lintcheck/sources/{}#L{})",
117 file_with_pos
, file
, self.line
119 let _
= write
!(output
, r
#" | {:<50} | "{}" |"#, lint, self.message);
124 "target/lintcheck/sources/{} {} \"{}\"\n",
125 file_with_pos
, self.linttype
, self.message
131 fn get(path
: &str) -> Result
<ureq
::Response
, ureq
::Error
> {
132 const MAX_RETRIES
: u8 = 4;
135 match ureq
::get(path
).call() {
136 Ok(res
) => return Ok(res
),
137 Err(e
) if retries
>= MAX_RETRIES
=> return Err(e
),
138 Err(ureq
::Error
::Transport(e
)) => eprintln
!("Error: {}", e
),
139 Err(e
) => return Err(e
),
141 eprintln
!("retrying in {} seconds...", retries
);
142 thread
::sleep(Duration
::from_secs(retries
as u64));
148 /// Makes the sources available on the disk for clippy to check.
149 /// Clones a git repo and checks out the specified commit or downloads a crate from crates.io or
150 /// copies a local folder
151 fn download_and_extract(&self) -> Crate
{
153 CrateSource
::CratesIo { name, version, options }
=> {
154 let extract_dir
= PathBuf
::from(LINTCHECK_SOURCES
);
155 let krate_download_dir
= PathBuf
::from(LINTCHECK_DOWNLOADS
);
157 // url to download the crate from crates.io
158 let url
= format
!("https://crates.io/api/v1/crates/{}/{}/download", name
, version
);
159 println
!("Downloading and extracting {} {} from {}", name
, version
, url
);
160 create_dirs(&krate_download_dir
, &extract_dir
);
162 let krate_file_path
= krate_download_dir
.join(format
!("{}-{}.crate.tar.gz", name
, version
));
163 // don't download/extract if we already have done so
164 if !krate_file_path
.is_file() {
165 // create a file path to download and write the crate data into
166 let mut krate_dest
= std
::fs
::File
::create(&krate_file_path
).unwrap();
167 let mut krate_req
= get(&url
).unwrap().into_reader();
168 // copy the crate into the file
169 std
::io
::copy(&mut krate_req
, &mut krate_dest
).unwrap();
172 let ungz_tar
= flate2
::read
::GzDecoder
::new(std
::fs
::File
::open(&krate_file_path
).unwrap());
173 // extract the tar archive
174 let mut archive
= tar
::Archive
::new(ungz_tar
);
175 archive
.unpack(&extract_dir
).expect("Failed to extract!");
177 // crate is extracted, return a new Krate object which contains the path to the extracted
178 // sources that clippy can check
180 version
: version
.clone(),
182 path
: extract_dir
.join(format
!("{}-{}/", name
, version
)),
183 options
: options
.clone(),
193 let mut repo_path
= PathBuf
::from(LINTCHECK_SOURCES
);
194 // add a -git suffix in case we have the same crate from crates.io and a git repo
195 repo_path
.push(format
!("{}-git", name
));
198 // clone the repo if we have not done so
199 if !repo_path
.is_dir() {
200 println
!("Cloning {} and checking out {}", url
, commit
);
201 if !Command
::new("git")
206 .expect("Failed to clone git repo!")
209 eprintln
!("Failed to clone {} into {}", url
, repo_path
.display())
212 // check out the commit/branch/whatever
213 if !Command
::new("git")
216 .current_dir(&repo_path
)
218 .expect("Failed to check out commit")
221 eprintln
!("Failed to checkout {} of repo at {}", commit
, repo_path
.display())
225 version
: commit
.clone(),
228 options
: options
.clone(),
231 CrateSource
::Path { name, path, options }
=> {
232 // copy path into the dest_crate_root but skip directories that contain a CACHEDIR.TAG file.
233 // The target/ directory contains a CACHEDIR.TAG file so it is the most commonly skipped directory
234 // as a result of this filter.
235 let dest_crate_root
= PathBuf
::from(LINTCHECK_SOURCES
).join(name
);
236 if dest_crate_root
.exists() {
237 println
!("Deleting existing directory at {:?}", dest_crate_root
);
238 std
::fs
::remove_dir_all(&dest_crate_root
).unwrap();
241 println
!("Copying {:?} to {:?}", path
, dest_crate_root
);
243 fn is_cache_dir(entry
: &DirEntry
) -> bool
{
244 std
::fs
::read(entry
.path().join("CACHEDIR.TAG"))
245 .map(|x
| x
.starts_with(b
"Signature: 8a477f597d28d172789f06886806bc55"))
249 for entry
in WalkDir
::new(path
).into_iter().filter_entry(|e
| !is_cache_dir(e
)) {
250 let entry
= entry
.unwrap();
251 let entry_path
= entry
.path();
252 let relative_entry_path
= entry_path
.strip_prefix(path
).unwrap();
253 let dest_path
= dest_crate_root
.join(relative_entry_path
);
254 let metadata
= entry_path
.symlink_metadata().unwrap();
256 if metadata
.is_dir() {
257 std
::fs
::create_dir(dest_path
).unwrap();
258 } else if metadata
.is_file() {
259 std
::fs
::copy(entry_path
, dest_path
).unwrap();
264 version
: String
::from("local"),
266 path
: dest_crate_root
,
267 options
: options
.clone(),
275 /// Run `cargo clippy` on the `Crate` and collect and return all the lint warnings that clippy
279 cargo_clippy_path
: &Path
,
280 target_dir_index
: &AtomicUsize
,
282 total_crates_to_lint
: usize,
284 lint_filter
: &Vec
<String
>,
285 ) -> Vec
<ClippyWarning
> {
286 // advance the atomic index by one
287 let index
= target_dir_index
.fetch_add(1, Ordering
::SeqCst
);
288 // "loop" the index within 0..thread_limit
289 let thread_index
= index
% thread_limit
;
290 let perc
= (index
* 100) / total_crates_to_lint
;
292 if thread_limit
== 1 {
294 "{}/{} {}% Linting {} {}",
295 index
, total_crates_to_lint
, perc
, &self.name
, &self.version
299 "{}/{} {}% Linting {} {} in target dir {:?}",
300 index
, total_crates_to_lint
, perc
, &self.name
, &self.version
, thread_index
304 let cargo_clippy_path
= std
::fs
::canonicalize(cargo_clippy_path
).unwrap();
306 let shared_target_dir
= clippy_project_root().join("target/lintcheck/shared_target_dir");
308 let mut args
= if fix
{
311 vec
!["--", "--message-format=json", "--"]
314 if let Some(options
) = &self.options
{
319 args
.extend(&["-Wclippy::pedantic", "-Wclippy::cargo"])
322 if lint_filter
.is_empty() {
323 args
.push("--cap-lints=warn");
325 args
.push("--cap-lints=allow");
326 args
.extend(lint_filter
.iter().map(|filter
| filter
.as_str()))
329 let all_output
= std
::process
::Command
::new(&cargo_clippy_path
)
330 // use the looping index to create individual target dirs
333 shared_target_dir
.join(format
!("_{:?}", thread_index
)),
335 // lint warnings will look like this:
336 // src/cargo/ops/cargo_compile.rs:127:35: warning: usage of `FromIterator::from_iter`
338 .current_dir(&self.path
)
340 .unwrap_or_else(|error
| {
342 "Encountered error:\n{:?}\ncargo_clippy_path: {}\ncrate path:{}\n",
344 &cargo_clippy_path
.display(),
348 let stdout
= String
::from_utf8_lossy(&all_output
.stdout
);
349 let stderr
= String
::from_utf8_lossy(&all_output
.stderr
);
350 let status
= &all_output
.status
;
352 if !status
.success() {
354 "\nWARNING: bad exit status after checking {} {} \n",
355 self.name
, self.version
360 if let Some(stderr
) = stderr
362 .find(|line
| line
.contains("failed to automatically apply fixes suggested by rustc to crate"))
364 let subcrate
= &stderr
[63..];
366 "ERROR: failed to apply some suggetion to {} / to (sub)crate {}",
370 // fast path, we don't need the warnings anyway
374 let output_lines
= stdout
.lines();
375 let warnings
: Vec
<ClippyWarning
> = output_lines
377 // get all clippy warnings and ICEs
378 .filter(|line
| filter_clippy_warnings(&line
))
379 .map(|json_msg
| parse_json_message(json_msg
, &self))
387 struct LintcheckConfig
{
388 /// max number of jobs to spawn (default 1)
390 /// we read the sources to check from here
391 sources_toml_path
: PathBuf
,
392 /// we save the clippy lint results here
393 lintcheck_results_path
: PathBuf
,
394 /// whether to just run --fix and not collect all the warnings
396 /// A list of lints that this lintcheck run should focus on
397 lint_filter
: Vec
<String
>,
398 /// Indicate if the output should support markdown syntax
402 impl LintcheckConfig
{
403 fn from_clap(clap_config
: &ArgMatches
) -> Self {
404 // first, check if we got anything passed via the LINTCHECK_TOML env var,
405 // if not, ask clap if we got any value for --crates-toml <foo>
406 // if not, use the default "lintcheck/lintcheck_crates.toml"
407 let sources_toml
= env
::var("LINTCHECK_TOML").unwrap_or_else(|_
| {
409 .value_of("crates-toml")
411 .unwrap_or("lintcheck/lintcheck_crates.toml")
415 let markdown
= clap_config
.is_present("markdown");
416 let sources_toml_path
= PathBuf
::from(sources_toml
);
418 // for the path where we save the lint results, get the filename without extension (so for
419 // wasd.toml, use "wasd"...)
420 let filename
: PathBuf
= sources_toml_path
.file_stem().unwrap().into();
421 let lintcheck_results_path
= PathBuf
::from(format
!(
422 "lintcheck-logs/{}_logs.{}",
424 if markdown { "md" }
else { "txt" }
427 // look at the --threads arg, if 0 is passed, ask rayon rayon how many threads it would spawn and
428 // use half of that for the physical core count
429 // by default use a single thread
430 let max_jobs
= match clap_config
.value_of("threads") {
432 let threads
: usize = threads
434 .unwrap_or_else(|_
| panic
!("Failed to parse '{}' to a digit", threads
));
437 // Rayon seems to return thread count so half that for core count
438 (rayon
::current_num_threads() / 2) as usize
443 // no -j passed, use a single thread
446 let fix
: bool
= clap_config
.is_present("fix");
447 let lint_filter
: Vec
<String
> = clap_config
450 iter
.map(|lint_name
| {
451 let mut filter
= lint_name
.replace('_'
, "-");
452 if !filter
.starts_with("clippy::") {
453 filter
.insert_str(0, "clippy::");
459 .unwrap_or_default();
464 lintcheck_results_path
,
472 /// takes a single json-formatted clippy warnings and returns true (we are interested in that line)
473 /// or false (we aren't)
474 fn filter_clippy_warnings(line
: &str) -> bool
{
475 // we want to collect ICEs because clippy might have crashed.
476 // these are summarized later
477 if line
.contains("internal compiler error: ") {
480 // in general, we want all clippy warnings
481 // however due to some kind of bug, sometimes there are absolute paths
482 // to libcore files inside the message
483 // or we end up with cargo-metadata output (https://github.com/rust-lang/rust-clippy/issues/6508)
485 // filter out these message to avoid unnecessary noise in the logs
486 if line
.contains("clippy::")
487 && !(line
.contains("could not read cargo metadata")
488 || (line
.contains(".rustup") && line
.contains("toolchains")))
495 /// Builds clippy inside the repo to make sure we have a clippy executable we can use.
497 let status
= Command
::new("cargo")
500 .expect("Failed to build clippy!");
501 if !status
.success() {
502 eprintln
!("Error: Failed to compile Clippy!");
503 std
::process
::exit(1);
507 /// Read a `toml` file and return a list of `CrateSources` that we want to check with clippy
508 fn read_crates(toml_path
: &Path
) -> Vec
<CrateSource
> {
509 let toml_content
: String
=
510 std
::fs
::read_to_string(&toml_path
).unwrap_or_else(|_
| panic
!("Failed to read {}", toml_path
.display()));
511 let crate_list
: SourceList
=
512 toml
::from_str(&toml_content
).unwrap_or_else(|e
| panic
!("Failed to parse {}: \n{}", toml_path
.display(), e
));
513 // parse the hashmap of the toml file into a list of crates
514 let tomlcrates
: Vec
<TomlCrate
> = crate_list
517 .map(|(_cratename
, tomlcrate
)| tomlcrate
)
520 // flatten TomlCrates into CrateSources (one TomlCrates may represent several versions of a crate =>
521 // multiple Cratesources)
522 let mut crate_sources
= Vec
::new();
523 tomlcrates
.into_iter().for_each(|tk
| {
524 if let Some(ref path
) = tk
.path
{
525 crate_sources
.push(CrateSource
::Path
{
526 name
: tk
.name
.clone(),
527 path
: PathBuf
::from(path
),
528 options
: tk
.options
.clone(),
532 // if we have multiple versions, save each one
533 if let Some(ref versions
) = tk
.versions
{
534 versions
.iter().for_each(|ver
| {
535 crate_sources
.push(CrateSource
::CratesIo
{
536 name
: tk
.name
.clone(),
537 version
: ver
.to_string(),
538 options
: tk
.options
.clone(),
542 // otherwise, we should have a git source
543 if tk
.git_url
.is_some() && tk
.git_hash
.is_some() {
544 crate_sources
.push(CrateSource
::Git
{
545 name
: tk
.name
.clone(),
546 url
: tk
.git_url
.clone().unwrap(),
547 commit
: tk
.git_hash
.clone().unwrap(),
548 options
: tk
.options
.clone(),
551 // if we have a version as well as a git data OR only one git data, something is funky
552 if tk
.versions
.is_some() && (tk
.git_url
.is_some() || tk
.git_hash
.is_some())
553 || tk
.git_hash
.is_some() != tk
.git_url
.is_some()
555 eprintln
!("tomlkrate: {:?}", tk
);
556 if tk
.git_hash
.is_some() != tk
.git_url
.is_some() {
557 panic
!("Error: Encountered TomlCrate with only one of git_hash and git_url!");
559 if tk
.path
.is_some() && (tk
.git_hash
.is_some() || tk
.versions
.is_some()) {
560 panic
!("Error: TomlCrate can only have one of 'git_.*', 'version' or 'path' fields");
562 unreachable
!("Failed to translate TomlCrate into CrateSource!");
566 crate_sources
.sort();
571 /// Parse the json output of clippy and return a `ClippyWarning`
572 fn parse_json_message(json_message
: &str, krate
: &Crate
) -> ClippyWarning
{
573 let jmsg
: Value
= serde_json
::from_str(&json_message
).unwrap_or_else(|e
| panic
!("Failed to parse json:\n{:?}", e
));
575 let file
: String
= jmsg
["message"]["spans"][0]["file_name"]
580 let file = if file.contains(".cargo
") {
581 // if we deal with macros, a filename may show the origin of a macro which can be inside a dep from
583 // don't show the full path in that case.
585 // /home/matthias/.cargo/registry/src/github.com-1ecc6299db9ec823/syn-1.0.63/src/custom_keyword.rs
586 let path = PathBuf::from(file);
587 let mut piter = path.iter();
588 // consume all elements until we find ".cargo
", so that "/home
/matthias
" is skipped
589 let _: Option<&OsStr> = piter.find(|x| x == &std::ffi::OsString::from(".cargo
"));
590 // collect the remaining segments
591 let file = piter.collect::<PathBuf>();
592 format!("{}
", file.display())
598 crate_name: krate.name.to_string(),
599 crate_version: krate.version.to_string(),
601 line: jmsg["message
"]["spans
"][0]["line_start
"]
605 column
: jmsg
["message"]["spans"][0]["text"][0]["highlight_start"]
609 linttype: jmsg["message
"]["code
"]["code
"].to_string().trim_matches('"'
).into(),
610 message
: jmsg
["message"]["message"].to_string().trim_matches('
"').into(),
611 is_ice: json_message.contains("internal compiler error
: "),
615 /// Generate a short list of occurring lints-types and their count
616 fn gather_stats(clippy_warnings: &[ClippyWarning]) -> (String, HashMap<&String, usize>) {
617 // count lint type occurrences
618 let mut counter: HashMap<&String, usize> = HashMap::new();
621 .for_each(|wrn| *counter.entry(&wrn.linttype).or_insert(0) += 1);
623 // collect into a tupled list for sorting
624 let mut stats: Vec<(&&String, &usize)> = counter.iter().map(|(lint, count)| (lint, count)).collect();
625 // sort by "000{count} {clippy::lintname}
"
626 // to not have a lint with 200 and 2 warnings take the same spot
627 stats.sort_by_key(|(lint, count)| format!("{:0>4}
, {}
", count, lint));
629 let mut header = String::from("| lint
| count
|\n");
630 header.push_str("| -------------------------------------------------- | ----- |\n");
631 let stats_string = stats
633 .map(|(lint, count)| format!("| {:<50}
| {:>4}
|\n", lint, count))
634 .fold(header, |mut table, line| {
635 table.push_str(&line);
639 (stats_string, counter)
642 /// check if the latest modification of the logfile is older than the modification date of the
643 /// clippy binary, if this is true, we should clean the lintchec shared target directory and recheck
644 fn lintcheck_needs_rerun(lintcheck_logs_path: &Path) -> bool {
645 if !lintcheck_logs_path.exists() {
649 let clippy_modified: std::time::SystemTime = {
650 let mut times = [CLIPPY_DRIVER_PATH, CARGO_CLIPPY_PATH].iter().map(|p| {
652 .expect("failed to get metadata of file
")
654 .expect("failed to get modification date
")
656 // the oldest modification of either of the binaries
657 std::cmp::max(times.next().unwrap(), times.next().unwrap())
660 let logs_modified: std::time::SystemTime = std::fs::metadata(lintcheck_logs_path)
661 .expect("failed to get metadata of file
")
663 .expect("failed to get modification date
");
665 // time is represented in seconds since X
666 // logs_modified 2 and clippy_modified 5 means clippy binary is older and we need to recheck
667 logs_modified < clippy_modified
670 /// lintchecks `main()` function
674 /// This function panics if the clippy binaries don't exist
675 /// or if lintcheck is executed from the wrong directory (aka none-repo-root)
677 // assert that we launch lintcheck from the repo root (via cargo lintcheck)
678 if std::fs::metadata("lintcheck
/Cargo
.toml
").is_err() {
679 eprintln!("lintcheck needs to be run from clippys repo root
!\nUse `cargo lintcheck` alternatively
.");
680 std::process::exit(3);
683 let clap_config = &get_clap_config();
685 let config = LintcheckConfig::from_clap(clap_config);
687 println!("Compiling clippy
...");
689 println!("Done compiling
");
691 // if the clippy bin is newer than our logs, throw away target dirs to force clippy to
693 if lintcheck_needs_rerun(&config.lintcheck_results_path) {
694 let shared_target_dir = "target
/lintcheck
/shared_target_dir
";
695 // if we get an Err here, the shared target dir probably does simply not exist
696 if let Ok(metadata) = std::fs::metadata(&shared_target_dir) {
697 if metadata.is_dir() {
698 println!("Clippy is newer than lint check logs
, clearing lintcheck shared target dir
...");
699 std::fs::remove_dir_all(&shared_target_dir)
700 .expect("failed to remove target
/lintcheck
/shared_target_dir
");
705 let cargo_clippy_path: PathBuf = PathBuf::from(CARGO_CLIPPY_PATH)
707 .expect("failed to canonicalize path to clippy binary
");
709 // assert that clippy is found
711 cargo_clippy_path.is_file(),
712 "target
/debug
/cargo
-clippy binary not found
! {}
",
713 cargo_clippy_path.display()
716 let clippy_ver = std::process::Command::new(CARGO_CLIPPY_PATH)
719 .map(|o| String::from_utf8_lossy(&o.stdout).into_owned())
720 .expect("could not get clippy version
!");
722 // download and extract the crates, then run clippy on them and collect clippys warnings
723 // flatten into one big list of warnings
725 let crates = read_crates(&config.sources_toml_path);
726 let old_stats = read_stats_from_file(&config.lintcheck_results_path);
728 let counter = AtomicUsize::new(1);
729 let lint_filter: Vec<String> = config
733 let mut filter = filter.clone();
734 filter.insert_str(0, "--force
-warn
=");
739 let clippy_warnings: Vec<ClippyWarning> = if let Some(only_one_crate) = clap_config.value_of("only
") {
740 // if we don't have the specified crate in the .toml, throw an error
741 if !crates.iter().any(|krate| {
742 let name = match krate {
743 CrateSource::CratesIo { name, .. } | CrateSource::Git { name, .. } | CrateSource::Path { name, .. } => {
747 name == only_one_crate
750 "ERROR
: could not find
crate '{}'
in lintcheck
/lintcheck_crates
.toml
",
753 std::process::exit(1);
756 // only check a single crate that was passed via cmdline
759 .map(|krate| krate.download_and_extract())
760 .filter(|krate| krate.name == only_one_crate)
762 krate.run_clippy_lints(&cargo_clippy_path, &AtomicUsize::new(0), 1, 1, config.fix, &lint_filter)
766 if config.max_jobs > 1 {
767 // run parallel with rayon
769 // Ask rayon for thread count. Assume that half of that is the number of physical cores
770 // Use one target dir for each core so that we can run N clippys in parallel.
771 // We need to use different target dirs because cargo would lock them for a single build otherwise,
772 // killing the parallelism. However this also means that deps will only be reused half/a
773 // quarter of the time which might result in a longer wall clock runtime
775 // This helps when we check many small crates with dep-trees that don't have a lot of branches in
776 // order to achieve some kind of parallelism
778 // by default, use a single thread
779 let num_cpus = config.max_jobs;
780 let num_crates = crates.len();
782 // check all crates (default)
785 .map(|krate| krate.download_and_extract())
787 krate.run_clippy_lints(
799 let num_crates = crates.len();
802 .map(|krate| krate.download_and_extract())
804 krate.run_clippy_lints(&cargo_clippy_path, &counter, 1, num_crates, config.fix, &lint_filter)
810 // if we are in --fix mode, don't change the log files, terminate here
815 // generate some stats
816 let (stats_formatted, new_stats) = gather_stats(&clippy_warnings);
818 // grab crashes/ICEs, save the crate name and the ice message
819 let ices: Vec<(&String, &String)> = clippy_warnings
821 .filter(|warning| warning.is_ice)
822 .map(|w| (&w.crate_name, &w.message))
825 let mut all_msgs: Vec<String> = clippy_warnings
827 .map(|warn| warn.to_output(config.markdown))
830 all_msgs.push("\n\n### Stats:\n\n".into());
831 all_msgs.push(stats_formatted);
833 // save the text into lintcheck-logs/logs.txt
834 let mut text = clippy_ver; // clippy version number on top
835 text.push_str("\n### Reports\n\n");
837 text.push_str("| file | lint | message |\n");
838 text.push_str("| --- | --- | --- |\n");
840 write!(text, "{}", all_msgs.join(""));
841 text.push_str("\n\n### ICEs:\n");
842 for (cratename, msg) in ices.iter() {
843 let _ = write!(text, "{}: '{}'", cratename, msg);
846 println!("Writing logs to {}", config.lintcheck_results_path.display());
847 std::fs::create_dir_all(config.lintcheck_results_path.parent().unwrap()).unwrap();
848 write(&config.lintcheck_results_path, text).unwrap();
850 print_stats(old_stats, new_stats, &config.lint_filter);
853 /// read the previous stats from the lintcheck-log file
854 fn read_stats_from_file(file_path: &Path) -> HashMap<String, usize> {
855 let file_content: String = match std::fs::read_to_string(file_path).ok() {
856 Some(content) => content,
858 return HashMap::new();
862 let lines: Vec<String> = file_content.lines().map(ToString::to_string).collect();
866 .skip_while(|line| line.as_str() != "### Stats:")
867 // Skipping the table header and the `Stats:` label
869 .take_while(|line| line.starts_with("| "))
871 let mut spl = line.split('|');
872 // Skip the first `|` symbol
874 if let (Some(lint), Some(count)) = (spl.next(), spl.next()) {
875 Some((lint.trim().to_string(), count.trim().parse::<usize>().unwrap()))
880 .collect::<HashMap<String, usize>>()
883 /// print how lint counts changed between runs
884 fn print_stats(old_stats: HashMap<String, usize>, new_stats: HashMap<&String, usize>, lint_filter: &Vec<String>) {
885 let same_in_both_hashmaps = old_stats
887 .filter(|(old_key, old_val)| new_stats.get::<&String>(&old_key) == Some(old_val))
888 .map(|(k, v)| (k.to_string(), *v))
889 .collect::<Vec<(String, usize)>>();
891 let mut old_stats_deduped = old_stats;
892 let mut new_stats_deduped = new_stats;
894 // remove duplicates from both hashmaps
895 same_in_both_hashmaps.iter().for_each(|(k, v)| {
896 assert!(old_stats_deduped.remove(k) == Some(*v));
897 assert!(new_stats_deduped.remove(k) == Some(*v));
900 println!("\nStats:");
902 // list all new counts (key is in new stats but not in old stats)
905 .filter(|(new_key, _)| old_stats_deduped.get::<str>(&new_key).is_none())
906 .for_each(|(new_key, new_value)| {
907 println!("{} 0 => {}", new_key, new_value);
910 // list all changed counts (key is in both maps but value differs)
913 .filter(|(new_key, _new_val)| old_stats_deduped.get::<str>(&new_key).is_some())
914 .for_each(|(new_key, new_val)| {
915 let old_val = old_stats_deduped.get::<str>(&new_key).unwrap();
916 println!("{} {} => {}", new_key, old_val, new_val);
919 // list all gone counts (key is in old status but not in new stats)
922 .filter(|(old_key, _)| new_stats_deduped.get::<&String>(&old_key).is_none())
923 .filter(|(old_key, _)| lint_filter.is_empty() || lint_filter.contains(old_key))
924 .for_each(|(old_key, old_value)| {
925 println!("{} {} => 0", old_key, old_value);
929 /// Create necessary directories to run the lintcheck tool.
933 /// This function panics if creating one of the dirs fails.
934 fn create_dirs(krate_download_dir: &Path, extract_dir: &Path) {
935 std::fs::create_dir("target/lintcheck/").unwrap_or_else(|err| {
936 if err.kind() != ErrorKind::AlreadyExists {
937 panic!("cannot create lintcheck target dir");
940 std::fs::create_dir(&krate_download_dir).unwrap_or_else(|err| {
941 if err.kind() != ErrorKind::AlreadyExists {
942 panic!("cannot create crate download dir");
945 std::fs::create_dir(&extract_dir).unwrap_or_else(|err| {
946 if err.kind() != ErrorKind::AlreadyExists {
947 panic!("cannot create crate extraction dir");
952 fn get_clap_config<'a>() -> ArgMatches<'a> {
953 App::new("lintcheck")
954 .about("run clippy on a set of crates and check output")
956 Arg::with_name("only")
960 .help("only process a single crate of the list"),
963 Arg::with_name("crates-toml")
965 .value_name("CRATES-SOURCES-TOML-PATH")
967 .help("set the path for a crates.toml where lintcheck should read the sources from"),
970 Arg::with_name("threads")
975 .help("number of threads to use, 0 automatic choice"),
978 Arg::with_name("fix")
980 .help("runs cargo clippy --fix and checks if all suggestions apply"),
983 Arg::with_name("filter")
987 .value_name("clippy_lint_name")
988 .help("apply a filter to only collect specified lints, this also overrides `allow` attributes"),
991 Arg::with_name("markdown")
993 .help("change the reports table to use markdown links"),
998 /// Returns the path to the Clippy project directory
1002 /// Panics if the current directory could not be retrieved, there was an error reading any of the
1003 /// Cargo.toml files or ancestor directory is the clippy root directory
1005 pub fn clippy_project_root() -> PathBuf {
1006 let current_dir = std::env::current_dir().unwrap();
1007 for path in current_dir.ancestors() {
1008 let result = std::fs::read_to_string(path.join("Cargo.toml"));
1009 if let Err(err) = &result {
1010 if err.kind() == std::io::ErrorKind::NotFound {
1015 let content = result.unwrap();
1016 if content.contains("[package]\nname = \"clippy\"") {
1017 return path.to_path_buf();
1020 panic!("error: Can't determine root of project. Please run inside a Clippy working dir.");
1024 fn lintcheck_test() {
1030 "./lintcheck/Cargo.toml",
1033 "lintcheck/test_sources.toml",
1035 let status = std::process::Command::new("cargo")
1037 .current_dir("..") // repo root
1041 assert!(status.unwrap().success());