From af594bd628ed2ca6593aa1894cd1ac69b6a68992 Mon Sep 17 00:00:00 2001 From: Kaur Kuut Date: Sun, 1 Feb 2026 22:09:19 +0200 Subject: [PATCH] Add `--crates ` option to `clippy`. --- prep/CHANGELOG.md | 9 +++++++++ prep/src/cmd/ci.rs | 15 ++++++++++++--- prep/src/cmd/clippy.rs | 10 ++++++---- prep/src/cmd/mod.rs | 27 +++++++++++++++++++++++++++ prep/src/main.rs | 6 +++++- prep/src/ui/help.rs | 4 ++++ 6 files changed, 63 insertions(+), 8 deletions(-) diff --git a/prep/CHANGELOG.md b/prep/CHANGELOG.md index 17caaf1..65e5a2c 100644 --- a/prep/CHANGELOG.md +++ b/prep/CHANGELOG.md @@ -2,6 +2,14 @@ ## [Unreleased] +### Added + +* `--crates ` option to the `clippy` command. ([#18] by [@xStrom]) + +### Changed + +* `ci` `clippy` now uses `--crates all` by default and does two separate checks with `--crates main` and `--crates aux` in extended mode. ([#18] by [@xStrom]) + ## [0.1.0] - 2026-01-31 ### Added @@ -15,6 +23,7 @@ [#2]: https://github.com/Nevermore/prep/pull/2 [#3]: https://github.com/Nevermore/prep/pull/3 [#5]: https://github.com/Nevermore/prep/pull/5 +[#18]: https://github.com/Nevermore/prep/pull/18 [Unreleased]: https://github.com/Nevermore/prep/compare/v0.1.0...HEAD [0.1.0]: https://github.com/Nevermore/prep/compare/v0.0.0...v0.1.0 diff --git a/prep/src/cmd/ci.rs b/prep/src/cmd/ci.rs index dac76d8..77bdaa6 100644 --- a/prep/src/cmd/ci.rs +++ b/prep/src/cmd/ci.rs @@ -1,14 +1,14 @@ // Copyright 2026 the Prep Authors // SPDX-License-Identifier: Apache-2.0 OR MIT -use crate::cmd::{clippy, format}; +use crate::cmd::{CargoTargets, clippy, format}; /// Runs CI verification. /// /// Can be ran in `extended` mode for more thorough checks. /// /// Set `fail_fast` to `false` to run the checks to the end regardless of failure. -pub fn run(_extended: bool, fail_fast: bool) -> anyhow::Result<()> { +pub fn run(extended: bool, fail_fast: bool) -> anyhow::Result<()> { let mut errs: Vec = Vec::new(); let mut step = |f: fn() -> anyhow::Result<()>| -> anyhow::Result<()> { if let Err(e) = f() { @@ -21,7 +21,16 @@ pub fn run(_extended: bool, fail_fast: bool) -> anyhow::Result<()> { }; step(|| format::run(true))?; - step(|| clippy::run(true))?; + + if extended { + // We need to avoid --all-targets because it will unify dev and regular dep features. + step(|| clippy::run(CargoTargets::Main, true))?; + step(|| clippy::run(CargoTargets::Auxiliary, true))?; + } else { + // Slightly faster due to shared build cache, + // but will miss unified feature bugs. + step(|| clippy::run(CargoTargets::All, true))?; + } if errs.is_empty() { Ok(()) diff --git a/prep/src/cmd/clippy.rs b/prep/src/cmd/clippy.rs index 051d6c1..e6901f4 100644 --- a/prep/src/cmd/clippy.rs +++ b/prep/src/cmd/clippy.rs @@ -5,18 +5,20 @@ use std::process::Command; use anyhow::{Context, ensure}; +use crate::cmd::CargoTargets; use crate::ui; -/// Runs Clippy analysis. +/// Runs Clippy analysis on the given `targets`. /// /// In `strict` mode warnings are treated as errors. -pub fn run(strict: bool) -> anyhow::Result<()> { +pub fn run(targets: CargoTargets, strict: bool) -> anyhow::Result<()> { let mut cmd = Command::new("cargo"); let mut cmd = cmd .arg("clippy") + .arg("--locked") .arg("--workspace") - .arg("--all-features") - .arg("--locked"); + .args(targets.as_args()) + .arg("--all-features"); if strict { cmd = cmd.args(["--", "-D", "warnings"]); } diff --git a/prep/src/cmd/mod.rs b/prep/src/cmd/mod.rs index 2d1fb20..732aa17 100644 --- a/prep/src/cmd/mod.rs +++ b/prep/src/cmd/mod.rs @@ -1,6 +1,33 @@ // Copyright 2026 the Prep Authors // SPDX-License-Identifier: Apache-2.0 OR MIT +use clap::ValueEnum; + pub mod ci; pub mod clippy; pub mod format; + +/// Cargo targets. +#[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum)] +pub enum CargoTargets { + /// All targets, i.e. `--lib --bins --examples --tests --benches`. + All, + /// Main targets are `--lib` and `--bins`. + Main, + /// Auxiliary targets are `--examples`, `--tests`, and `--benches`. + #[value(name = "aux")] + Auxiliary, +} + +impl CargoTargets { + /// Returns the Cargo flag arguments corresponding to `self`. + pub fn as_args(&self) -> Vec<&str> { + match self { + Self::All => vec!["--all-targets"], + // --lib --bins would produce a Cargo error if there are no binary targets, + // but luckily providing no targets gives the same behavior without the error. + Self::Main => vec![], + Self::Auxiliary => vec!["--examples", "--tests", "--benches"], + } + } +} diff --git a/prep/src/main.rs b/prep/src/main.rs index 124cf82..fd75aff 100644 --- a/prep/src/main.rs +++ b/prep/src/main.rs @@ -10,6 +10,8 @@ use clap::{CommandFactory, FromArgMatches, Parser, Subcommand}; use ui::help; +use crate::cmd::CargoTargets; + #[derive(Parser)] #[command(version, about, long_about = None)] struct Cli { @@ -28,6 +30,8 @@ enum Commands { }, #[command(alias = "clp")] Clippy { + #[arg(name = "crates", short, long, value_enum, default_value_t = CargoTargets::Main)] + targets: CargoTargets, #[arg(short, long)] strict: bool, }, @@ -53,7 +57,7 @@ fn main() -> anyhow::Result<()> { extended, no_fail_fast, } => cmd::ci::run(extended, !no_fail_fast), - Commands::Clippy { strict } => cmd::clippy::run(strict), + Commands::Clippy { targets, strict } => cmd::clippy::run(targets, strict), Commands::Format { check } => cmd::format::run(check), } } diff --git a/prep/src/ui/help.rs b/prep/src/ui/help.rs index e2bf858..dd08dc3 100644 --- a/prep/src/ui/help.rs +++ b/prep/src/ui/help.rs @@ -102,6 +102,10 @@ Analyze the Rust workspace with Clippy. ···· ······ {cb}prep clippy{cb:#} {bb}[options]{bb:#} {gb}Options:{gb:#} + {cb}-c --crates {cb:#}Target specified crates. Possible values: + ···· ······{bb}main{bb:#} -> Binaries and the main library. (default) + ···· ······{bb}aux{bb:#} -> Examples, tests, and benches. + ···· ······{bb}all{bb:#} -> All of the above. {cb}-s --strict {cb:#}Treat warnings as errors. {cb}-h --help {cb:#}Print this help message. "