Skip to content

Commit 708fefc

Browse files
committed
Add stricter lints and make them happy
1 parent 2579b5d commit 708fefc

File tree

9 files changed

+186
-100
lines changed

9 files changed

+186
-100
lines changed

.github/workflows/push.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
name: push
22

3-
on:
3+
on:
44
push:
5-
branches:
5+
branches:
66
- main
77
pull_request:
88

@@ -13,18 +13,18 @@ env:
1313
jobs:
1414
install-and-build-shaders:
1515
strategy:
16-
matrix:
16+
matrix:
1717
os: [ubuntu-latest, macos-latest, windows-latest]
1818
runs-on: ${{ matrix.os }}
1919
defaults:
20-
run:
20+
run:
2121
shell: bash
22-
env:
22+
env:
2323
RUST_LOG: debug
2424
steps:
2525
- uses: actions/checkout@v2
2626
- uses: moonrepo/setup-rust@v1
27-
- run: rustup default stable
27+
- run: rustup default stable
2828
- run: rustup update
2929
- run: cargo test
3030
- run: cargo install --path crates/cargo-gpu

Cargo.toml

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[workspace]
22
members = [
3-
"crates/cargo-gpu",
3+
"crates/cargo-gpu",
44
"crates/shader-crate-template"
55
]
66

@@ -21,3 +21,32 @@ relative-path = "1.9.3"
2121
serde = { version = "1.0.214", features = ["derive"] }
2222
serde_json = "1.0.132"
2323
toml = "0.8.19"
24+
25+
[workspace.lints.rust]
26+
missing_docs = "warn"
27+
28+
[workspace.lints.clippy]
29+
all = { level = "warn", priority = 0 }
30+
pedantic = { level = "warn", priority = 0 }
31+
nursery = { level = "warn", priority = 0 }
32+
cargo = { level = "warn", priority = 0 }
33+
restriction = { level = "warn", priority = 0 }
34+
blanket_clippy_restriction_lints = { level = "allow", priority = 1 }
35+
36+
arithmetic_side_effects = { level = "allow", priority = 1 }
37+
absolute_paths = { level = "allow", priority = 1 }
38+
cargo_common_metadata = { level = "allow", priority = 1 }
39+
implicit_return = { level = "allow", priority = 1 }
40+
single_call_fn = { level = "allow", priority = 1 }
41+
question_mark_used = { level = "allow", priority = 1 }
42+
multiple_crate_versions = { level = "allow", priority = 1 }
43+
pub_with_shorthand = { level = "allow", priority = 1 }
44+
partial_pub_fields = { level = "allow", priority = 1 }
45+
pattern_type_mismatch = { level = "allow", priority = 1 }
46+
print_stdout = { level = "allow", priority = 1 }
47+
std_instead_of_alloc = { level = "allow", priority = 1 }
48+
49+
# TODO: Try to not depend on these lints
50+
unwrap_used = { level = "allow", priority = 1 }
51+
panic = { level = "allow", priority = 1 }
52+

crates/cargo-gpu/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ name = "cargo-gpu"
33
version = "0.1.0"
44
edition = "2021"
55
description = "Generates shader .spv files from rust-gpu shader crates"
6+
repository = "https://github.com/Rust-GPU/cargo-gpu"
67
readme = "../../README.md"
7-
8+
keywords = ["gpu", "compiler"]
89
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
910

1011
[dependencies]
@@ -33,3 +34,6 @@ codegen-units = 256
3334
opt-level = 3
3435
incremental = true
3536
codegen-units = 256
37+
38+
[lints]
39+
workspace = true

crates/cargo-gpu/src/builder.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
use std::io::Write;
1+
//! `cargo gpu build`, analogous to `cargo build`
2+
3+
use std::io::Write as _;
24

35
use clap::Parser;
46
use spirv_builder_cli::{Linkage, ShaderModule};
57

68
use crate::{install::Install, target_spec_dir};
79

