]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/lintcheck/src/main.rs
816efbdaedf36d770aab843378175781f4274fb6
[rustc.git] / src / tools / clippy / lintcheck / src / main.rs
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
3 // testsuite).
4 //
5 // When a new lint is introduced, we can search the results for new warnings and check for false
6 // positives.
7
8 #![allow(clippy::collapsible_else_if)]
9
10 use std::ffi::OsStr;
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};
15 use std::{
16 env,
17 fs::write,
18 path::{Path, PathBuf},
19 thread,
20 time::Duration,
21 };
22
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};
28
29 #[cfg(not(windows))]
30 const CLIPPY_DRIVER_PATH: &str = "target/debug/clippy-driver";
31 #[cfg(not(windows))]
32 const CARGO_CLIPPY_PATH: &str = "target/debug/cargo-clippy";
33
34 #[cfg(windows)]
35 const CLIPPY_DRIVER_PATH: &str = "target/debug/clippy-driver.exe";
36 #[cfg(windows)]
37 const CARGO_CLIPPY_PATH: &str = "target/debug/cargo-clippy.exe";
38
39 const LINTCHECK_DOWNLOADS: &str = "target/lintcheck/downloads";
40 const LINTCHECK_SOURCES: &str = "target/lintcheck/sources";
41
42 /// List of sources to check, loaded from a .toml file
43 #[derive(Debug, Serialize, Deserialize)]
44 struct SourceList {
45 crates: HashMap<String, TomlCrate>,
46 }
47
48 /// A crate source stored inside the .toml
49 /// will be translated into on one of the `CrateSource` variants
50 #[derive(Debug, Serialize, Deserialize)]
51 struct TomlCrate {
52 name: String,
53 versions: Option<Vec<String>>,
54 git_url: Option<String>,
55 git_hash: Option<String>,
56 path: Option<String>,
57 options: Option<Vec<String>>,
58 }
59
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)]
63 enum CrateSource {
64 CratesIo {
65 name: String,
66 version: String,
67 options: Option<Vec<String>>,
68 },
69 Git {
70 name: String,
71 url: String,
72 commit: String,
73 options: Option<Vec<String>>,
74 },
75 Path {
76 name: String,
77 path: PathBuf,
78 options: Option<Vec<String>>,
79 },
80 }
81
82 /// Represents the actual source code of a crate that we ran "cargo clippy" on
83 #[derive(Debug)]
84 struct Crate {
85 version: String,
86 name: String,
87 // path to the extracted sources that clippy can check
88 path: PathBuf,
89 options: Option<Vec<String>>,
90 }
91
92 /// A single warning that clippy issued while checking a `Crate`
93 #[derive(Debug)]
94 struct ClippyWarning {
95 crate_name: String,
96 crate_version: String,
97 file: String,
98 line: String,
99 column: String,
100 linttype: String,
101 message: String,
102 is_ice: bool,
103 }
104
105 #[allow(unused)]
106 impl ClippyWarning {
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);
110 if markdown {
111 let lint = format!("`{}`", self.linttype);
112
113 let mut output = String::from("| ");
114 let _ = write!(
115 output,
116 "[`{}`](../target/lintcheck/sources/{}#L{})",
117 file_with_pos, file, self.line
118 );
119 let _ = write!(output, r#" | {:<50} | "{}" |"#, lint, self.message);
120 output.push('\n');
121 output
122 } else {
123 format!(
124 "target/lintcheck/sources/{} {} \"{}\"\n",
125 file_with_pos, self.linttype, self.message
126 )
127 }
128 }
129 }
130
131 fn get(path: &str) -> Result<ureq::Response, ureq::Error> {
132 const MAX_RETRIES: u8 = 4;
133 let mut retries = 0;
134 loop {
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),
140 }
141 eprintln!("retrying in {} seconds...", retries);
142 thread::sleep(Duration::from_secs(retries as u64));
143 retries += 1;
144 }
145 }
146
147 impl CrateSource {
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 {
152 match self {
153 CrateSource::CratesIo { name, version, options } => {
154 let extract_dir = PathBuf::from(LINTCHECK_SOURCES);
155 let krate_download_dir = PathBuf::from(LINTCHECK_DOWNLOADS);
156
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);
161
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();
170
171 // unzip the tarball
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!");
176 }
177 // crate is extracted, return a new Krate object which contains the path to the extracted
178 // sources that clippy can check
179 Crate {
180 version: version.clone(),
181 name: name.clone(),
182 path: extract_dir.join(format!("{}-{}/", name, version)),
183 options: options.clone(),
184 }
185 },
186 CrateSource::Git {
187 name,
188 url,
189 commit,
190 options,
191 } => {
192 let repo_path = {
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));
196 repo_path
197 };
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")
202 .arg("clone")
203 .arg(url)
204 .arg(&repo_path)
205 .status()
206 .expect("Failed to clone git repo!")
207 .success()
208 {
209 eprintln!("Failed to clone {} into {}", url, repo_path.display())
210 }
211 }
212 // check out the commit/branch/whatever
213 if !Command::new("git")
214 .arg("checkout")
215 .arg(commit)
216 .current_dir(&repo_path)
217 .status()
218 .expect("Failed to check out commit")
219 .success()
220 {
221 eprintln!("Failed to checkout {} of repo at {}", commit, repo_path.display())
222 }
223
224 Crate {
225 version: commit.clone(),
226 name: name.clone(),
227 path: repo_path,
228 options: options.clone(),
229 }
230 },
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();
239 }
240
241 println!("Copying {:?} to {:?}", path, dest_crate_root);
242
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"))
246 .unwrap_or(false)
247 }
248
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();
255
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();
260 }
261 }
262
263 Crate {
264 version: String::from("local"),
265 name: name.clone(),
266 path: dest_crate_root,
267 options: options.clone(),
268 }
269 },
270 }
271 }
272 }
273
274 impl Crate {
275 /// Run `cargo clippy` on the `Crate` and collect and return all the lint warnings that clippy
276 /// issued
277 fn run_clippy_lints(
278 &self,
279 cargo_clippy_path: &Path,
280 target_dir_index: &AtomicUsize,
281 thread_limit: usize,
282 total_crates_to_lint: usize,
283 fix: bool,
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;
291
292 if thread_limit == 1 {
293 println!(
294 "{}/{} {}% Linting {} {}",
295 index, total_crates_to_lint, perc, &self.name, &self.version
296 );
297 } else {
298 println!(
299 "{}/{} {}% Linting {} {} in target dir {:?}",
300 index, total_crates_to_lint, perc, &self.name, &self.version, thread_index
301 );
302 }
303
304 let cargo_clippy_path = std::fs::canonicalize(cargo_clippy_path).unwrap();
305
306 let shared_target_dir = clippy_project_root().join("target/lintcheck/shared_target_dir");
307
308 let mut args = if fix {
309 vec!["--fix", "--"]
310 } else {
311 vec!["--", "--message-format=json", "--"]
312 };
313
314 if let Some(options) = &self.options {
315 for opt in options {
316 args.push(opt);
317 }
318 } else {
319 args.extend(&["-Wclippy::pedantic", "-Wclippy::cargo"])
320 }
321
322 if lint_filter.is_empty() {
323 args.push("--cap-lints=warn");
324 } else {
325 args.push("--cap-lints=allow");
326 args.extend(lint_filter.iter().map(|filter| filter.as_str()))
327 }
328
329 let all_output = std::process::Command::new(&cargo_clippy_path)
330 // use the looping index to create individual target dirs
331 .env(
332 "CARGO_TARGET_DIR",
333 shared_target_dir.join(format!("_{:?}", thread_index)),
334 )
335 // lint warnings will look like this:
336 // src/cargo/ops/cargo_compile.rs:127:35: warning: usage of `FromIterator::from_iter`
337 .args(&args)
338 .current_dir(&self.path)
339 .output()
340 .unwrap_or_else(|error| {
341 panic!(
342 "Encountered error:\n{:?}\ncargo_clippy_path: {}\ncrate path:{}\n",
343 error,
344 &cargo_clippy_path.display(),
345 &self.path.display()
346 );
347 });
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;
351
352 if !status.success() {
353 eprintln!(
354 "\nWARNING: bad exit status after checking {} {} \n",
355 self.name, self.version
356 );
357 }
358
359 if fix {
360 if let Some(stderr) = stderr
361 .lines()
362 .find(|line| line.contains("failed to automatically apply fixes suggested by rustc to crate"))
363 {
364 let subcrate = &stderr[63..];
365 println!(
366 "ERROR: failed to apply some suggetion to {} / to (sub)crate {}",
367 self.name, subcrate
368 );
369 }
370 // fast path, we don't need the warnings anyway
371 return Vec::new();
372 }
373
374 let output_lines = stdout.lines();
375 let warnings: Vec<ClippyWarning> = output_lines
376 .into_iter()
377 // get all clippy warnings and ICEs
378 .filter(|line| filter_clippy_warnings(&line))
379 .map(|json_msg| parse_json_message(json_msg, &self))
380 .collect();
381
382 warnings
383 }
384 }
385
386 #[derive(Debug)]
387 struct LintcheckConfig {
388 /// max number of jobs to spawn (default 1)
389 max_jobs: usize,
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
395 fix: bool,
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
399 markdown: bool,
400 }
401
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(|_| {
408 clap_config
409 .value_of("crates-toml")
410 .clone()
411 .unwrap_or("lintcheck/lintcheck_crates.toml")
412 .to_string()
413 });
414
415 let markdown = clap_config.is_present("markdown");
416 let sources_toml_path = PathBuf::from(sources_toml);
417
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.{}",
423 filename.display(),
424 if markdown { "md" } else { "txt" }
425 ));
426
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") {
431 Some(threads) => {
432 let threads: usize = threads
433 .parse()
434 .unwrap_or_else(|_| panic!("Failed to parse '{}' to a digit", threads));
435 if threads == 0 {
436 // automatic choice
437 // Rayon seems to return thread count so half that for core count
438 (rayon::current_num_threads() / 2) as usize
439 } else {
440 threads
441 }
442 },
443 // no -j passed, use a single thread
444 None => 1,
445 };
446 let fix: bool = clap_config.is_present("fix");
447 let lint_filter: Vec<String> = clap_config
448 .values_of("filter")
449 .map(|iter| {
450 iter.map(|lint_name| {
451 let mut filter = lint_name.replace('_', "-");
452 if !filter.starts_with("clippy::") {
453 filter.insert_str(0, "clippy::");
454 }
455 filter
456 })
457 .collect()
458 })
459 .unwrap_or_default();
460
461 LintcheckConfig {
462 max_jobs,
463 sources_toml_path,
464 lintcheck_results_path,
465 fix,
466 lint_filter,
467 markdown,
468 }
469 }
470 }
471
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: ") {
478 return true;
479 }
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)
484
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")))
489 {
490 return true;
491 }
492 false
493 }
494
495 /// Builds clippy inside the repo to make sure we have a clippy executable we can use.
496 fn build_clippy() {
497 let status = Command::new("cargo")
498 .arg("build")
499 .status()
500 .expect("Failed to build clippy!");
501 if !status.success() {
502 eprintln!("Error: Failed to compile Clippy!");
503 std::process::exit(1);
504 }
505 }
506
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
515 .crates
516 .into_iter()
517 .map(|(_cratename, tomlcrate)| tomlcrate)
518 .collect();
519
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(),
529 });
530 }
531
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(),
539 });
540 })
541 }
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(),
549 });
550 }
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()
554 {
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!");
558 }
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");
561 }
562 unreachable!("Failed to translate TomlCrate into CrateSource!");
563 }
564 });
565 // sort the crates
566 crate_sources.sort();
567
568 crate_sources
569 }
570
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));
574
575 let file: String = jmsg["message"]["spans"][0]["file_name"]
576 .to_string()
577 .trim_matches('"')
578 .into();
579
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
582 // the registry.
583 // don't show the full path in that case.
584
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())
593 } else {
594 file
595 };
596
597 ClippyWarning {
598 crate_name: krate.name.to_string(),
599 crate_version: krate.version.to_string(),
600 file,
601 line: jmsg["message"]["spans"][0]["line_start"]
602 .to_string()
603 .trim_matches('"')
604 .into(),
605 column: jmsg["message"]["spans"][0]["text"][0]["highlight_start"]
606 .to_string()
607 .trim_matches('"')
608 .into(),
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: "),
612 }
613 }
614
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();
619 clippy_warnings
620 .iter()
621 .for_each(|wrn| *counter.entry(&wrn.linttype).or_insert(0) += 1);
622
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));
628
629 let mut header = String::from("| lint | count |\n");
630 header.push_str("| -------------------------------------------------- | ----- |\n");
631 let stats_string = stats
632 .iter()
633 .map(|(lint, count)| format!("| {:<50} | {:>4} |\n", lint, count))
634 .fold(header, |mut table, line| {
635 table.push_str(&line);
636 table
637 });
638
639 (stats_string, counter)
640 }
641
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() {
646 return true;
647 }
648
649 let clippy_modified: std::time::SystemTime = {
650 let mut times = [CLIPPY_DRIVER_PATH, CARGO_CLIPPY_PATH].iter().map(|p| {
651 std::fs::metadata(p)
652 .expect("failed to get metadata of file")
653 .modified()
654 .expect("failed to get modification date")
655 });
656 // the oldest modification of either of the binaries
657 std::cmp::max(times.next().unwrap(), times.next().unwrap())
658 };
659
660 let logs_modified: std::time::SystemTime = std::fs::metadata(lintcheck_logs_path)
661 .expect("failed to get metadata of file")
662 .modified()
663 .expect("failed to get modification date");
664
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
668 }
669
670 /// lintchecks `main()` function
671 ///
672 /// # Panics
673 ///
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)
676 pub fn main() {
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);
681 }
682
683 let clap_config = &get_clap_config();
684
685 let config = LintcheckConfig::from_clap(clap_config);
686
687 println!("Compiling clippy...");
688 build_clippy();
689 println!("Done compiling");
690
691 // if the clippy bin is newer than our logs, throw away target dirs to force clippy to
692 // refresh the logs
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");
701 }
702 }
703 }
704
705 let cargo_clippy_path: PathBuf = PathBuf::from(CARGO_CLIPPY_PATH)
706 .canonicalize()
707 .expect("failed to canonicalize path to clippy binary");
708
709 // assert that clippy is found
710 assert!(
711 cargo_clippy_path.is_file(),
712 "target/debug/cargo-clippy binary not found! {}",
713 cargo_clippy_path.display()
714 );
715
716 let clippy_ver = std::process::Command::new(CARGO_CLIPPY_PATH)
717 .arg("--version")
718 .output()
719 .map(|o| String::from_utf8_lossy(&o.stdout).into_owned())
720 .expect("could not get clippy version!");
721
722 // download and extract the crates, then run clippy on them and collect clippys warnings
723 // flatten into one big list of warnings
724
725 let crates = read_crates(&config.sources_toml_path);
726 let old_stats = read_stats_from_file(&config.lintcheck_results_path);
727
728 let counter = AtomicUsize::new(1);
729 let lint_filter: Vec<String> = config
730 .lint_filter
731 .iter()
732 .map(|filter| {
733 let mut filter = filter.clone();
734 filter.insert_str(0, "--force-warn=");
735 filter
736 })
737 .collect();
738
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, .. } => {
744 name
745 },
746 };
747 name == only_one_crate
748 }) {
749 eprintln!(
750 "ERROR: could not find crate '{}' in lintcheck/lintcheck_crates.toml",
751 only_one_crate
752 );
753 std::process::exit(1);
754 }
755
756 // only check a single crate that was passed via cmdline
757 crates
758 .into_iter()
759 .map(|krate| krate.download_and_extract())
760 .filter(|krate| krate.name == only_one_crate)
761 .flat_map(|krate| {
762 krate.run_clippy_lints(&cargo_clippy_path, &AtomicUsize::new(0), 1, 1, config.fix, &lint_filter)
763 })
764 .collect()
765 } else {
766 if config.max_jobs > 1 {
767 // run parallel with rayon
768
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
774
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
777
778 // by default, use a single thread
779 let num_cpus = config.max_jobs;
780 let num_crates = crates.len();
781
782 // check all crates (default)
783 crates
784 .into_par_iter()
785 .map(|krate| krate.download_and_extract())
786 .flat_map(|krate| {
787 krate.run_clippy_lints(
788 &cargo_clippy_path,
789 &counter,
790 num_cpus,
791 num_crates,
792 config.fix,
793 &lint_filter,
794 )
795 })
796 .collect()
797 } else {
798 // run sequential
799 let num_crates = crates.len();
800 crates
801 .into_iter()
802 .map(|krate| krate.download_and_extract())
803 .flat_map(|krate| {
804 krate.run_clippy_lints(&cargo_clippy_path, &counter, 1, num_crates, config.fix, &lint_filter)
805 })
806 .collect()
807 }
808 };
809
810 // if we are in --fix mode, don't change the log files, terminate here
811 if config.fix {
812 return;
813 }
814
815 // generate some stats
816 let (stats_formatted, new_stats) = gather_stats(&clippy_warnings);
817
818 // grab crashes/ICEs, save the crate name and the ice message
819 let ices: Vec<(&String, &String)> = clippy_warnings
820 .iter()
821 .filter(|warning| warning.is_ice)
822 .map(|w| (&w.crate_name, &w.message))
823 .collect();
824
825 let mut all_msgs: Vec<String> = clippy_warnings
826 .iter()
827 .map(|warn| warn.to_output(config.markdown))
828 .collect();
829 all_msgs.sort();
830 all_msgs.push("\n\n### Stats:\n\n".into());
831 all_msgs.push(stats_formatted);
832
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");
836 if config.markdown {
837 text.push_str("| file | lint | message |\n");
838 text.push_str("| --- | --- | --- |\n");
839 }
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);
844 }
845
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();
849
850 print_stats(old_stats, new_stats, &config.lint_filter);
851 }
852
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,
857 None => {
858 return HashMap::new();
859 },
860 };
861
862 let lines: Vec<String> = file_content.lines().map(ToString::to_string).collect();
863
864 lines
865 .iter()
866 .skip_while(|line| line.as_str() != "### Stats:")
867 // Skipping the table header and the `Stats:` label
868 .skip(4)
869 .take_while(|line| line.starts_with("| "))
870 .filter_map(|line| {
871 let mut spl = line.split('|');
872 // Skip the first `|` symbol
873 spl.next();
874 if let (Some(lint), Some(count)) = (spl.next(), spl.next()) {
875 Some((lint.trim().to_string(), count.trim().parse::<usize>().unwrap()))
876 } else {
877 None
878 }
879 })
880 .collect::<HashMap<String, usize>>()
881 }
882
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
886 .iter()
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)>>();
890
891 let mut old_stats_deduped = old_stats;
892 let mut new_stats_deduped = new_stats;
893
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));
898 });
899
900 println!("\nStats:");
901
902 // list all new counts (key is in new stats but not in old stats)
903 new_stats_deduped
904 .iter()
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);
908 });
909
910 // list all changed counts (key is in both maps but value differs)
911 new_stats_deduped
912 .iter()
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);
917 });
918
919 // list all gone counts (key is in old status but not in new stats)
920 old_stats_deduped
921 .iter()
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);
926 });
927 }
928
929 /// Create necessary directories to run the lintcheck tool.
930 ///
931 /// # Panics
932 ///
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");
938 }
939 });
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");
943 }
944 });
945 std::fs::create_dir(&extract_dir).unwrap_or_else(|err| {
946 if err.kind() != ErrorKind::AlreadyExists {
947 panic!("cannot create crate extraction dir");
948 }
949 });
950 }
951
952 fn get_clap_config<'a>() -> ArgMatches<'a> {
953 App::new("lintcheck")
954 .about("run clippy on a set of crates and check output")
955 .arg(
956 Arg::with_name("only")
957 .takes_value(true)
958 .value_name("CRATE")
959 .long("only")
960 .help("only process a single crate of the list"),
961 )
962 .arg(
963 Arg::with_name("crates-toml")
964 .takes_value(true)
965 .value_name("CRATES-SOURCES-TOML-PATH")
966 .long("crates-toml")
967 .help("set the path for a crates.toml where lintcheck should read the sources from"),
968 )
969 .arg(
970 Arg::with_name("threads")
971 .takes_value(true)
972 .value_name("N")
973 .short("j")
974 .long("jobs")
975 .help("number of threads to use, 0 automatic choice"),
976 )
977 .arg(
978 Arg::with_name("fix")
979 .long("--fix")
980 .help("runs cargo clippy --fix and checks if all suggestions apply"),
981 )
982 .arg(
983 Arg::with_name("filter")
984 .long("--filter")
985 .takes_value(true)
986 .multiple(true)
987 .value_name("clippy_lint_name")
988 .help("apply a filter to only collect specified lints, this also overrides `allow` attributes"),
989 )
990 .arg(
991 Arg::with_name("markdown")
992 .long("--markdown")
993 .help("change the reports table to use markdown links"),
994 )
995 .get_matches()
996 }
997
998 /// Returns the path to the Clippy project directory
999 ///
1000 /// # Panics
1001 ///
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
1004 #[must_use]
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 {
1011 continue;
1012 }
1013 }
1014
1015 let content = result.unwrap();
1016 if content.contains("[package]\nname = \"clippy\"") {
1017 return path.to_path_buf();
1018 }
1019 }
1020 panic!("error: Can't determine root of project. Please run inside a Clippy working dir.");
1021 }
1022
1023 #[test]
1024 fn lintcheck_test() {
1025 let args = [
1026 "run",
1027 "--target-dir",
1028 "lintcheck/target",
1029 "--manifest-path",
1030 "./lintcheck/Cargo.toml",
1031 "--",
1032 "--crates-toml",
1033 "lintcheck/test_sources.toml",
1034 ];
1035 let status = std::process::Command::new("cargo")
1036 .args(&args)
1037 .current_dir("..") // repo root
1038 .status();
1039 //.output();
1040
1041 assert!(status.unwrap().success());
1042 }