]> git.lizzy.rs Git - rust.git/blob - clippy_dev/src/lint.rs
rustc: Parameterize `ty::Visibility` over used ID
[rust.git] / clippy_dev / src / lint.rs
1 use crate::cargo_clippy_path;
2 use std::process::{self, Command, ExitStatus};
3 use std::{fs, io};
4
5 fn exit_if_err(status: io::Result<ExitStatus>) {
6     match status.expect("failed to run command").code() {
7         Some(0) => {},
8         Some(n) => process::exit(n),
9         None => {
10             eprintln!("Killed by signal");
11             process::exit(1);
12         },
13     }
14 }
15
16 pub fn run<'a>(path: &str, args: impl Iterator<Item = &'a String>) {
17     let is_file = match fs::metadata(path) {
18         Ok(metadata) => metadata.is_file(),
19         Err(e) => {
20             eprintln!("Failed to read {path}: {e:?}");
21             process::exit(1);
22         },
23     };
24
25     if is_file {
26         exit_if_err(
27             Command::new("cargo")
28                 .args(["run", "--bin", "clippy-driver", "--"])
29                 .args(["-L", "./target/debug"])
30                 .args(["-Z", "no-codegen"])
31                 .args(["--edition", "2021"])
32                 .arg(path)
33                 .args(args)
34                 .status(),
35         );
36     } else {
37         exit_if_err(Command::new("cargo").arg("build").status());
38
39         // Run in a tempdir as changes to clippy do not retrigger linting
40         let target = tempfile::Builder::new()
41             .prefix("clippy")
42             .tempdir()
43             .expect("failed to create tempdir");
44
45         let status = Command::new(cargo_clippy_path())
46             .arg("clippy")
47             .args(args)
48             .current_dir(path)
49             .env("CARGO_TARGET_DIR", target.as_ref())
50             .status();
51
52         target.close().expect("failed to remove tempdir");
53         exit_if_err(status);
54     }
55 }