From 0bdd950f08b37b857d8a3466791ac1122eaa77d3 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 8 Dec 2024 14:57:57 -0800 Subject: [PATCH] Resolve dead code warnings on various platforms (#1423) --- .github/workflows/CI.yml | 3 +++ src/unix/apple/disk.rs | 5 +++++ src/unix/apple/macos/ffi.rs | 28 +++++++++++++++++----------- src/unix/apple/mod.rs | 2 +- src/unix/apple/system.rs | 2 +- src/unix/linux/utils.rs | 6 ++++++ src/windows/process.rs | 11 ----------- src/windows/sid.rs | 11 +++++++---- 8 files changed, 40 insertions(+), 28 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 105255347..0a839e352 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -8,6 +8,9 @@ concurrency: group: ${{ github.head_ref }} cancel-in-progress: true +env: + RUSTFLAGS: -Dwarnings + jobs: rustfmt: runs-on: ubuntu-latest diff --git a/src/unix/apple/disk.rs b/src/unix/apple/disk.rs index bedc1504e..c5b24b4f0 100644 --- a/src/unix/apple/disk.rs +++ b/src/unix/apple/disk.rs @@ -25,6 +25,7 @@ use std::ptr; pub(crate) struct DiskInner { pub(crate) type_: DiskKind, pub(crate) name: OsString, + #[cfg(target_os = "macos")] bsd_name: Option>, pub(crate) file_system: OsString, pub(crate) mount_point: PathBuf, @@ -338,6 +339,7 @@ unsafe fn get_list(container: &mut Vec, refresh_kind: DiskRefreshKind) { type RetainedCFArray = CFReleaser; pub(crate) type RetainedCFDictionary = CFReleaser; type RetainedCFURL = CFReleaser; +#[cfg(target_os = "macos")] pub(crate) type RetainedCFString = CFReleaser; unsafe fn build_requested_properties(properties: &[CFStringRef]) -> Option { @@ -527,6 +529,7 @@ unsafe fn new_disk( ) }; + #[cfg(target_os = "macos")] let bsd_name = get_bsd_name(&c_disk); // IOKit is not available on any but the most recent (16+) iOS and iPadOS versions. @@ -565,6 +568,7 @@ unsafe fn new_disk( let mut disk = DiskInner { type_: DiskKind::Unknown(-1), name, + #[cfg(target_os = "macos")] bsd_name, file_system, mount_point, @@ -619,6 +623,7 @@ unsafe fn with_autorelease T>(call: F) -> T { // Pool is drained here before returning } +#[cfg(target_os = "macos")] fn get_bsd_name(disk: &libc::statfs) -> Option> { // Removes `/dev/` from the value. unsafe { diff --git a/src/unix/apple/macos/ffi.rs b/src/unix/apple/macos/ffi.rs index 974dc11b6..662f7cf06 100644 --- a/src/unix/apple/macos/ffi.rs +++ b/src/unix/apple/macos/ffi.rs @@ -123,11 +123,14 @@ extern "C" { matching: CFMutableDictionaryRef, existing: *mut io_iterator_t, ) -> kern_return_t; - #[cfg(any( - feature = "system", - all( - feature = "component", - any(target_arch = "x86", target_arch = "x86_64") + #[cfg(all( + not(feature = "apple-sandbox"), + any( + feature = "system", + all( + feature = "component", + any(target_arch = "x86", target_arch = "x86_64") + ), ), ))] pub fn IOServiceMatching(a: *const c_char) -> CFMutableDictionaryRef; @@ -155,17 +158,20 @@ extern "C" { options: u32, bsdName: *const c_char, ) -> CFMutableDictionaryRef; - #[cfg(feature = "system")] + #[cfg(all(feature = "system", not(feature = "apple-sandbox")))] pub fn IORegistryEntryGetName(entry: io_registry_entry_t, name: io_name_t) -> kern_return_t; #[cfg(feature = "disk")] pub fn IOObjectConformsTo(object: io_object_t, className: *const c_char) -> libc::boolean_t; } -#[cfg(any( - feature = "system", - all( - feature = "component", - any(target_arch = "x86", target_arch = "x86_64") +#[cfg(all( + not(feature = "apple-sandbox"), + any( + feature = "system", + all( + feature = "component", + any(target_arch = "x86", target_arch = "x86_64") + ), ), ))] pub const KIO_RETURN_SUCCESS: i32 = 0; diff --git a/src/unix/apple/mod.rs b/src/unix/apple/mod.rs index 8f1c703d1..15a74f864 100644 --- a/src/unix/apple/mod.rs +++ b/src/unix/apple/mod.rs @@ -10,7 +10,7 @@ cfg_if! { if #[cfg(all(target_os = "macos", any(feature = "disk", feature = "system", feature = "component")))] { pub(crate) mod macos; pub(crate) use self::macos as inner; - } else if #[cfg(all(target_os = "ios", any(feature = "disk", feature = "system", feature = "component")))] { + } else if #[cfg(all(target_os = "ios", any(feature = "system", feature = "component")))] { pub(crate) mod ios; pub(crate) use self::ios as inner; } diff --git a/src/unix/apple/system.rs b/src/unix/apple/system.rs index 536c9f387..c4ca7549b 100644 --- a/src/unix/apple/system.rs +++ b/src/unix/apple/system.rs @@ -231,7 +231,7 @@ impl SystemInner { #[cfg(any(target_os = "ios", feature = "apple-sandbox"))] pub(crate) fn refresh_processes_specifics( &mut self, - processes_to_update: ProcessesToUpdate<'_>, + _processes_to_update: ProcessesToUpdate<'_>, _refresh_kind: ProcessRefreshKind, ) -> usize { 0 diff --git a/src/unix/linux/utils.rs b/src/unix/linux/utils.rs index 7b30e2513..678333372 100644 --- a/src/unix/linux/utils.rs +++ b/src/unix/linux/utils.rs @@ -1,7 +1,10 @@ // Take a look at the license at the top of the repository in the LICENSE file. +#[cfg(any(feature = "disk", feature = "system"))] use std::fs::File; +#[cfg(any(feature = "disk", feature = "system"))] use std::io::{self, Read, Seek}; +#[cfg(any(feature = "disk", feature = "system"))] use std::path::Path; #[cfg(feature = "system")] @@ -12,6 +15,7 @@ pub(crate) fn get_all_data_from_file(file: &mut File, size: usize) -> io::Result Ok(buf) } +#[cfg(any(feature = "disk", feature = "system"))] pub(crate) fn get_all_utf8_data_from_file(file: &mut File, size: usize) -> io::Result { let mut buf = String::with_capacity(size); file.rewind()?; @@ -19,6 +23,7 @@ pub(crate) fn get_all_utf8_data_from_file(file: &mut File, size: usize) -> io::R Ok(buf) } +#[cfg(any(feature = "disk", feature = "system"))] pub(crate) fn get_all_utf8_data>(file_path: P, size: usize) -> io::Result { let mut file = File::open(file_path.as_ref())?; get_all_utf8_data_from_file(&mut file, size) @@ -85,6 +90,7 @@ pub(crate) fn to_u64(v: &[u8]) -> u64 { } /// Converts a path to a NUL-terminated `Vec` suitable for use with C functions. +#[cfg(feature = "disk")] pub(crate) fn to_cpath(path: &std::path::Path) -> Vec { use std::{ffi::OsStr, os::unix::ffi::OsStrExt}; diff --git a/src/windows/process.rs b/src/windows/process.rs index 373d00e49..e037b47aa 100644 --- a/src/windows/process.rs +++ b/src/windows/process.rs @@ -225,17 +225,6 @@ fn windows_8_1_or_newer() -> &'static bool { }) } -#[cfg(feature = "debug")] -unsafe fn display_ntstatus_error(ntstatus: windows::core::HRESULT) { - let code = ntstatus.0; - let message = ntstatus.message(); - sysinfo_debug!( - "Couldn't get process infos: NtQuerySystemInformation returned {}: {}", - code, - message - ); -} - unsafe fn get_exe(process_handler: &HandleWrapper) -> Option { let mut exe_buf = [0u16; MAX_PATH as usize + 1]; GetModuleFileNameExW( diff --git a/src/windows/sid.rs b/src/windows/sid.rs index 5dc474ab6..f022c11fe 100644 --- a/src/windows/sid.rs +++ b/src/windows/sid.rs @@ -3,11 +3,13 @@ use std::{fmt::Display, str::FromStr}; use windows::core::{PCWSTR, PWSTR}; -use windows::Win32::Foundation::{LocalFree, ERROR_INSUFFICIENT_BUFFER, HLOCAL, PSID}; +#[cfg(feature = "user")] +use windows::Win32::Foundation::ERROR_INSUFFICIENT_BUFFER; +use windows::Win32::Foundation::{LocalFree, HLOCAL, PSID}; use windows::Win32::Security::Authorization::{ConvertSidToStringSidW, ConvertStringSidToSidW}; -use windows::Win32::Security::{ - CopySid, GetLengthSid, IsValidSid, LookupAccountSidW, SidTypeUnknown, -}; +use windows::Win32::Security::{CopySid, GetLengthSid, IsValidSid}; +#[cfg(feature = "user")] +use windows::Win32::Security::{LookupAccountSidW, SidTypeUnknown}; use crate::sys::utils::to_utf8_str; @@ -56,6 +58,7 @@ impl Sid { } /// Retrieves the account name of this SID. + #[cfg(feature = "user")] pub(crate) fn account_name(&self) -> Option { unsafe { let mut name_len = 0;