xtask/
clippy.rs

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use std::process::{Command, Stdio};

use cargo_metadata::{Metadata, MetadataCommand};

use crate::Backend;

#[derive(Debug, clap::Args)]
pub struct ClippyArgs {
    /// Run clippy for a specific backend
    #[clap(default_value_t = Backend::All)]
    pub backend: Backend,
    /// do not abort running if we encounter an error
    /// while running clippy for all backends
    #[clap(long = "keep-going")]
    pub keep_going: bool,
    /// additional flags passed to cargo clippy
    ///
    /// This is useful for passing custom arguments like `cargo clippy --fix`
    pub flags: Vec<String>,
}

impl ClippyArgs {
    pub(crate) fn run(&self) {
        let metadata = MetadataCommand::default().exec().unwrap();
        let failed = if matches!(self.backend, Backend::All) {
            let mut failed = false;
            for backend in Backend::ALL {
                if !self.run_for_backend(*backend, &metadata) {
                    failed = true;
                    if !self.keep_going {
                        break;
                    }
                }
            }
            failed
        } else {
            !self.run_for_backend(self.backend, &metadata)
        };
        if failed {
            std::process::exit(1);
        }
    }

    fn run_for_backend(&self, backend: Backend, metadata: &Metadata) -> bool {
        let exclude = crate::utils::get_exclude_for_backend(&backend.to_string(), metadata);
        let flags = [
            "-F".into(),
            format!("diesel/{backend}"),
            "-F".into(),
            format!("diesel_derives/{backend}"),
            "-F".into(),
            format!("diesel_cli/{backend}"),
            "-F".into(),
            format!("diesel_tests/{backend}"),
            "-F".into(),
            format!("diesel-dynamic-schema/{backend}"),
        ];
        let mut command = Command::new("cargo");

        command
            .args([
                "clippy",
                "--workspace",
                "--no-default-features",
                "--all-targets",
            ])
            .args(exclude)
            .args(flags)
            .args(["-F", "diesel/extras", "-F", "diesel/with-deprecated"])
            .args(&self.flags)
            .current_dir(&metadata.workspace_root);

        println!("Run clippy via `{command:?}`");
        command
            .stderr(Stdio::inherit())
            .stdout(Stdio::inherit())
            .status()
            .unwrap()
            .success()
    }
}