Skip to content

Commit fd36c42

Browse files
Brooooooklynclaude
andauthored
style: fix clippy::items_after_statements warnings (#231)
style: fix clippy::items_after_statements warnings Move item declarations (const, use, extern) to the beginning of their scopes: - macos.rs: Move DYLD_INSERT_LIBRARIES const to function start - client/mod.rs: Move extern "C" blocks and use statements before code - unix/mod.rs: Move PRELOAD_CDYLIB and SAFE_FD_NUM consts to function/scope start These changes improve code organization and follow Rust best practices where items should be declared at the start of their scope. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> style: fix clippy::items_after_statements in test code Move item declarations (struct, use statements) to the beginning of test functions to comply with clippy::items_after_statements lint. Changes: - vite_package_manager/src/request.rs: Move struct and use statements to the beginning of test functions - vite_task/src/fingerprint.rs: Move use statements to the beginning of test functions and remove duplicate imports All tests pass and clippy is clean with -D clippy::items_after_statements. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 7c39ed6 commit fd36c42

5 files changed

Lines changed: 51 additions & 53 deletions

File tree

crates/fspy/src/unix/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,19 @@ impl SpyInner {
7373

7474
#[cfg(target_os = "macos")]
7575
pub fn init_in(dir: &Path) -> io::Result<Self> {
76+
const PRELOAD_CDYLIB: Fixture = Fixture {
77+
name: "fspy_preload",
78+
content: PRELOAD_CDYLIB_BINARY,
79+
hash: formatcp!("{:x}", xxh3_128(PRELOAD_CDYLIB_BINARY)),
80+
};
81+
7682
use const_format::formatcp;
7783
use xxhash_rust::const_xxh3::xxh3_128;
7884

7985
use crate::fixture::Fixture;
8086
let coreutils_path = macos_fixtures::COREUTILS_BINARY.write_to(dir, "")?;
8187
let bash_path = macos_fixtures::OILS_BINARY.write_to(dir, "")?;
8288

83-
const PRELOAD_CDYLIB: Fixture = Fixture {
84-
name: "fspy_preload",
85-
content: PRELOAD_CDYLIB_BINARY,
86-
hash: formatcp!("{:x}", xxh3_128(PRELOAD_CDYLIB_BINARY)),
87-
};
88-
8989
let preload_cdylib_path = PRELOAD_CDYLIB.write_to(dir, ".dylib")?;
9090
let fixtures = Fixtures {
9191
bash_path: bash_path.as_path().into(), //Path::new("/opt/homebrew/bin/bash"),//brush.as_path(),
@@ -154,8 +154,9 @@ impl PathAccessIterable {
154154

155155
// https://github.com/nodejs/node/blob/5794e644b724c6c6cac02d306d87a4d6b78251e5/deps/uv/src/unix/core.c#L803-L808
156156
fn duplicate_until_safe(mut fd: OwnedFd) -> io::Result<OwnedFd> {
157-
let mut fds: Vec<OwnedFd> = vec![];
158157
const SAFE_FD_NUM: RawFd = 17;
158+
159+
let mut fds: Vec<OwnedFd> = vec![];
159160
while fd.as_raw_fd() < SAFE_FD_NUM {
160161
let new_fd = fd.try_clone()?;
161162
fds.push(fd);

crates/fspy_preload_unix/src/client/mod.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ impl Client {
211211
file_actions: &mut *const libc::posix_spawn_file_actions_t,
212212
attrp: *const libc::posix_spawnattr_t,
213213
) -> nix::Result<()> {
214+
unsafe extern "C" {
215+
unsafe fn posix_spawn_file_actions_addinherit_np(
216+
actions: *mut libc::posix_spawn_file_actions_t,
217+
fd: libc::c_int,
218+
) -> libc::c_int;
219+
}
220+
214221
use core::mem::zeroed;
215222

216223
use libc::c_short;
@@ -229,13 +236,6 @@ impl Client {
229236
return Ok(());
230237
}
231238

232-
unsafe extern "C" {
233-
unsafe fn posix_spawn_file_actions_addinherit_np(
234-
actions: *mut libc::posix_spawn_file_actions_t,
235-
fd: libc::c_int,
236-
) -> libc::c_int;
237-
}
238-
239239
// ensure ipc fd is inherited when POSIX_SPAWN_CLOEXEC_DEFAULT is set.
240240
if (*file_actions).is_null() {
241241
let shared_file_actions = self.posix_spawn_file_actions.get_or_init(|| {
@@ -285,9 +285,6 @@ pub unsafe fn handle_open(path: impl ToAbsolutePath, mode: impl ToAccessMode) {
285285
#[cfg(not(test))]
286286
#[ctor::ctor]
287287
fn init_client() {
288-
use libc::pthread_atfork;
289-
290-
CLIENT.set(Client::from_env()).unwrap();
291288
unsafe extern "C" fn reset_shm_atfork() {
292289
let Some(client) = global_client() else {
293290
return;
@@ -301,6 +298,10 @@ fn init_client() {
301298
shm_cursor.position = shm_cursor.mmap_mut.len();
302299
});
303300
}
301+
302+
use libc::pthread_atfork;
303+
304+
CLIENT.set(Client::from_env()).unwrap();
304305
let ret = unsafe { pthread_atfork(None, None, Some(reset_shm_atfork)) };
305306
if ret != 0 {
306307
panic!("pthread_atfork failed: {}", ret);

crates/fspy_shared_unix/src/spawn/macos.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub fn handle_exec(
2929
command: &mut Exec,
3030
encoded_payload: &EncodedPayload,
3131
) -> nix::Result<Option<PreExec>> {
32+
const DYLD_INSERT_LIBRARIES: &[u8] = b"DYLD_INSERT_LIBRARIES";
33+
3234
if command.program.first() != Some(&b'/') {
3335
let program =
3436
absolute(OsStr::from_bytes(&command.program)).expect("Failed to get absolute path");
@@ -65,7 +67,6 @@ pub fn handle_exec(
6567
true
6668
};
6769

68-
const DYLD_INSERT_LIBRARIES: &[u8] = b"DYLD_INSERT_LIBRARIES";
6970
if injectable {
7071
ensure_env(
7172
&mut command.envs,

crates/vite_package_manager/src/request.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,13 @@ mod tests {
416416

417417
#[tokio::test]
418418
async fn test_http_client_get_json() {
419+
#[derive(serde::Deserialize, Debug, PartialEq)]
420+
struct PackageInfo {
421+
name: String,
422+
version: String,
423+
description: String,
424+
}
425+
419426
let server = MockServer::start();
420427

421428
// Create mock JSON response
@@ -435,13 +442,6 @@ mod tests {
435442
let client = HttpClient::new();
436443
let url = format!("{}/api/package.json", server.base_url());
437444

438-
#[derive(serde::Deserialize, Debug, PartialEq)]
439-
struct PackageInfo {
440-
name: String,
441-
version: String,
442-
description: String,
443-
}
444-
445445
let result: Result<PackageInfo, _> = client.get_json(&url).await;
446446
assert!(result.is_ok());
447447

@@ -523,6 +523,8 @@ mod tests {
523523

524524
#[tokio::test]
525525
async fn test_verify_file_hash_sha1() {
526+
use sha1::Sha1;
527+
use sha2::Digest;
526528
use tokio::io::AsyncWriteExt;
527529

528530
let temp_dir = TempDir::new().unwrap();
@@ -534,8 +536,6 @@ mod tests {
534536
file.write_all(content).await.unwrap();
535537

536538
// Calculate expected SHA1
537-
use sha1::Sha1;
538-
use sha2::Digest;
539539
let mut hasher = Sha1::new();
540540
hasher.update(content);
541541
let expected_hash = format!("sha1.{:x}", hasher.finalize());
@@ -552,6 +552,7 @@ mod tests {
552552

553553
#[tokio::test]
554554
async fn test_verify_file_hash_sha224() {
555+
use sha2::{Digest, Sha224};
555556
use tokio::io::AsyncWriteExt;
556557

557558
let temp_dir = TempDir::new().unwrap();
@@ -563,7 +564,6 @@ mod tests {
563564
file.write_all(content).await.unwrap();
564565

565566
// Calculate expected SHA224
566-
use sha2::{Digest, Sha224};
567567
let mut hasher = Sha224::new();
568568
hasher.update(content);
569569
let expected_hash = format!("sha224.{:x}", hasher.finalize());
@@ -603,6 +603,11 @@ mod tests {
603603

604604
#[tokio::test]
605605
async fn test_http_client_json_with_invalid_response() {
606+
#[derive(serde::Deserialize)]
607+
struct TestData {
608+
_field: String,
609+
}
610+
606611
let server = MockServer::start();
607612

608613
// Mock response with invalid JSON
@@ -614,11 +619,6 @@ mod tests {
614619
let client = HttpClient::new();
615620
let url = format!("{}/invalid.json", server.base_url());
616621

617-
#[derive(serde::Deserialize)]
618-
struct TestData {
619-
_field: String,
620-
}
621-
622622
let result: Result<TestData, _> = client.get_json(&url).await;
623623
assert!(result.is_err(), "Expected JSON parsing to fail");
624624
}

crates/vite_task/src/fingerprint.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ mod tests {
132132

133133
#[test]
134134
fn test_command_fingerprint_stable_with_multiple_envs() {
135+
use bincode::{decode_from_slice, encode_to_vec};
136+
135137
// Test that CommandFingerprint with TaskCommand::Parsed maintains stable ordering
136138
let parsed_cmd = TaskParsedCommand {
137139
envs: [
@@ -173,7 +175,6 @@ mod tests {
173175
};
174176

175177
// Serialize both fingerprints
176-
use bincode::{decode_from_slice, encode_to_vec};
177178
let config = bincode::config::standard();
178179

179180
let bytes1 = encode_to_vec(&fingerprint1, config).unwrap();
@@ -193,6 +194,13 @@ mod tests {
193194

194195
#[test]
195196
fn test_fingerprint_stability_across_runs() {
197+
use std::{
198+
collections::hash_map::DefaultHasher,
199+
hash::{Hash, Hasher},
200+
};
201+
202+
use bincode::encode_to_vec;
203+
196204
// This test simulates what happens when the same task is fingerprinted
197205
// multiple times across different program runs
198206

@@ -222,16 +230,10 @@ mod tests {
222230
};
223231

224232
// Serialize the fingerprint
225-
use bincode::encode_to_vec;
226233
let config = bincode::config::standard();
227234
let bytes = encode_to_vec(&fingerprint, config).unwrap();
228235

229236
// Create a hash of the serialized bytes to verify stability
230-
use std::{
231-
collections::hash_map::DefaultHasher,
232-
hash::{Hash, Hasher},
233-
};
234-
235237
let mut hasher = DefaultHasher::new();
236238
bytes.hash(&mut hasher);
237239
let hash = hasher.finish();
@@ -245,6 +247,8 @@ mod tests {
245247

246248
#[test]
247249
fn test_task_config_with_sorted_envs() {
250+
use bincode::encode_to_vec;
251+
248252
// Test that TaskConfig produces stable fingerprints even with HashSet envs
249253
let mut envs = HashSet::new();
250254
envs.insert("VAR_3".into());
@@ -265,7 +269,6 @@ mod tests {
265269
let resolved = ResolvedTaskConfig { config_dir: RelativePathBuf::default(), config };
266270

267271
// Serialize multiple times
268-
use bincode::encode_to_vec;
269272
let bincode_config = bincode::config::standard();
270273

271274
let bytes1 = encode_to_vec(&resolved, bincode_config).unwrap();
@@ -675,12 +678,9 @@ mod tests {
675678

676679
#[test]
677680
fn test_command_fingerprint_with_fingerprint_ignores() {
678-
// Test that CommandFingerprint includes fingerprint_ignores
679-
use crate::{
680-
cmd::TaskParsedCommand,
681-
config::{CommandFingerprint, TaskCommand},
682-
};
681+
use bincode::encode_to_vec;
683682

683+
// Test that CommandFingerprint includes fingerprint_ignores
684684
let parsed_cmd = TaskParsedCommand {
685685
envs: [].into(),
686686
program: "pnpm".into(),
@@ -710,7 +710,6 @@ mod tests {
710710
assert_ne!(fingerprint_with_ignores, fingerprint_without_ignores);
711711

712712
// Serialize to verify they produce different cache keys
713-
use bincode::encode_to_vec;
714713
let config = bincode::config::standard();
715714

716715
let bytes_with = encode_to_vec(&fingerprint_with_ignores, config).unwrap();
@@ -724,12 +723,9 @@ mod tests {
724723

725724
#[test]
726725
fn test_command_fingerprint_ignores_order_matters() {
727-
// Test that the order of fingerprint_ignores patterns matters
728-
use crate::{
729-
cmd::TaskParsedCommand,
730-
config::{CommandFingerprint, TaskCommand},
731-
};
726+
use bincode::encode_to_vec;
732727

728+
// Test that the order of fingerprint_ignores patterns matters
733729
let parsed_cmd =
734730
TaskParsedCommand { envs: [].into(), program: "build".into(), args: vec![] };
735731

@@ -753,7 +749,6 @@ mod tests {
753749
// (because last-match-wins means different semantics)
754750
assert_ne!(fingerprint1, fingerprint2);
755751

756-
use bincode::encode_to_vec;
757752
let config = bincode::config::standard();
758753

759754
let bytes1 = encode_to_vec(&fingerprint1, config).unwrap();

0 commit comments

Comments
 (0)