10+
/// `cargo build` subcommands
811
#[derive(Parser, Debug)]
9-
pub(crate) struct Build {
12+
pub struct Build {
13+
/// Install the `rust-gpu` compiler and components
1014
#[clap(flatten)]
1115
install: Install,
1216

@@ -32,6 +36,7 @@ pub(crate) struct Build {
3236
}
3337

3438
impl Build {
39+
/// Entrypoint
3540
pub fn run(&mut self) {
3641
let (dylib_path, spirv_builder_cli_path) = self.install.run();
3742

@@ -93,7 +98,7 @@ impl Build {
9398
entry,
9499
path: filepath,
95100
}| {
96-
use relative_path::PathExt;
101+
use relative_path::PathExt as _;
97102
let path = self.output_dir.join(filepath.file_name().unwrap());
98103
std::fs::copy(&filepath, &path).unwrap();
99104
let path_relative_to_shader_crate =
@@ -109,19 +114,19 @@ impl Build {
109114
linkage.sort();
110115
// UNWRAP: safe because we know this always serializes
111116
let json = serde_json::to_string_pretty(&linkage).unwrap();
112-
let mut file = std::fs::File::create(&manifest_path).unwrap_or_else(|e| {
117+
let mut file = std::fs::File::create(&manifest_path).unwrap_or_else(|error| {
113118
log::error!(
114-
"could not create shader manifest file '{}': {e}",
119+
"could not create shader manifest file '{}': {error}",
115120
manifest_path.display(),
116121
);
117-
panic!("{e}")
122+
panic!("{error}")
118123
});
119-
file.write_all(json.as_bytes()).unwrap_or_else(|e| {
124+
file.write_all(json.as_bytes()).unwrap_or_else(|error| {
120125
log::error!(
121-
"could not write shader manifest file '{}': {e}",
126+
"could not write shader manifest file '{}': {error}",
122127
manifest_path.display(),
123128
);
124-
panic!("{e}")
129+
panic!("{error}")
125130
});
126131

127132
log::info!("wrote manifest to '{}'", manifest_path.display());

crates/cargo-gpu/src/install.rs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
use std::io::Write;
1+
//! Install a dedicated per-shader crate that has the `rust-gpu` compiler in it.
2+
use std::io::Write as _;
23

34
use crate::{cache_dir, spirv::Spirv, target_spec_dir};
45

5-
const SPIRV_BUILDER_CLI_CARGO_TOML: &str = include_str!("../../spirv-builder-cli/Cargo.toml");
6-
const SPIRV_BUILDER_CLI_MAIN: &str = include_str!("../../spirv-builder-cli/src/main.rs");
7-
const SPIRV_BUILDER_CLI_LIB: &str = include_str!("../../spirv-builder-cli/src/lib.rs");
6+
/// These are the files needed to create the dedicated, per-shader `rust-gpu` builder create.
87
const SPIRV_BUILDER_FILES: &[(&str, &str)] = &[
9-
("Cargo.toml", SPIRV_BUILDER_CLI_CARGO_TOML),
10-
("src/main.rs", SPIRV_BUILDER_CLI_MAIN),
11-
("src/lib.rs", SPIRV_BUILDER_CLI_LIB),
8+
(
9+
"Cargo.toml",
10+
include_str!("../../spirv-builder-cli/Cargo.toml"),
11+
),
12+
(
13+
"src/main.rs",
14+
include_str!("../../spirv-builder-cli/src/main.rs"),
15+
),
16+
(
17+
"src/lib.rs",
18+
include_str!("../../spirv-builder-cli/src/lib.rs"),
19+
),
1220
];
1321

22+
/// Metadata for the compile targets supported by `rust-gpu`
1423
const TARGET_SPECS: &[(&str, &str)] = &[
1524
(
1625
"spirv-unknown-opengl4.0.json",
@@ -74,15 +83,16 @@ const TARGET_SPECS: &[(&str, &str)] = &[
7483
),
7584
];
7685

86+
/// `cargo gpu install`
7787
#[derive(clap::Parser, Debug)]
78-
pub(crate) struct Install {
88+
pub struct Install {
7989
/// spirv-builder dependency, written just like in a Cargo.toml file.
8090
#[clap(long, default_value = Spirv::DEFAULT_DEP)]
8191
spirv_builder: String,
8292

8393
/// Rust toolchain channel to use to build `spirv-builder`.
8494
///
85-
/// This must match the `spirv_builder` argument.
95+
/// This must be compatible with the `spirv_builder` argument as defined in the `rust-gpu` repo.
8696
#[clap(long, default_value = Spirv::DEFAULT_CHANNEL)]
8797
rust_toolchain: String,
8898

@@ -92,18 +102,20 @@ pub(crate) struct Install {
92102
}
93103

94104
impl Install {
105+
/// Returns a [`Spirv`] instance, responsible for ensuring the right version of the `spirv-builder-cli` crate.
95106
fn spirv_cli(&self) -> Spirv {
96107
Spirv {
97108
dep: self.spirv_builder.clone(),
98109
channel: self.rust_toolchain.clone(),
99110
}
100111
}
101112

113+
/// Create the `spirv-builder-cli` crate.
102114
fn write_source_files(&self) {
103115
let cli = self.spirv_cli();
104116
let checkout = cli.cached_checkout_path();
105117
std::fs::create_dir_all(checkout.join("src")).unwrap();
106-
for (filename, contents) in SPIRV_BUILDER_FILES.iter() {
118+
for (filename, contents) in SPIRV_BUILDER_FILES {
107119
log::debug!("writing {filename}");
108120
let path = checkout.join(filename);
109121
let mut file = std::fs::File::create(&path).unwrap();
@@ -114,8 +126,9 @@ impl Install {
114126
}
115127
}
116128

129+
/// Add the target spec files to the crate.
117130
fn write_target_spec_files(&self) {
118-
for (filename, contents) in TARGET_SPECS.iter() {
131+
for (filename, contents) in TARGET_SPECS {
119132
let path = target_spec_dir().join(filename);
120133
if !path.is_file() || self.force_spirv_cli_rebuild {
121134
let mut file = std::fs::File::create(&path).unwrap();
@@ -124,14 +137,14 @@ impl Install {
124137
}
125138
}
126139

127-
// Install the binary pair and return the paths, (dylib, cli).
140+
/// Install the binary pair and return the paths, (dylib, cli).
128141
pub fn run(&self) -> (std::path::PathBuf, std::path::PathBuf) {
129142
// Ensure the cache dir exists
130143
let cache_dir = cache_dir();
131144
log::info!("cache directory is '{}'", cache_dir.display());
132-
std::fs::create_dir_all(&cache_dir).unwrap_or_else(|e| {
145+
std::fs::create_dir_all(&cache_dir).unwrap_or_else(|error| {
133146
log::error!(
134-
"could not create cache directory '{}': {e}",
147+
"could not create cache directory '{}': {error}",
135148
cache_dir.display()
136149
);
137150
panic!("could not create cache dir");
@@ -178,7 +191,7 @@ impl Install {
178191

179192
command.args([
180193
"--features",
181-
&Self::get_required_spirv_builder_version(spirv_version.channel),
194+
&Self::get_required_spirv_builder_version(&spirv_version.channel),
182195
]);
183196

184197
log::debug!("building artifacts with `{:?}`", command);
@@ -209,8 +222,8 @@ impl Install {
209222
} else {
210223
log::error!("could not find {}", cli_path.display());
211224
log::debug!("contents of '{}':", release.display());
212-
for entry in std::fs::read_dir(&release).unwrap() {
213-
let entry = entry.unwrap();
225+
for maybe_entry in std::fs::read_dir(&release).unwrap() {
226+
let entry = maybe_entry.unwrap();
214227
log::debug!("{}", entry.file_name().to_string_lossy());
215228
}
216229
panic!("spirv-builder-cli build failed");
@@ -228,9 +241,9 @@ impl Install {
228241
/// `spirv-builder` version from there.
229242
/// * Warn the user that certain `cargo-gpu` features aren't available when building with
230243
/// older versions of `spirv-builder`, eg setting the target spec.
231-
fn get_required_spirv_builder_version(toolchain_channel: String) -> String {
244+
fn get_required_spirv_builder_version(toolchain_channel: &str) -> String {
232245
let parse_date = chrono::NaiveDate::parse_from_str;
233-
let datetime = parse_date(&toolchain_channel, "nightly-%Y-%m-%d").unwrap();
246+
let datetime = parse_date(toolchain_channel, "nightly-%Y-%m-%d").unwrap();
234247
let pre_cli_date = parse_date("2024-04-24", "%Y-%m-%d").unwrap();
235248

236249
if datetime < pre_cli_date {

0 commit comments

Comments
 (0)