1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use std::{any::Any, process::Command};

use crate::{
    args::{EnvArgs, FeatureArgs},
    config::Config,
    process::CommandExt,
    Result, Run,
};

/// Arguments definition of the `clippy` subcommand.
#[cfg_attr(doc, doc = include_str!("../../doc/cargo-xtask-clippy.md"))]
#[derive(Debug, Clone, Default, clap::Args)]
#[non_exhaustive]
pub struct Clippy {
    /// Environment variables to set for `cargo clippy`.
    #[clap(flatten)]
    pub env_args: EnvArgs,
    /// Features to run the `cargo clippy` with
    #[clap(flatten)]
    pub feature_args: FeatureArgs,
    /// Options to pass to the `cargo clippy`
    pub extra_options: Vec<String>,
}

impl Run for Clippy {
    fn run(&self, config: &Config) -> Result<()> {
        self.run(config)
    }

    fn into_any(self: Box<Self>) -> Box<dyn Any> {
        self
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

impl Clippy {
    /// Runs the `clippy` subcommand.
    #[tracing::instrument(name = "clippy", skip_all, err)]
    pub fn run(&self, _config: &Config) -> Result<()> {
        let Self {
            env_args,
            feature_args,
            extra_options,
        } = self;

        for res in feature_args.features() {
            let (workspace, package, features) = res?;
            Command::new("cargo")
                .args(["clippy", "--package", &package.name])
                .args(features.map(|f| f.to_args()).unwrap_or_default())
                .args(extra_options)
                .envs(env_args.env.clone())
                .workspace_spawn(workspace)?;
        }

        Ok(())
    }
}