diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-06-27 03:52:05 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-06-27 04:21:34 +0900 |
| commit | 2b51554ff59d1e5cbf8dd2db65d278b0202a9102 (patch) | |
| tree | f4d9b0abf4df9b5e363e3bd65511d70e3d5ada00 /crates/shirabe-php-shim/src | |
| parent | cc07b5abb83a40d678401c335bdc49bb81b72c5f (diff) | |
| download | php-shirabe-2b51554ff59d1e5cbf8dd2db65d278b0202a9102.tar.gz php-shirabe-2b51554ff59d1e5cbf8dd2db65d278b0202a9102.tar.zst php-shirabe-2b51554ff59d1e5cbf8dd2db65d278b0202a9102.zip | |
refactor: fix compiler warnings and clippy warnings
Diffstat (limited to 'crates/shirabe-php-shim/src')
| -rw-r--r-- | crates/shirabe-php-shim/src/array.rs | 55 | ||||
| -rw-r--r-- | crates/shirabe-php-shim/src/env.rs | 10 | ||||
| -rw-r--r-- | crates/shirabe-php-shim/src/fs.rs | 43 | ||||
| -rw-r--r-- | crates/shirabe-php-shim/src/hash.rs | 1 | ||||
| -rw-r--r-- | crates/shirabe-php-shim/src/json.rs | 1 | ||||
| -rw-r--r-- | crates/shirabe-php-shim/src/lib.rs | 6 | ||||
| -rw-r--r-- | crates/shirabe-php-shim/src/phar.rs | 3 | ||||
| -rw-r--r-- | crates/shirabe-php-shim/src/preg.rs | 2 | ||||
| -rw-r--r-- | crates/shirabe-php-shim/src/process.rs | 15 | ||||
| -rw-r--r-- | crates/shirabe-php-shim/src/rar.rs | 3 | ||||
| -rw-r--r-- | crates/shirabe-php-shim/src/stream.rs | 12 | ||||
| -rw-r--r-- | crates/shirabe-php-shim/src/string.rs | 20 | ||||
| -rw-r--r-- | crates/shirabe-php-shim/src/xml.rs | 4 | ||||
| -rw-r--r-- | crates/shirabe-php-shim/src/zip.rs | 16 |
14 files changed, 100 insertions, 91 deletions
diff --git a/crates/shirabe-php-shim/src/array.rs b/crates/shirabe-php-shim/src/array.rs index ace6a72..6c2004c 100644 --- a/crates/shirabe-php-shim/src/array.rs +++ b/crates/shirabe-php-shim/src/array.rs @@ -86,12 +86,12 @@ pub fn array_merge(array1: PhpMixed, array2: PhpMixed) -> PhpMixed { } PhpMixed::Array(map) => { for (key, value) in map { - if let Ok(n) = key.parse::<i64>() { - if n.to_string() == key { - result.insert(next_int.to_string(), value); - next_int += 1; - continue; - } + if let Ok(n) = key.parse::<i64>() + && n.to_string() == key + { + result.insert(next_int.to_string(), value); + next_int += 1; + continue; } result.insert(key, value); } @@ -123,12 +123,12 @@ pub fn array_merge_map<V>( let mut next_int: i64 = 0; for array in [array1, array2] { for (key, value) in array { - if let Ok(n) = key.parse::<i64>() { - if n.to_string() == key { - result.insert(next_int.to_string(), value); - next_int += 1; - continue; - } + if let Ok(n) = key.parse::<i64>() + && n.to_string() == key + { + result.insert(next_int.to_string(), value); + next_int += 1; + continue; } result.insert(key, value); } @@ -437,10 +437,10 @@ fn merge_entries(value: PhpMixed) -> Vec<(MergeKey, PhpMixed)> { } fn merge_parse_key(key: String) -> MergeKey { - if let Ok(n) = key.parse::<i64>() { - if n.to_string() == key { - return MergeKey::Int(n); - } + if let Ok(n) = key.parse::<i64>() + && n.to_string() == key + { + return MergeKey::Int(n); } MergeKey::Str(key) } @@ -561,10 +561,10 @@ pub fn array_diff_key( /// Map a PHP array key (always stored as a `String` here) back to its PHP value /// type: an integer-like key becomes an int, anything else stays a string. fn php_key_to_mixed(key: &str) -> PhpMixed { - if let Ok(n) = key.parse::<i64>() { - if n.to_string() == key { - return PhpMixed::Int(n); - } + if let Ok(n) = key.parse::<i64>() + && n.to_string() == key + { + return PhpMixed::Int(n); } PhpMixed::String(key.to_string()) } @@ -664,7 +664,7 @@ pub fn krsort<V>(array: &mut IndexMap<i64, V>) { array.sort_by(|k1, _, k2, _| k2.cmp(k1)); } -pub fn uasort<T, F>(array: &mut Vec<T>, compare: F) +pub fn uasort<T, F>(array: &mut [T], compare: F) where F: FnMut(&T, &T) -> i64, { @@ -684,7 +684,7 @@ pub fn sort<T: Ord>(_array: &mut Vec<T>) { _array.sort(); } -pub fn sort_with_flags<T: Ord>(array: &mut Vec<T>, flags: i64) { +pub fn sort_with_flags<T: Ord>(array: &mut [T], flags: i64) { if flags != SORT_REGULAR { // TODO(phase-d): flag-specific comparison (SORT_NUMERIC/SORT_STRING/ // SORT_NATURAL/SORT_FLAG_CASE) cannot be expressed for a generic @@ -717,10 +717,11 @@ pub fn ksort<V>(array: &mut IndexMap<String, V>) { // TODO(phase-d): full SORT_REGULAR semantics for mixed integer/non-numeric-string // keys are not reproduced; every current caller uses homogeneous string keys. fn php_sort_regular_key(a: &str, b: &str) -> std::cmp::Ordering { - if let (Ok(na), Ok(nb)) = (a.parse::<i64>(), b.parse::<i64>()) { - if na.to_string() == a && nb.to_string() == b { - return na.cmp(&nb); - } + if let (Ok(na), Ok(nb)) = (a.parse::<i64>(), b.parse::<i64>()) + && na.to_string() == a + && nb.to_string() == b + { + return na.cmp(&nb); } a.cmp(b) } @@ -737,7 +738,7 @@ where array.sort_by(|k1, _, k2, _| callback(k1, k2).cmp(&0)); } -pub fn sort_natural_flag_case(values: &mut Vec<String>) { +pub fn sort_natural_flag_case(values: &mut [String]) { values.sort_by(|a, b| crate::strnatcasecmp(a, b).cmp(&0)); } diff --git a/crates/shirabe-php-shim/src/env.rs b/crates/shirabe-php-shim/src/env.rs index cf0c659..9043167 100644 --- a/crates/shirabe-php-shim/src/env.rs +++ b/crates/shirabe-php-shim/src/env.rs @@ -8,11 +8,21 @@ pub fn getenv<K: AsRef<std::ffi::OsStr>>(key: K) -> Option<std::ffi::OsString> { std::env::var_os(key) } +/// # Safety +/// +/// Wraps [`std::env::set_var`], which is unsafe: the caller must ensure no other +/// thread is concurrently reading or writing the process environment for the +/// duration of this call. pub unsafe fn putenv<K: AsRef<std::ffi::OsStr>, V: AsRef<std::ffi::OsStr>>(key: K, value: V) { // TODO: validate key and value format to avoid panic? unsafe { std::env::set_var(key, value) } } +/// # Safety +/// +/// Wraps [`std::env::remove_var`], which is unsafe: the caller must ensure no other +/// thread is concurrently reading or writing the process environment for the +/// duration of this call. pub unsafe fn putenv_clear<K: AsRef<std::ffi::OsStr>>(key: K) { // TODO: validate key and value format to avoid panic? unsafe { std::env::remove_var(key) } diff --git a/crates/shirabe-php-shim/src/fs.rs b/crates/shirabe-php-shim/src/fs.rs index a557d26..bd882c2 100644 --- a/crates/shirabe-php-shim/src/fs.rs +++ b/crates/shirabe-php-shim/src/fs.rs @@ -259,13 +259,15 @@ pub fn fopen(file: &str, mode: &str) -> Result<PhpResource, std::io::Error> { // (this is what Symfony's ApplicationTester relies on -- it opens "php://memory" with "w" and // then rewinds + reads it back). So memory streams are always both readable and writable. if file == "php://memory" || file.starts_with("php://temp") { - return Ok(StreamState::new( - StreamBacking::Memory(std::io::Cursor::new(Vec::new())), - true, - true, - mode.to_string(), - file.to_string(), - )); + return Ok(PhpResource::Stream(std::rc::Rc::new( + std::cell::RefCell::new(StreamState::new( + StreamBacking::Memory(std::io::Cursor::new(Vec::new())), + true, + true, + mode.to_string(), + file.to_string(), + )), + ))); } let uri = file.to_string(); let mut options = std::fs::OpenOptions::new(); @@ -284,13 +286,15 @@ pub fn fopen(file: &str, mode: &str) -> Result<PhpResource, std::io::Error> { _ => options.read(true), }; let file = options.open(file)?; - Ok(StreamState::new( - StreamBacking::File(file), - readable, - writable, - mode.to_string(), - uri, - )) + Ok(PhpResource::Stream(std::rc::Rc::new( + std::cell::RefCell::new(StreamState::new( + StreamBacking::File(file), + readable, + writable, + mode.to_string(), + uri, + )), + ))) } /// PHP `fwrite()`. `length` caps the number of bytes written (`None` = whole string). @@ -380,7 +384,6 @@ pub fn fclose(stream: &PhpResource) -> bool { if state.closed { return false; } - use std::io::Write; let _ = state.backing.as_rws().flush(); state.closed = true; true @@ -430,10 +433,10 @@ fn fgets_read_line<R: std::io::Read + ?Sized>( let mut line = Vec::new(); let mut byte = [0u8; 1]; loop { - if let Some(max) = limit { - if line.len() >= max { - break; - } + if let Some(max) = limit + && line.len() >= max + { + break; } let n = r.read(&mut byte)?; if n == 0 { @@ -478,7 +481,6 @@ pub fn fgetc(stream: &PhpResource) -> Option<String> { /// PHP `ftell()`: the current position, or `None` for `false`-on-failure. pub fn ftell(stream: &PhpResource) -> Option<i64> { - use std::io::Seek; match stream { PhpResource::Stdin | PhpResource::Stdout @@ -501,7 +503,6 @@ pub fn ftell(stream: &PhpResource) -> Option<i64> { /// PHP `fseek()`. Returns 0 on success, -1 on failure. pub fn fseek(stream: &PhpResource, offset: i64, whence: i64) -> i64 { - use std::io::Seek; let from = match whence { SEEK_CUR => std::io::SeekFrom::Current(offset), SEEK_END => std::io::SeekFrom::End(offset), diff --git a/crates/shirabe-php-shim/src/hash.rs b/crates/shirabe-php-shim/src/hash.rs index 6e12a81..d77cc0a 100644 --- a/crates/shirabe-php-shim/src/hash.rs +++ b/crates/shirabe-php-shim/src/hash.rs @@ -1,6 +1,5 @@ use crate::bin2hex; use sha1::Digest as _; -use sha2::Digest as _; pub fn hash(algo: &str, data: &str) -> String { crate::bin2hex(&calculate_hash(algo, data.as_bytes())) diff --git a/crates/shirabe-php-shim/src/json.rs b/crates/shirabe-php-shim/src/json.rs index e2c48e4..1fa86ca 100644 --- a/crates/shirabe-php-shim/src/json.rs +++ b/crates/shirabe-php-shim/src/json.rs @@ -33,7 +33,6 @@ pub fn json_encode_ex<T: serde::Serialize + ?Sized>( // them when a call site needs them. let mut s = if flags & JSON_PRETTY_PRINT != 0 { // PHP's JSON_PRETTY_PRINT uses a 4-space indent. - use serde::Serialize; let mut buf = Vec::new(); let formatter = serde_json::ser::PrettyFormatter::with_indent(b" "); let mut ser = serde_json::Serializer::with_formatter(&mut buf, formatter); diff --git a/crates/shirabe-php-shim/src/lib.rs b/crates/shirabe-php-shim/src/lib.rs index 8c8073f..cc868ff 100644 --- a/crates/shirabe-php-shim/src/lib.rs +++ b/crates/shirabe-php-shim/src/lib.rs @@ -480,8 +480,8 @@ impl StreamState { writable: bool, mode: String, uri: String, - ) -> PhpResource { - PhpResource::Stream(std::rc::Rc::new(std::cell::RefCell::new(StreamState { + ) -> StreamState { + StreamState { backing, readable, writable, @@ -489,6 +489,6 @@ impl StreamState { closed: false, mode, uri, - }))) + } } } diff --git a/crates/shirabe-php-shim/src/phar.rs b/crates/shirabe-php-shim/src/phar.rs index 8217808..692d593 100644 --- a/crates/shirabe-php-shim/src/phar.rs +++ b/crates/shirabe-php-shim/src/phar.rs @@ -1,6 +1,3 @@ -use crate::PhpMixed; -use indexmap::IndexMap; - #[derive(Debug)] pub struct Phar { path: String, diff --git a/crates/shirabe-php-shim/src/preg.rs b/crates/shirabe-php-shim/src/preg.rs index e5bd52e..4b43467 100644 --- a/crates/shirabe-php-shim/src/preg.rs +++ b/crates/shirabe-php-shim/src/preg.rs @@ -366,7 +366,7 @@ pub fn preg_split2(pattern: &str, subject: &str, limit: i64, flags: i64) -> Vec< }; let mut result: Vec<String> = Vec::new(); - let mut push = |s: &str, result: &mut Vec<String>| { + let push = |s: &str, result: &mut Vec<String>| { if !(no_empty && s.is_empty()) { result.push(s.to_string()); } diff --git a/crates/shirabe-php-shim/src/process.rs b/crates/shirabe-php-shim/src/process.rs index 6999d99..ad6e1d5 100644 --- a/crates/shirabe-php-shim/src/process.rs +++ b/crates/shirabe-php-shim/src/process.rs @@ -253,13 +253,14 @@ pub fn proc_open( 2 => (ChildPipe::Err(child.stderr.take().unwrap()), true, false), _ => unreachable!(), }; - let resource = StreamState::new( - StreamBacking::Pipe(pipe), - readable, - writable, - mode, - format!("pipe:fd{}", fd), - ); + let resource = + PhpResource::Stream(std::rc::Rc::new(std::cell::RefCell::new(StreamState::new( + StreamBacking::Pipe(pipe), + readable, + writable, + mode, + format!("pipe:fd{}", fd), + )))); pipes.insert(fd, resource); } diff --git a/crates/shirabe-php-shim/src/rar.rs b/crates/shirabe-php-shim/src/rar.rs index 8efce90..10c7e27 100644 --- a/crates/shirabe-php-shim/src/rar.rs +++ b/crates/shirabe-php-shim/src/rar.rs @@ -1,6 +1,3 @@ -use crate::PhpMixed; -use indexmap::IndexMap; - #[derive(Debug)] pub struct RarEntry; diff --git a/crates/shirabe-php-shim/src/stream.rs b/crates/shirabe-php-shim/src/stream.rs index 606b202..98f0322 100644 --- a/crates/shirabe-php-shim/src/stream.rs +++ b/crates/shirabe-php-shim/src/stream.rs @@ -346,12 +346,12 @@ pub fn stream_select( // emit a warning for them. We skip them here, leaving them out of the ready set. let mut prepare = |set: &mut FdSet, resources: &[PhpResource]| { for resource in resources { - if let Some(fd) = resource.raw_fd() { - if (fd as usize) < FD_SETSIZE { - set.set(fd); - if fd + 1 > nfds { - nfds = fd + 1; - } + if let Some(fd) = resource.raw_fd() + && (fd as usize) < FD_SETSIZE + { + set.set(fd); + if fd + 1 > nfds { + nfds = fd + 1; } } } diff --git a/crates/shirabe-php-shim/src/string.rs b/crates/shirabe-php-shim/src/string.rs index 9a82fe8..97c6695 100644 --- a/crates/shirabe-php-shim/src/string.rs +++ b/crates/shirabe-php-shim/src/string.rs @@ -333,10 +333,8 @@ fn natcmp_compare_right(a: &[u8], ap: &mut usize, b: &[u8], bp: &mut usize) -> i if bias == 0 { bias = -1; } - } else if ca > cb { - if bias == 0 { - bias = 1; - } + } else if ca > cb && bias == 0 { + bias = 1; } *ap += 1; *bp += 1; @@ -613,14 +611,14 @@ pub fn rawurldecode(s: &str) -> String { let mut out: Vec<u8> = Vec::with_capacity(bytes.len()); let mut i = 0; while i < bytes.len() { - if bytes[i] == b'%' && i + 2 < bytes.len() { - if let (Some(h), Some(l)) = + if bytes[i] == b'%' + && i + 2 < bytes.len() + && let (Some(h), Some(l)) = (hex_digit_value(bytes[i + 1]), hex_digit_value(bytes[i + 2])) - { - out.push((h << 4) | l); - i += 3; - continue; - } + { + out.push((h << 4) | l); + i += 3; + continue; } out.push(bytes[i]); i += 1; diff --git a/crates/shirabe-php-shim/src/xml.rs b/crates/shirabe-php-shim/src/xml.rs index 1a74edc..cc323ca 100644 --- a/crates/shirabe-php-shim/src/xml.rs +++ b/crates/shirabe-php-shim/src/xml.rs @@ -187,6 +187,10 @@ impl DOMNodeList { self.0.len() } + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } + pub fn iter(&self) -> std::slice::Iter<'_, DOMNode> { self.0.iter() } diff --git a/crates/shirabe-php-shim/src/zip.rs b/crates/shirabe-php-shim/src/zip.rs index d1db9e5..8bb51d1 100644 --- a/crates/shirabe-php-shim/src/zip.rs +++ b/crates/shirabe-php-shim/src/zip.rs @@ -210,13 +210,15 @@ impl ZipArchive { let mut file = archive.by_name(name).ok()?; let mut buf = Vec::new(); std::io::Read::read_to_end(&mut file, &mut buf).ok()?; - Some(StreamState::new( - StreamBacking::Memory(std::io::Cursor::new(buf)), - true, - false, - "r".to_string(), - format!("zip://{}", name), - )) + Some(crate::PhpResource::Stream(std::rc::Rc::new( + std::cell::RefCell::new(StreamState::new( + StreamBacking::Memory(std::io::Cursor::new(buf)), + true, + false, + "r".to_string(), + format!("zip://{}", name), + )), + ))) } pub fn add_empty_dir(&self, local_name: &str) -> bool { |
