diff --git a/crates/libs/bindgen/src/types/cpp_const.rs b/crates/libs/bindgen/src/types/cpp_const.rs index 1e706f6341..1356ba2630 100644 --- a/crates/libs/bindgen/src/types/cpp_const.rs +++ b/crates/libs/bindgen/src/types/cpp_const.rs @@ -109,7 +109,7 @@ impl CppConst { } } else if let Some(attribute) = self.field.find_attribute("ConstantAttribute") { let args = attribute.args(); - let Some((_, Value::Str(mut input))) = args.first() else { + let Some(&(_, Value::Str(mut input))) = args.first() else { panic!() }; diff --git a/crates/libs/implement/src/lib.rs b/crates/libs/implement/src/lib.rs index d82bf0e59e..c50509af72 100644 --- a/crates/libs/implement/src/lib.rs +++ b/crates/libs/implement/src/lib.rs @@ -6,7 +6,8 @@ use quote::{quote, ToTokens}; -mod gen; +mod r#gen; +use r#gen::gen_all; #[cfg(test)] mod tests; @@ -75,7 +76,7 @@ fn implement_core( original_type, }; - let items = gen::gen_all(&inputs); + let items = gen_all(&inputs); let mut tokens = inputs.original_type.into_token_stream(); for item in items { tokens.extend(item.into_token_stream()); diff --git a/crates/libs/strings/src/literals.rs b/crates/libs/strings/src/literals.rs index c5c5d86c97..cfdabc6a7a 100644 --- a/crates/libs/strings/src/literals.rs +++ b/crates/libs/strings/src/literals.rs @@ -41,8 +41,7 @@ macro_rules! h { ($s:literal) => {{ const INPUT: &[u8] = $s.as_bytes(); const OUTPUT_LEN: usize = $crate::utf16_len(INPUT) + 1; - #[allow(clippy::declare_interior_mutable_const)] - const RESULT: $crate::HSTRING = { + static RESULT: $crate::HSTRING = { if OUTPUT_LEN == 1 { unsafe { ::core::mem::transmute(::core::ptr::null::()) } } else { @@ -60,7 +59,6 @@ macro_rules! h { } } }; - #[allow(clippy::borrow_interior_mutable_const)] &RESULT }}; } diff --git a/crates/samples/components/json_validator/src/lib.rs b/crates/samples/components/json_validator/src/lib.rs index a679531ce9..5e45e64079 100644 --- a/crates/samples/components/json_validator/src/lib.rs +++ b/crates/samples/components/json_validator/src/lib.rs @@ -9,7 +9,7 @@ unsafe extern "system" fn CreateJsonValidator( schema_len: usize, handle: *mut usize, ) -> HRESULT { - create_validator(schema, schema_len, handle).into() + unsafe { create_validator(schema, schema_len, handle).into() } } // Validates a JSON value against a previously-compiled schema. @@ -21,39 +21,45 @@ unsafe extern "system" fn ValidateJson( sanitized_value: *mut *mut u8, sanitized_value_len: *mut usize, ) -> HRESULT { - validate( - handle, - value, - value_len, - sanitized_value, - sanitized_value_len, - ) - .into() + unsafe { + validate( + handle, + value, + value_len, + sanitized_value, + sanitized_value_len, + ) + .into() + } } // Closes a JSON validator object. #[no_mangle] unsafe extern "system" fn CloseJsonValidator(handle: usize) { - if handle != 0 { - _ = Box::from_raw(handle as *mut Validator); + unsafe { + if handle != 0 { + _ = Box::from_raw(handle as *mut Validator); + } } } // Implementation of the `CreateJsonValidator` function so we can use `Result` for simplicity. unsafe fn create_validator(schema: *const u8, schema_len: usize, handle: *mut usize) -> Result<()> { - let schema = json_from_raw_parts(schema, schema_len)?; + unsafe { + let schema = json_from_raw_parts(schema, schema_len)?; - let compiled = - Validator::new(&schema).map_err(|error| Error::new(E_INVALIDARG, error.to_string()))?; + let compiled = + Validator::new(&schema).map_err(|error| Error::new(E_INVALIDARG, error.to_string()))?; - if handle.is_null() { - return Err(E_POINTER.into()); - } + if handle.is_null() { + return Err(E_POINTER.into()); + } - // The handle is not null so we can safely dereference it here. - *handle = Box::into_raw(Box::new(compiled)) as usize; + // The handle is not null so we can safely dereference it here. + *handle = Box::into_raw(Box::new(compiled)) as usize; - Ok(()) + Ok(()) + } } // Implementation of the `ValidateJson` function so we can use `Result` for simplicity. @@ -64,56 +70,60 @@ unsafe fn validate( sanitized_value: *mut *mut u8, sanitized_value_len: *mut usize, ) -> Result<()> { - if handle == 0 { - return Err(E_HANDLE.into()); - } + unsafe { + if handle == 0 { + return Err(E_HANDLE.into()); + } + + let value = json_from_raw_parts(value, value_len)?; - let value = json_from_raw_parts(value, value_len)?; + // This looks a bit tricky but we're just turning the opaque handle into `Validator` pointer + // and then returning a reference to avoid taking ownership of it. + let schema = &*(handle as *const Validator); - // This looks a bit tricky but we're just turning the opaque handle into `Validator` pointer - // and then returning a reference to avoid taking ownership of it. - let schema = &*(handle as *const Validator); + if schema.is_valid(&value) { + if !sanitized_value.is_null() && !sanitized_value_len.is_null() { + let value = value.to_string(); - if schema.is_valid(&value) { - if !sanitized_value.is_null() && !sanitized_value_len.is_null() { - let value = value.to_string(); + *sanitized_value = CoTaskMemAlloc(value.len()) as _; - *sanitized_value = CoTaskMemAlloc(value.len()) as _; + if (*sanitized_value).is_null() { + return Err(E_OUTOFMEMORY.into()); + } - if (*sanitized_value).is_null() { - return Err(E_OUTOFMEMORY.into()); + (*sanitized_value).copy_from(value.as_ptr(), value.len()); + *sanitized_value_len = value.len(); } - (*sanitized_value).copy_from(value.as_ptr(), value.len()); - *sanitized_value_len = value.len(); - } + Ok(()) + } else { + let mut message = String::new(); - Ok(()) - } else { - let mut message = String::new(); + // The `validate` method returns a collection of errors. We'll just return the first + // for simplicity. + if let Some(error) = schema.validate(&value).unwrap_err().next() { + message = error.to_string(); + } - // The `validate` method returns a collection of errors. We'll just return the first - // for simplicity. - if let Some(error) = schema.validate(&value).unwrap_err().next() { - message = error.to_string(); + Err(Error::new(E_INVALIDARG, message)) } - - Err(Error::new(E_INVALIDARG, message)) } } // Takes care of all the JSON parsing and parameter validation. unsafe fn json_from_raw_parts(value: *const u8, value_len: usize) -> Result { - if value.is_null() { - return Err(E_POINTER.into()); - } + unsafe { + if value.is_null() { + return Err(E_POINTER.into()); + } - let value = std::slice::from_raw_parts(value, value_len); + let value = std::slice::from_raw_parts(value, value_len); - let value = - std::str::from_utf8(value).map_err(|_| Error::from(ERROR_NO_UNICODE_TRANSLATION))?; + let value = + std::str::from_utf8(value).map_err(|_| Error::from(ERROR_NO_UNICODE_TRANSLATION))?; - serde_json::from_str(value).map_err(|error| Error::new(E_INVALIDARG, format!("{error}"))) + serde_json::from_str(value).map_err(|error| Error::new(E_INVALIDARG, format!("{error}"))) + } } #[test] diff --git a/crates/samples/components/json_validator_winrt_client_cpp/src/lib.rs b/crates/samples/components/json_validator_winrt_client_cpp/src/lib.rs index 6f1497b02a..84773e8ad5 100644 --- a/crates/samples/components/json_validator_winrt_client_cpp/src/lib.rs +++ b/crates/samples/components/json_validator_winrt_client_cpp/src/lib.rs @@ -2,7 +2,7 @@ #[test] fn test() { - extern "system" { + unsafe extern "system" { fn client(); } unsafe { diff --git a/crates/samples/windows-sys/delay_load/src/main.rs b/crates/samples/windows-sys/delay_load/src/main.rs index 6f6691e3fd..c771deaf30 100644 --- a/crates/samples/windows-sys/delay_load/src/main.rs +++ b/crates/samples/windows-sys/delay_load/src/main.rs @@ -4,24 +4,26 @@ use windows_sys::{core::*, Win32::Foundation::*, Win32::System::LibraryLoader::* /// /// The `PCSTR` parameters need to be valid for reads up until and including the next `\0`. pub unsafe fn delay_load(library: PCSTR, function: PCSTR) -> Option { - let library = LoadLibraryExA( - library, - core::ptr::null_mut(), - LOAD_LIBRARY_SEARCH_DEFAULT_DIRS, - ); + unsafe { + let library = LoadLibraryExA( + library, + core::ptr::null_mut(), + LOAD_LIBRARY_SEARCH_DEFAULT_DIRS, + ); - if library.is_null() { - return None; - } + if library.is_null() { + return None; + } - let address = GetProcAddress(library, function); + let address = GetProcAddress(library, function); - if address.is_some() { - return Some(std::mem::transmute_copy(&address)); - } + if address.is_some() { + return Some(std::mem::transmute_copy(&address)); + } - FreeLibrary(library); - None + FreeLibrary(library); + None + } } fn main() { diff --git a/crates/samples/windows/data_protection/src/main.rs b/crates/samples/windows/data_protection/src/main.rs index aa95ac4e4d..85c0ce78a4 100644 --- a/crates/samples/windows/data_protection/src/main.rs +++ b/crates/samples/windows/data_protection/src/main.rs @@ -24,9 +24,12 @@ fn main() -> std::io::Result<()> { unsafe fn as_mut_bytes(buffer: &IBuffer) -> Result<&mut [u8]> { let interop = buffer.cast::()?; - let data = interop.Buffer()?; - Ok(std::slice::from_raw_parts_mut( - data, - buffer.Length()? as usize, - )) + + unsafe { + let data = interop.Buffer()?; + Ok(std::slice::from_raw_parts_mut( + data, + buffer.Length()? as usize, + )) + } } diff --git a/crates/samples/windows/delay_load/src/main.rs b/crates/samples/windows/delay_load/src/main.rs index 5a8c9aeb26..42e0c69746 100644 --- a/crates/samples/windows/delay_load/src/main.rs +++ b/crates/samples/windows/delay_load/src/main.rs @@ -4,20 +4,22 @@ use windows::{core::*, Win32::Foundation::*, Win32::System::LibraryLoader::*}; /// /// The `PCSTR` parameters need to be valid for reads up until and including the next `\0`. pub unsafe fn delay_load(library: PCSTR, function: PCSTR) -> Option { - let library = LoadLibraryExA(library, None, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS); + unsafe { + let library = LoadLibraryExA(library, None, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS); - let Ok(library) = library else { - return None; - }; + let Ok(library) = library else { + return None; + }; - let address = GetProcAddress(library, function); + let address = GetProcAddress(library, function); - if address.is_some() { - return Some(std::mem::transmute_copy(&address)); - } + if address.is_some() { + return Some(std::mem::transmute_copy(&address)); + } - _ = FreeLibrary(library); - None + _ = FreeLibrary(library); + None + } } fn main() { diff --git a/crates/samples/windows/memory_buffer/src/main.rs b/crates/samples/windows/memory_buffer/src/main.rs index 025e9a931a..dc86398a60 100644 --- a/crates/samples/windows/memory_buffer/src/main.rs +++ b/crates/samples/windows/memory_buffer/src/main.rs @@ -10,8 +10,10 @@ unsafe fn as_mut_slice(buffer: &IMemoryBufferReference) -> Result<&mut [u8]> { let mut data = std::ptr::null_mut(); let mut len = 0; - interop.GetBuffer(&mut data, &mut len)?; - Ok(std::slice::from_raw_parts_mut(data, len as usize)) + unsafe { + interop.GetBuffer(&mut data, &mut len)?; + Ok(std::slice::from_raw_parts_mut(data, len as usize)) + } } fn main() -> Result<()> { diff --git a/crates/tests/libs/core/tests/unknown.rs b/crates/tests/libs/core/tests/unknown.rs index d43ba107b6..ac3a03d59a 100644 --- a/crates/tests/libs/core/tests/unknown.rs +++ b/crates/tests/libs/core/tests/unknown.rs @@ -14,7 +14,7 @@ struct Test { impl ITest_Impl for Test_Impl { unsafe fn Test(&self) -> u32 { - *self.drop + unsafe { *self.drop } } } diff --git a/crates/tests/libs/interface/tests/com.rs b/crates/tests/libs/interface/tests/com.rs index 323b7447c2..78da00c37e 100644 --- a/crates/tests/libs/interface/tests/com.rs +++ b/crates/tests/libs/interface/tests/com.rs @@ -61,8 +61,10 @@ struct PersistState { impl ICustomPersist_Impl for Persist_Impl { unsafe fn GetClassID(&self, clsid: *mut GUID) -> HRESULT { - *clsid = "117fb826-2155-483a-b50d-bc99a2c7cca3".try_into().unwrap(); - S_OK + unsafe { + *clsid = "117fb826-2155-483a-b50d-bc99a2c7cca3".try_into().unwrap(); + S_OK + } } } @@ -79,7 +81,9 @@ impl ICustomPersistMemory_Impl for Persist_Impl { unsafe fn Load(&self, input: *const core::ffi::c_void, size: u32) -> HRESULT { let mut writer = self.0.write().unwrap(); if size <= writer.memory.len() as u32 { - std::ptr::copy(input, writer.memory.as_mut_ptr() as _, size as usize); + unsafe { + std::ptr::copy(input, writer.memory.as_mut_ptr() as _, size as usize); + } writer.dirty = true; S_OK } else { @@ -90,7 +94,9 @@ impl ICustomPersistMemory_Impl for Persist_Impl { unsafe fn Save(&self, output: *mut core::ffi::c_void, clear_dirty: BOOL, size: u32) -> HRESULT { let mut writer = self.0.write().unwrap(); if size <= writer.memory.len() as u32 { - std::ptr::copy(writer.memory.as_mut_ptr() as _, output, size as usize); + unsafe { + std::ptr::copy(writer.memory.as_mut_ptr() as _, output, size as usize); + } if clear_dirty.as_bool() { writer.dirty = false; } @@ -102,7 +108,9 @@ impl ICustomPersistMemory_Impl for Persist_Impl { unsafe fn GetSizeMax(&self, len: *mut u32) -> HRESULT { let reader = self.0.read().unwrap(); - *len = reader.memory.len() as u32; + unsafe { + *len = reader.memory.len() as u32; + } S_OK } diff --git a/crates/tests/libs/interface/tests/non_com_new.rs b/crates/tests/libs/interface/tests/non_com_new.rs index 74efe892ae..826306f48a 100644 --- a/crates/tests/libs/interface/tests/non_com_new.rs +++ b/crates/tests/libs/interface/tests/non_com_new.rs @@ -17,7 +17,7 @@ impl IBase_Impl for Base { } unsafe fn base_value(test: &IBase) -> i32 { - test.BaseValue() + unsafe { test.BaseValue() } } #[test] diff --git a/crates/tests/libs/interface_core/tests/ref.rs b/crates/tests/libs/interface_core/tests/ref.rs index 65942b8df7..e146b43b8a 100644 --- a/crates/tests/libs/interface_core/tests/ref.rs +++ b/crates/tests/libs/interface_core/tests/ref.rs @@ -40,7 +40,7 @@ impl ITest_Impl for Test_Impl { if input.is_none() { E_INVALIDARG } else { - self.interface(input, output) + unsafe { self.interface(input, output) } } } @@ -48,7 +48,7 @@ impl ITest_Impl for Test_Impl { if output.is_null() { S_FALSE } else { - self.interface(input, output) + unsafe { self.interface(input, output) } } } @@ -65,7 +65,7 @@ impl ITest_Impl for Test_Impl { if input.is_none() { E_INVALIDARG.ok() } else { - self.result_interface(input, output) + unsafe { self.result_interface(input, output) } } } } diff --git a/crates/tests/libs/interface_core/tests/ref_ok.rs b/crates/tests/libs/interface_core/tests/ref_ok.rs index 286accf382..45935d62e7 100644 --- a/crates/tests/libs/interface_core/tests/ref_ok.rs +++ b/crates/tests/libs/interface_core/tests/ref_ok.rs @@ -17,7 +17,7 @@ impl ITest_Impl for Test_Impl { Ok(()) } unsafe fn TestOther(&self, other: Ref, result: &mut i32) -> Result<()> { - other.ok()?.Test(result) + unsafe { other.ok()?.Test(result) } } } diff --git a/crates/tests/libs/strings/tests/literals.rs b/crates/tests/libs/strings/tests/literals.rs index 4d61c32615..d9a6e853f3 100644 --- a/crates/tests/libs/strings/tests/literals.rs +++ b/crates/tests/libs/strings/tests/literals.rs @@ -9,8 +9,19 @@ fn literals() -> Result<()> { const W: PCWSTR = w!("wide"); assert_eq!(unsafe { W.to_string()? }, "wide"); - const H: &HSTRING = h!("hstring"); - assert_eq!(H, "hstring"); + let h: &'static HSTRING = h!("hstring"); + assert_eq!(h, "hstring"); Ok(()) } + +#[test] +fn temporary() { + expect_pcstr(s!("ansi")); + expect_pcwstr(w!("wide")); + expect_hstring(h!("hstring")); +} + +fn expect_hstring(_: &'static HSTRING) {} +fn expect_pcwstr(_: PCWSTR) {} +fn expect_pcstr(_: PCSTR) {} diff --git a/crates/tests/misc/const_params/tests/sys.rs b/crates/tests/misc/const_params/tests/sys.rs index 427dd6f452..32571153d9 100644 --- a/crates/tests/misc/const_params/tests/sys.rs +++ b/crates/tests/misc/const_params/tests/sys.rs @@ -5,7 +5,7 @@ extern "C" { } unsafe fn to_string(s: PCWSTR) -> String { - String::from_utf16_lossy(std::slice::from_raw_parts(s, wcslen(s))) + unsafe { String::from_utf16_lossy(std::slice::from_raw_parts(s, wcslen(s))) } } #[test]