Skip to content

Commit b401f77

Browse files
committed
fix clippy
1 parent 3f65b3c commit b401f77

File tree

7 files changed

+72
-76
lines changed

7 files changed

+72
-76
lines changed

crates/cargo-gpu/src/build.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ pub struct Build {
2424

2525
impl Build {
2626
/// Entrypoint
27-
#[expect(clippy::too_many_lines, reason = "It's not too confusing")]
2827
pub fn run(&mut self) -> anyhow::Result<()> {
2928
let (rustc_codegen_spirv_location, toolchain_channel) = self.install.run()?;
3029

@@ -76,7 +75,7 @@ impl Build {
7675

7776
let shaders = match &result.module {
7877
ModuleResult::MultiModule(modules) => {
79-
assert!(!modules.is_empty(), "No shader modules to compile");
78+
anyhow::ensure!(!modules.is_empty(), "No shader modules were compiled");
8079
modules.iter().collect::<Vec<_>>()
8180
}
8281
ModuleResult::SingleModule(filepath) => result
@@ -95,7 +94,7 @@ impl Build {
9594
.context("Couldn't parse file name from shader module path")?,
9695
);
9796
log::debug!("copying {} to {}", filepath.display(), path.display());
98-
std::fs::copy(&filepath, &path)?;
97+
std::fs::copy(filepath, &path)?;
9998
log::debug!(
10099
"linkage of {} relative to {}",
101100
path.display(),

crates/cargo-gpu/src/install.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl Install {
4343
trace!("writing dummy Cargo.toml");
4444
let version_spec = match &source {
4545
SpirvSource::CratesIO(version) => {
46-
format!("version = \"{}\"", version)
46+
format!("version = \"{version}\"")
4747
}
4848
SpirvSource::Git { url, rev } => format!("git = \"{url}\"\nrev = \"{rev}\""),
4949
SpirvSource::Path {
@@ -52,7 +52,7 @@ impl Install {
5252
} => {
5353
let mut new_path = rust_gpu_path.to_owned();
5454
new_path.push("crates/spirv-builder");
55-
format!("path = \"{new_path}\"\nversion = \"{}\"", version)
55+
format!("path = \"{new_path}\"\nversion = \"{version}\"")
5656
}
5757
};
5858
let cargo_toml = format!(
@@ -90,6 +90,7 @@ package = "rustc_codegen_spirv"
9090
}
9191

9292
/// Install the binary pair and return the `(dylib_path, toolchain_channel)`.
93+
#[expect(clippy::too_many_lines, reason = "it's fine")]
9394
pub fn run(&mut self) -> anyhow::Result<(PathBuf, String)> {
9495
// Ensure the cache dir exists
9596
let cache_dir = cache_dir()?;
@@ -164,7 +165,7 @@ package = "rustc_codegen_spirv"
164165
let mut build_command = std::process::Command::new("cargo");
165166
build_command
166167
.current_dir(&checkout)
167-
.arg(format!("+{}", toolchain_channel))
168+
.arg(format!("+{toolchain_channel}"))
168169
.args(["build", "--release"])
169170
.env_remove("RUSTC");
170171
if source_is_path {
@@ -207,7 +208,7 @@ package = "rustc_codegen_spirv"
207208
.context("writing target spec files")?;
208209
}
209210

210-
self.spirv_install.dylib_path = dest_dylib_path.clone();
211+
self.spirv_install.dylib_path.clone_from(&dest_dylib_path);
211212
Ok((dest_dylib_path, toolchain_channel))
212213
}
213214
}

crates/cargo-gpu/src/install_toolchain.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use anyhow::Context;
1+
//! toolchain installation logic
2+
3+
use anyhow::Context as _;
24

35
/// Use `rustup` to install the toolchain and components, if not already installed.
46
///
@@ -22,11 +24,11 @@ pub fn ensure_toolchain_and_components_exist(
2224
let string_toolchain_list = String::from_utf8_lossy(&output_toolchain_list.stdout);
2325
if string_toolchain_list
2426
.split_whitespace()
25-
.any(|toolchain| toolchain.starts_with(&channel))
27+
.any(|toolchain| toolchain.starts_with(channel))
2628
{
27-
log::debug!("toolchain {} is already installed", channel);
29+
log::debug!("toolchain {channel} is already installed");
2830
} else {
29-
let message = format!("Rust {} with `rustup`", channel);
31+
let message = format!("Rust {channel} with `rustup`");
3032
get_consent_for_toolchain_install(
3133
format!("Install {message}").as_ref(),
3234
skip_toolchain_install_consent,
@@ -35,7 +37,7 @@ pub fn ensure_toolchain_and_components_exist(
3537

3638
let output_toolchain_add = std::process::Command::new("rustup")
3739
.args(["toolchain", "add"])
38-
.arg(&channel)
40+
.arg(channel)
3941
.stdout(std::process::Stdio::inherit())
4042
.stderr(std::process::Stdio::inherit())
4143
.output()
@@ -49,7 +51,7 @@ pub fn ensure_toolchain_and_components_exist(
4951
// Check for the required components
5052
let output_component_list = std::process::Command::new("rustup")
5153
.args(["component", "list", "--toolchain"])
52-
.arg(&channel)
54+
.arg(channel)
5355
.output()
5456
.context("getting toolchain list")?;
5557
anyhow::ensure!(
@@ -78,7 +80,7 @@ pub fn ensure_toolchain_and_components_exist(
7880

7981
let output_component_add = std::process::Command::new("rustup")
8082
.args(["component", "add", "--toolchain"])
81-
.arg(&channel)
83+
.arg(channel)
8284
.args(["rust-src", "rustc-dev", "llvm-tools"])
8385
.stdout(std::process::Stdio::inherit())
8486
.stderr(std::process::Stdio::inherit())
@@ -126,6 +128,7 @@ fn get_consent_for_toolchain_install(
126128
Ok(())
127129
} else {
128130
crate::user_output!("Exiting...\n");
131+
#[expect(clippy::exit, reason = "user requested abort")]
129132
std::process::exit(0);
130133
}
131134
}

crates/cargo-gpu/src/linkage.rs

-6
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,4 @@ impl Linkage {
2727
entry_point: entry_point.as_ref().to_owned(),
2828
}
2929
}
30-
31-
/// The entry point function name, without the fully qualified mod path
32-
#[expect(clippy::unwrap_used, reason = "unreachable")]
33-
pub fn fn_name(&self) -> &str {
34-
self.entry_point.split("::").last().unwrap()
35-
}
3630
}

crates/cargo-gpu/src/lockfile.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl LockfileMismatchHandler {
4040
let maybe_workspace_crate_lock =
4141
Self::ensure_shader_rust_version_doesnt_conflict_with_any_cargo_locks(
4242
shader_crate_path,
43-
&toolchain_channel,
43+
toolchain_channel,
4444
is_force_overwrite_lockfiles_v4_to_v3,
4545
)
4646
.context("ensure_shader_rust_version_doesnt_conflict_with_any_cargo_locks")?;

crates/cargo-gpu/src/metadata.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ impl Metadata {
8080
/// Get any `rust-gpu` metadata set in the crate's `Cargo.toml`
8181
fn get_crate_metadata(
8282
json: &cargo_metadata::Metadata,
83-
shader_crate_path: &std::path::Path,
83+
path: &std::path::Path,
8484
) -> anyhow::Result<Value> {
85-
let shader_crate_path = std::fs::canonicalize(shader_crate_path)?.join("Cargo.toml");
85+
let shader_crate_path = std::fs::canonicalize(path)?.join("Cargo.toml");
8686

8787
for package in &json.packages {
8888
let manifest_path = std::fs::canonicalize(package.manifest_path.as_std_path())?;
@@ -99,6 +99,7 @@ impl Metadata {
9999
Ok(serde_json::json!({}))
100100
}
101101

102+
/// Get `rust-gpu` value from some metadata
102103
fn get_rust_gpu_from_metadata(metadata: &Value) -> Value {
103104
Self::keys_to_snake_case(
104105
metadata
@@ -112,6 +113,7 @@ impl Metadata {
112113
///
113114
/// Detection of keys for serde deserialization must match the case in the Rust structs.
114115
/// However clap defaults to detecting CLI args in kebab case. So here we do the conversion.
116+
#[expect(clippy::wildcard_enum_match_arm, reason = "we only want objects")]
115117
fn keys_to_snake_case(json: Value) -> Value {
116118
match json {
117119
Value::Object(object) => Value::Object(
@@ -120,7 +122,7 @@ impl Metadata {
120122
.map(|(key, value)| (key.replace('-', "_"), Self::keys_to_snake_case(value)))
121123
.collect(),
122124
),
123-
e => e,
125+
other => other,
124126
}
125127
}
126128
}
@@ -182,7 +184,7 @@ mod test {
182184
let cargo_gpu = metadata
183185
.packages
184186
.iter_mut()
185-
.find(|p| p.name.contains("cargo-gpu"))
187+
.find(|package| package.name.contains("cargo-gpu"))
186188
.unwrap();
187189
cargo_gpu.metadata = serde_json::json!({
188190
"rust-gpu": {

crates/cargo-gpu/src/spirv_source.rs

+48-51
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! version. Then with that we `git checkout` the `rust-gpu` repo that corresponds to that version.
55
//! From there we can look at the source code to get the required Rust toolchain.
66
7-
use anyhow::{anyhow, Context as _};
7+
use anyhow::Context as _;
88
use cargo_metadata::camino::{Utf8Path, Utf8PathBuf};
99
use cargo_metadata::semver::Version;
1010
use cargo_metadata::{MetadataCommand, Package};
@@ -65,30 +65,31 @@ impl core::fmt::Display for SpirvSource {
6565
}
6666

6767
impl SpirvSource {
68+
/// Figures out which source of `rust-gpu` to use
6869
pub fn new(
6970
shader_crate_path: &Path,
7071
maybe_rust_gpu_source: Option<&str>,
7172
maybe_rust_gpu_version: Option<&str>,
7273
) -> anyhow::Result<Self> {
7374
let source = if let Some(rust_gpu_version) = maybe_rust_gpu_version {
7475
if let Some(rust_gpu_source) = maybe_rust_gpu_source {
75-
SpirvSource::Git {
76+
Self::Git {
7677
url: rust_gpu_source.to_owned(),
7778
rev: rust_gpu_version.to_owned(),
7879
}
7980
} else {
80-
SpirvSource::CratesIO(Version::parse(&rust_gpu_version)?)
81+
Self::CratesIO(Version::parse(rust_gpu_version)?)
8182
}
8283
} else {
83-
SpirvSource::get_rust_gpu_deps_from_shader(shader_crate_path)
84+
Self::get_rust_gpu_deps_from_shader(shader_crate_path)
8485
.context("get_rust_gpu_deps_from_shader")?
8586
};
8687
Ok(source)
8788
}
8889

8990
/// Look into the shader crate to get the version of `rust-gpu` it's using.
9091
pub fn get_rust_gpu_deps_from_shader(shader_crate_path: &Path) -> anyhow::Result<Self> {
91-
let spirv_std_package = get_package_from_crate(&shader_crate_path, "spirv-std")?;
92+
let spirv_std_package = get_package_from_crate(shader_crate_path, "spirv-std")?;
9293
let spirv_source = Self::parse_spirv_std_source_and_version(&spirv_std_package)?;
9394
log::debug!(
9495
"Parsed `SpirvSource` from crate `{}`: \
@@ -103,10 +104,10 @@ impl SpirvSource {
103104
/// maybe using their own fork for example.
104105
pub fn install_dir(&self) -> anyhow::Result<PathBuf> {
105106
match self {
106-
SpirvSource::Path {
107+
Self::Path {
107108
rust_gpu_repo_root, ..
108109
} => Ok(rust_gpu_repo_root.as_std_path().to_owned()),
109-
SpirvSource::CratesIO { .. } | SpirvSource::Git { .. } => {
110+
Self::CratesIO { .. } | Self::Git { .. } => {
110111
let dir = crate::to_dirname(self.to_string().as_ref());
111112
Ok(crate::cache_dir()?
112113
.join("rustc_backend_spirv_install")
@@ -120,53 +121,47 @@ impl SpirvSource {
120121
/// Which would return:
121122
/// `SpirvSource::Git("https://github.com/Rust-GPU/rust-gpu", "54f6978c")`
122123
fn parse_spirv_std_source_and_version(spirv_std_package: &Package) -> anyhow::Result<Self> {
123-
log::trace!(
124-
"parsing spirv-std source and version from package: '{:?}'",
125-
spirv_std_package
126-
);
124+
log::trace!("parsing spirv-std source and version from package: '{spirv_std_package:?}'");
127125

128-
let result = match &spirv_std_package.source {
129-
Some(source) => {
130-
let is_git = source.repr.starts_with("git+");
131-
let is_crates_io = source.is_crates_io();
126+
let result = if let Some(source) = &spirv_std_package.source {
127+
let is_git = source.repr.starts_with("git+");
128+
let is_crates_io = source.is_crates_io();
132129

133-
match (is_git, is_crates_io) {
134-
(true, true) => unreachable!(),
135-
(true, false) => {
136-
let link = &source.repr[4..];
137-
let sharp_index = link.find('#').ok_or(anyhow!(
138-
"Git url of spirv-std package does not contain revision!"
139-
))?;
140-
let question_mark_index = link.find('?').ok_or(anyhow!(
141-
"Git url of spirv-std package does not contain revision!"
142-
))?;
143-
let url = link[..question_mark_index].to_string();
144-
let rev = link[sharp_index + 1..].to_string();
145-
Self::Git { url, rev }
146-
}
147-
(false, true) => Self::CratesIO(spirv_std_package.version.clone()),
148-
(false, false) => {
149-
anyhow::bail!("Metadata of spirv-std package uses unknown url format!")
150-
}
130+
match (is_git, is_crates_io) {
131+
(true, true) => anyhow::bail!("parsed both git and crates.io?"),
132+
(true, false) => {
133+
let parse_git = || {
134+
let link = &source.repr.get(4..)?;
135+
let sharp_index = link.find('#')?;
136+
let question_mark_index = link.find('?')?;
137+
let url = link.get(..question_mark_index)?.to_owned();
138+
let rev = link.get(sharp_index + 1..)?.to_owned();
139+
Some(Self::Git { url, rev })
140+
};
141+
parse_git()
142+
.with_context(|| format!("Failed to parse git url {}", &source.repr))?
151143
}
152-
}
153-
None => {
154-
let rust_gpu_repo_root = spirv_std_package
155-
.manifest_path // rust-gpu/crates/spirv-std/Cargo.toml
156-
.parent() // rust-gpu/crates/spirv-std
157-
.and_then(Utf8Path::parent) // rust-gpu/crates
158-
.and_then(Utf8Path::parent) // rust-gpu
159-
.context("selecting rust-gpu workspace root dir in local path")?
160-
.to_owned();
161-
if !rust_gpu_repo_root.is_dir() {
162-
anyhow::bail!("path {rust_gpu_repo_root} is not a directory");
163-
}
164-
let version = spirv_std_package.version.clone();
165-
Self::Path {
166-
rust_gpu_repo_root,
167-
version,
144+
(false, true) => Self::CratesIO(spirv_std_package.version.clone()),
145+
(false, false) => {
146+
anyhow::bail!("Metadata of spirv-std package uses unknown url format!")
168147
}
169148
}
149+
} else {
150+
let rust_gpu_repo_root = spirv_std_package
151+
.manifest_path // rust-gpu/crates/spirv-std/Cargo.toml
152+
.parent() // rust-gpu/crates/spirv-std
153+
.and_then(Utf8Path::parent) // rust-gpu/crates
154+
.and_then(Utf8Path::parent) // rust-gpu
155+
.context("selecting rust-gpu workspace root dir in local path")?
156+
.to_owned();
157+
if !rust_gpu_repo_root.is_dir() {
158+
anyhow::bail!("path {rust_gpu_repo_root} is not a directory");
159+
}
160+
let version = spirv_std_package.version.clone();
161+
Self::Path {
162+
rust_gpu_repo_root,
163+
version,
164+
}
170165
};
171166

172167
log::debug!("Parsed `rust-gpu` source and version: {result:?}");
@@ -234,8 +229,10 @@ pub fn get_channel_from_rustc_codegen_spirv_build_script(
234229
.lines()
235230
.find_map(|line| line.strip_prefix(channel_start))
236231
.context(format!("Can't find `{channel_start}` line in {build_rs:?}"))?;
237-
let channel = &channel_line[..channel_line.find("\"").context("ending \" missing")?];
238-
Ok(channel.to_string())
232+
let channel = channel_line
233+
.get(..channel_line.find('"').context("ending \" missing")?)
234+
.context("can't slice version")?;
235+
Ok(channel.to_owned())
239236
}
240237

241238
#[cfg(test)]

0 commit comments

Comments
 (0)