ZIm/tooling/xtask/src/main.rs
Marshall Bowers 22fe03913c
Move Clippy configuration to the workspace level (#8891)
This PR moves the Clippy configuration up to the workspace level.

We're using the [`lints`
table](https://doc.rust-lang.org/cargo/reference/workspaces.html#the-lints-table)
to configure the Clippy ruleset in the workspace's `Cargo.toml`.

Each crate in the workspace now has the following in their own
`Cargo.toml` to inherit the lints from the workspace:

```toml
[lints]
workspace = true
```

This allows for configuring rust-analyzer to show Clippy lints in the
editor by using the following configuration in your Zed `settings.json`:

```json
{
  "lsp": {
    "rust-analyzer": {
      "initialization_options": {
        "check": {
          "command": "clippy"
        }
      }
    }
  }
```

Release Notes:

- N/A
2024-03-05 12:01:17 -05:00

86 lines
1.9 KiB
Rust

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 {
/// Automatically apply lint suggestions (`clippy --fix`).
#[arg(long)]
fix: bool,
/// The package to run Clippy against (`cargo -p <PACKAGE> clippy`).
#[arg(long, short)]
package: Option<String>,
}
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");
if let Some(package) = args.package.as_ref() {
clippy_command.args(["--package", package]);
} else {
clippy_command.arg("--workspace");
}
clippy_command
.arg("--release")
.arg("--all-targets")
.arg("--all-features");
if args.fix {
clippy_command.arg("--fix");
}
clippy_command.arg("--");
// Deny all warnings.
// We don't do this yet on Windows, as it still has some warnings present.
#[cfg(not(target_os = "windows"))]
clippy_command.args(["--deny", "warnings"]);
eprintln!(
"running: {cargo} {}",
clippy_command
.get_args()
.map(|arg| 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(())
}