1use std::ops::{Deref, DerefMut};
2use std::path::PathBuf;
3use std::process::{self, Command};
4
5use anyhow::{Context, Result};
6use clap::Parser;
7
8use crate::Zig;
9
10#[derive(Clone, Debug, Default, Parser)]
11#[command(
12 display_order = 1,
13 after_help = "Run `cargo help clippy` for more detailed information."
14)]
15pub struct Clippy {
16 #[command(flatten)]
17 pub cargo: cargo_options::Clippy,
18
19 #[arg(skip)]
21 pub disable_zig_linker: bool,
22
23 #[arg(skip)]
25 pub enable_zig_ar: bool,
26}
27
28impl Clippy {
29 #[allow(clippy::field_reassign_with_default)]
31 pub fn new(manifest_path: Option<PathBuf>) -> Self {
32 let mut build = Self::default();
33 build.manifest_path = manifest_path;
34 build
35 }
36
37 pub fn execute(&self) -> Result<()> {
39 let mut run = self.build_command()?;
40
41 let mut child = run.spawn().context("Failed to run cargo clippy")?;
42 let status = child
43 .wait()
44 .expect("Failed to wait on cargo clippy process");
45 if !status.success() {
46 process::exit(status.code().unwrap_or(1));
47 }
48 Ok(())
49 }
50
51 pub fn build_command(&self) -> Result<Command> {
53 let mut build = self.cargo.command();
54 if !self.disable_zig_linker {
55 Zig::apply_command_env(
56 self.manifest_path.as_deref(),
57 self.release,
58 &self.cargo.common,
59 &mut build,
60 self.enable_zig_ar,
61 )?;
62 }
63
64 Ok(build)
65 }
66}
67
68impl Deref for Clippy {
69 type Target = cargo_options::Clippy;
70
71 fn deref(&self) -> &Self::Target {
72 &self.cargo
73 }
74}
75
76impl DerefMut for Clippy {
77 fn deref_mut(&mut self) -> &mut Self::Target {
78 &mut self.cargo
79 }
80}
81
82impl From<cargo_options::Clippy> for Clippy {
83 fn from(cargo: cargo_options::Clippy) -> Self {
84 Self {
85 cargo,
86 ..Default::default()
87 }
88 }
89}