Add cargo xtask clippy
(#8722)
This PR sets up a `cargo xtask clippy` command for running `cargo clippy` with our defined set of options. The intent is to make this easier to manage as we start enabling more Clippy rules. Release Notes: - N/A
This commit is contained in:
parent
c19587d4e4
commit
c9a509c805
11 changed files with 111 additions and 7 deletions
10
tooling/xtask/Cargo.toml
Normal file
10
tooling/xtask/Cargo.toml
Normal file
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "xtask"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
license = "GPL-3.0-or-later"
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
clap = { workspace = true, features = ["derive"] }
|
1
tooling/xtask/LICENSE-GPL
Symbolic link
1
tooling/xtask/LICENSE-GPL
Symbolic link
|
@ -0,0 +1 @@
|
|||
../../LICENSE-GPL
|
80
tooling/xtask/src/main.rs
Normal file
80
tooling/xtask/src/main.rs
Normal file
|
@ -0,0 +1,80 @@
|
|||
use std::process::Command;
|
||||
|
||||
use anyhow::{bail, Context, Result};
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "cargo xtask")]
|
||||
struct Args {
|
||||
#[command(subcommand)]
|
||||
command: CliCommand,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum CliCommand {
|
||||
/// Runs `cargo clippy`.
|
||||
Clippy(ClippyArgs),
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let args = Args::parse();
|
||||
|
||||
match args.command {
|
||||
CliCommand::Clippy(args) => run_clippy(args),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
struct ClippyArgs {
|
||||
/// Whether to deny warnings (`clippy --deny warnings`).
|
||||
#[arg(long)]
|
||||
deny_warnings: bool,
|
||||
}
|
||||
|
||||
fn run_clippy(args: ClippyArgs) -> Result<()> {
|
||||
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
|
||||
|
||||
let mut clippy_command = Command::new(&cargo);
|
||||
clippy_command
|
||||
.arg("clippy")
|
||||
.arg("--workspace")
|
||||
.arg("--release")
|
||||
.arg("--all-targets")
|
||||
.arg("--all-features");
|
||||
|
||||
clippy_command.arg("--");
|
||||
|
||||
if args.deny_warnings {
|
||||
clippy_command.args(["--deny", "warnings"]);
|
||||
}
|
||||
|
||||
// Allow all Clippy lints by default, as we have a lot of violations at the moment.
|
||||
// We can tighten things up once we have a better handle on them.
|
||||
clippy_command.args(["--allow", "clippy::all"]);
|
||||
|
||||
// Deny `dbg!` and `todo!`s.
|
||||
clippy_command
|
||||
.args(["--deny", "clippy::dbg_macro"])
|
||||
.args(["--deny", "clippy::todo"]);
|
||||
|
||||
eprintln!(
|
||||
"running: {cargo} {}",
|
||||
clippy_command
|
||||
.get_args()
|
||||
.map(|arg| format!("{}", arg.to_str().unwrap()))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ")
|
||||
);
|
||||
|
||||
let exit_status = clippy_command
|
||||
.spawn()
|
||||
.context("failed to spawn child process")?
|
||||
.wait()
|
||||
.context("failed to wait for child process")?;
|
||||
|
||||
if !exit_status.success() {
|
||||
bail!("clippy failed: {}", exit_status);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue