https://github.com/zed-industries/zed/issues/30972 brought up another case where our context is not enough to track the actual source of the issue: we get a general top-level error without inner error. The reason for this was `.ok_or_else(|| anyhow!("failed to read HEAD SHA"))?; ` on the top level. The PR finally reworks the way we use anyhow to reduce such issues (or at least make it simpler to bubble them up later in a fix). On top of that, uses a few more anyhow methods for better readability. * `.ok_or_else(|| anyhow!("..."))`, `map_err` and other similar error conversion/option reporting cases are replaced with `context` and `with_context` calls * in addition to that, various `anyhow!("failed to do ...")` are stripped with `.context("Doing ...")` messages instead to remove the parasitic `failed to` text * `anyhow::ensure!` is used instead of `if ... { return Err(...); }` calls * `anyhow::bail!` is used instead of `return Err(anyhow!(...));` Release Notes: - N/A
64 lines
1.5 KiB
Rust
64 lines
1.5 KiB
Rust
use std::process::Command;
|
|
|
|
use anyhow::{Context as _, Result, bail};
|
|
use clap::Parser;
|
|
|
|
#[derive(Parser)]
|
|
pub 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>,
|
|
}
|
|
|
|
pub 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.
|
|
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(())
|
|
}
|