|
1 | | -use std::process::{self, Command}; |
| 1 | +use crate::cargo_clippy_path; |
| 2 | +use std::process::{self, Command, ExitStatus}; |
| 3 | +use std::{fs, io}; |
2 | 4 |
|
3 | | -pub fn run(filename: &str) { |
4 | | - let code = Command::new("cargo") |
5 | | - .args(["run", "--bin", "clippy-driver", "--"]) |
6 | | - .args(["-L", "./target/debug"]) |
7 | | - .args(["-Z", "no-codegen"]) |
8 | | - .args(["--edition", "2021"]) |
9 | | - .arg(filename) |
10 | | - .status() |
11 | | - .expect("failed to run cargo") |
12 | | - .code(); |
13 | | - |
14 | | - if code.is_none() { |
15 | | - eprintln!("Killed by signal"); |
| 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 | + }, |
16 | 13 | } |
| 14 | +} |
| 15 | + |
| 16 | +pub fn run(path: &str) { |
| 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 | + .status(), |
| 34 | + ); |
| 35 | + } else { |
| 36 | + exit_if_err(Command::new("cargo").arg("build").status()); |
17 | 37 |
|
18 | | - process::exit(code.unwrap_or(1)); |
| 38 | + // Run in a tempdir as changes to clippy do not retrigger linting |
| 39 | + let target = tempfile::Builder::new() |
| 40 | + .prefix("clippy") |
| 41 | + .tempdir() |
| 42 | + .expect("failed to create tempdir"); |
| 43 | + |
| 44 | + let status = Command::new(cargo_clippy_path()) |
| 45 | + .current_dir(path) |
| 46 | + .env("CARGO_TARGET_DIR", target.as_ref()) |
| 47 | + .status(); |
| 48 | + |
| 49 | + target.close().expect("failed to remove tempdir"); |
| 50 | + exit_if_err(status); |
| 51 | + } |
19 | 52 | } |
0 commit comments