Skip to content

Commit c9a509c

Browse files
authoredMar 2, 2024
Add cargo xtask clippy (zed-industries#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
1 parent c19587d commit c9a509c

File tree

11 files changed

+111
-7
lines changed

11 files changed

+111
-7
lines changed
 

‎.cargo/ci-config.toml

+3
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010
# in one spot, that's going to trigger a rebuild of all of the artifacts. Using ci-config.toml we can define these overrides for CI in one spot and not worry about it.
1111
[build]
1212
rustflags = ["-D", "warnings"]
13+
14+
[alias]
15+
xtask = "run --package xtask --"

‎.cargo/config.toml

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[build]
22
# v0 mangling scheme provides more detailed backtraces around closures
33
rustflags = ["-C", "symbol-mangling-version=v0"]
4+
5+
[alias]
6+
xtask = "run --package xtask --"

‎Cargo.lock

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ members = [
9393
"crates/zed",
9494
"crates/zed_actions",
9595
"extensions/gleam",
96+
"tooling/xtask",
9697
]
9798
default-members = ["crates/zed"]
9899
resolver = "2"
@@ -200,6 +201,7 @@ blade-graphics = { git = "https://github.com/kvark/blade", rev = "e9d93a4d41f394
200201
blade-macros = { git = "https://github.com/kvark/blade", rev = "e9d93a4d41f3946a03ffb76136290d6ccf7f2b80" }
201202
blade-rwh = { package = "raw-window-handle", version = "0.5" }
202203
chrono = { version = "0.4", features = ["serde"] }
204+
clap = "4.4"
203205
clickhouse = { version = "0.11.6" }
204206
ctor = "0.2.6"
205207
core-foundation = { version = "0.9.3" }

‎crates/cli/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ path = "src/main.rs"
1515

1616
[dependencies]
1717
anyhow.workspace = true
18+
# TODO: Use workspace version of `clap`.
1819
clap = { version = "3.1", features = ["derive"] }
1920
ipc-channel = "0.16"
2021
serde.workspace = true

‎crates/storybook/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ path = "src/storybook.rs"
1111

1212
[dependencies]
1313
anyhow.workspace = true
14-
clap = { version = "4.4", features = ["derive", "string"] }
14+
clap = { workspace = true, features = ["derive", "string"] }
1515
collab_ui = { workspace = true, features = ["stories"] }
1616
ctrlc = "3.4"
1717
dialoguer = { version = "0.11.0", features = ["fuzzy-select"] }

‎crates/theme_importer/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ license = "GPL-3.0-or-later"
77

88
[dependencies]
99
anyhow.workspace = true
10-
clap = { version = "4.4", features = ["derive"] }
10+
clap = { workspace = true, features = ["derive"] }
1111
gpui.workspace = true
1212
indexmap = { version = "1.6.2", features = ["serde"] }
1313
log.workspace = true

‎script/clippy

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,4 @@
22

33
set -euxo pipefail
44

5-
# clippy.toml is not currently supporting specifying allowed lints
6-
# so specify those here, and disable the rest until Zed's workspace
7-
# will have more fixes & suppression for the standard lint set
8-
cargo clippy --release --workspace --all-features --all-targets -- --allow clippy::all --deny clippy::dbg_macro --deny clippy::todo
9-
cargo clippy --package gpui -- --deny warnings
5+
cargo xtask clippy

‎tooling/xtask/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "xtask"
3+
version = "0.1.0"
4+
edition = "2021"
5+
publish = false
6+
license = "GPL-3.0-or-later"
7+
8+
[dependencies]
9+
anyhow.workspace = true
10+
clap = { workspace = true, features = ["derive"] }

‎tooling/xtask/LICENSE-GPL

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../LICENSE-GPL

‎tooling/xtask/src/main.rs

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use std::process::Command;
2+
3+
use anyhow::{bail, Context, Result};
4+
use clap::{Parser, Subcommand};
5+
6+
#[derive(Parser)]
7+
#[command(name = "cargo xtask")]
8+
struct Args {
9+
#[command(subcommand)]
10+
command: CliCommand,
11+
}
12+
13+
#[derive(Subcommand)]
14+
enum CliCommand {
15+
/// Runs `cargo clippy`.
16+
Clippy(ClippyArgs),
17+
}
18+
19+
fn main() -> Result<()> {
20+
let args = Args::parse();
21+
22+
match args.command {
23+
CliCommand::Clippy(args) => run_clippy(args),
24+
}
25+
}
26+
27+
#[derive(Parser)]
28+
struct ClippyArgs {
29+
/// Whether to deny warnings (`clippy --deny warnings`).
30+
#[arg(long)]
31+
deny_warnings: bool,
32+
}
33+
34+
fn run_clippy(args: ClippyArgs) -> Result<()> {
35+
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
36+
37+
let mut clippy_command = Command::new(&cargo);
38+
clippy_command
39+
.arg("clippy")
40+
.arg("--workspace")
41+
.arg("--release")
42+
.arg("--all-targets")
43+
.arg("--all-features");
44+
45+
clippy_command.arg("--");
46+
47+
if args.deny_warnings {
48+
clippy_command.args(["--deny", "warnings"]);
49+
}
50+
51+
// Allow all Clippy lints by default, as we have a lot of violations at the moment.
52+
// We can tighten things up once we have a better handle on them.
53+
clippy_command.args(["--allow", "clippy::all"]);
54+
55+
// Deny `dbg!` and `todo!`s.
56+
clippy_command
57+
.args(["--deny", "clippy::dbg_macro"])
58+
.args(["--deny", "clippy::todo"]);
59+
60+
eprintln!(
61+
"running: {cargo} {}",
62+
clippy_command
63+
.get_args()
64+
.map(|arg| format!("{}", arg.to_str().unwrap()))
65+
.collect::<Vec<_>>()
66+
.join(" ")
67+
);
68+
69+
let exit_status = clippy_command
70+
.spawn()
71+
.context("failed to spawn child process")?
72+
.wait()
73+
.context("failed to wait for child process")?;
74+
75+
if !exit_status.success() {
76+
bail!("clippy failed: {}", exit_status);
77+
}
78+
79+
Ok(())
80+
}

0 commit comments

Comments
 (0)
Please sign in to comment.