From 320b704194d0c144e7036b03aad169b29ce56e80 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 15 Dec 2025 19:32:19 -0800 Subject: [PATCH] Remove unnecessary 'unsafe' modifiers from classes and methods Removed redundant 'unsafe' modifiers from several classes and methods across ABI, Bindables, Collections, and InteropServices namespaces. This change improves code clarity and safety by limiting the use of 'unsafe' to where it is strictly required. --- .../ABI/System/Collections/IEnumerable.cs | 2 +- .../ABI/System/Collections/IEnumerator.cs | 2 +- src/WinRT.Runtime2/ABI/System/Collections/IList.cs | 2 +- .../Specialized/INotifyCollectionChanged.cs | 2 +- .../System/ComponentModel/INotifyPropertyChanged.cs | 2 +- .../ABI/Windows.Foundation/IAsyncAction.cs | 2 +- src/WinRT.Runtime2/Bindables/WindowsRuntimeIterator.cs | 2 +- .../Collections/WindowsRuntimeEnumerator{T}.cs | 2 +- .../Activation/WindowsRuntimeActivationHelper.cs | 10 +++++----- .../ObjectReference/FreeThreadedObjectReference.cs | 2 +- .../InteropServices/Platform/WindowsRuntimeImports.cs | 2 +- .../ProjectionImpls/IAgileObjectImpl.cs | 2 +- 12 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/WinRT.Runtime2/ABI/System/Collections/IEnumerable.cs b/src/WinRT.Runtime2/ABI/System/Collections/IEnumerable.cs index 9453e84e5..570c500fa 100644 --- a/src/WinRT.Runtime2/ABI/System/Collections/IEnumerable.cs +++ b/src/WinRT.Runtime2/ABI/System/Collections/IEnumerable.cs @@ -111,7 +111,7 @@ public override object CreateObject(void* value, out CreatedWrapperFlags wrapper DiagnosticId = WindowsRuntimeConstants.PrivateImplementationDetailObsoleteDiagnosticId, UrlFormat = WindowsRuntimeConstants.CsWinRTDiagnosticsUrlFormat)] [EditorBrowsable(EditorBrowsableState.Never)] -public static unsafe class IEnumerableMethods +public static class IEnumerableMethods { /// public static global::System.Collections.IEnumerator GetEnumerator(WindowsRuntimeObjectReference thisReference) diff --git a/src/WinRT.Runtime2/ABI/System/Collections/IEnumerator.cs b/src/WinRT.Runtime2/ABI/System/Collections/IEnumerator.cs index cde1bf29a..d7345b566 100644 --- a/src/WinRT.Runtime2/ABI/System/Collections/IEnumerator.cs +++ b/src/WinRT.Runtime2/ABI/System/Collections/IEnumerator.cs @@ -111,7 +111,7 @@ public override object CreateObject(void* value, out CreatedWrapperFlags wrapper DiagnosticId = WindowsRuntimeConstants.PrivateImplementationDetailObsoleteDiagnosticId, UrlFormat = WindowsRuntimeConstants.CsWinRTDiagnosticsUrlFormat)] [EditorBrowsable(EditorBrowsableState.Never)] -public static unsafe class IEnumeratorMethods +public static class IEnumeratorMethods { /// public static object? Current(WindowsRuntimeObjectReference thisReference) diff --git a/src/WinRT.Runtime2/ABI/System/Collections/IList.cs b/src/WinRT.Runtime2/ABI/System/Collections/IList.cs index aaad09b50..cc42530a3 100644 --- a/src/WinRT.Runtime2/ABI/System/Collections/IList.cs +++ b/src/WinRT.Runtime2/ABI/System/Collections/IList.cs @@ -111,7 +111,7 @@ public override object CreateObject(void* value, out CreatedWrapperFlags wrapper DiagnosticId = WindowsRuntimeConstants.PrivateImplementationDetailObsoleteDiagnosticId, UrlFormat = WindowsRuntimeConstants.CsWinRTDiagnosticsUrlFormat)] [EditorBrowsable(EditorBrowsableState.Never)] -public static unsafe class IListMethods +public static class IListMethods { /// public static int Count(WindowsRuntimeObjectReference thisReference) diff --git a/src/WinRT.Runtime2/ABI/System/Collections/Specialized/INotifyCollectionChanged.cs b/src/WinRT.Runtime2/ABI/System/Collections/Specialized/INotifyCollectionChanged.cs index 066cb0636..5c8692cdb 100644 --- a/src/WinRT.Runtime2/ABI/System/Collections/Specialized/INotifyCollectionChanged.cs +++ b/src/WinRT.Runtime2/ABI/System/Collections/Specialized/INotifyCollectionChanged.cs @@ -48,7 +48,7 @@ public static WindowsRuntimeObjectReferenceValue ConvertToUnmanaged(INotifyColle DiagnosticId = WindowsRuntimeConstants.PrivateImplementationDetailObsoleteDiagnosticId, UrlFormat = WindowsRuntimeConstants.CsWinRTDiagnosticsUrlFormat)] [EditorBrowsable(EditorBrowsableState.Never)] -public static unsafe class INotifyCollectionChangedMethods +public static class INotifyCollectionChangedMethods { /// /// The table for . diff --git a/src/WinRT.Runtime2/ABI/System/ComponentModel/INotifyPropertyChanged.cs b/src/WinRT.Runtime2/ABI/System/ComponentModel/INotifyPropertyChanged.cs index 332ae0d9f..e06f9a025 100644 --- a/src/WinRT.Runtime2/ABI/System/ComponentModel/INotifyPropertyChanged.cs +++ b/src/WinRT.Runtime2/ABI/System/ComponentModel/INotifyPropertyChanged.cs @@ -47,7 +47,7 @@ public static WindowsRuntimeObjectReferenceValue ConvertToUnmanaged(INotifyPrope DiagnosticId = WindowsRuntimeConstants.PrivateImplementationDetailObsoleteDiagnosticId, UrlFormat = WindowsRuntimeConstants.CsWinRTDiagnosticsUrlFormat)] [EditorBrowsable(EditorBrowsableState.Never)] -public static unsafe class INotifyPropertyChangedMethods +public static class INotifyPropertyChangedMethods { /// /// The table for . diff --git a/src/WinRT.Runtime2/ABI/Windows.Foundation/IAsyncAction.cs b/src/WinRT.Runtime2/ABI/Windows.Foundation/IAsyncAction.cs index ccf338616..010c0061b 100644 --- a/src/WinRT.Runtime2/ABI/Windows.Foundation/IAsyncAction.cs +++ b/src/WinRT.Runtime2/ABI/Windows.Foundation/IAsyncAction.cs @@ -48,7 +48,7 @@ public static WindowsRuntimeObjectReferenceValue ConvertToUnmanaged(IAsyncAction file abstract unsafe class IAsyncActionComWrappersCallback : IWindowsRuntimeUnsealedObjectComWrappersCallback { /// - public static unsafe bool TryCreateObject( + public static bool TryCreateObject( void* value, ReadOnlySpan runtimeClassName, [NotNullWhen(true)] out object? wrapperObject, diff --git a/src/WinRT.Runtime2/Bindables/WindowsRuntimeIterator.cs b/src/WinRT.Runtime2/Bindables/WindowsRuntimeIterator.cs index e2e6c0e87..3ff11229a 100644 --- a/src/WinRT.Runtime2/Bindables/WindowsRuntimeIterator.cs +++ b/src/WinRT.Runtime2/Bindables/WindowsRuntimeIterator.cs @@ -15,7 +15,7 @@ namespace WindowsRuntime; /// The implementation of all projected Windows Runtime types. /// /// -internal sealed unsafe class WindowsRuntimeIterator : WindowsRuntimeObject, IEnumerator, IWindowsRuntimeInterface +internal sealed class WindowsRuntimeIterator : WindowsRuntimeObject, IEnumerator, IWindowsRuntimeInterface { /// /// Indicates whether the underlying enumerator has been initialized. diff --git a/src/WinRT.Runtime2/Collections/WindowsRuntimeEnumerator{T}.cs b/src/WinRT.Runtime2/Collections/WindowsRuntimeEnumerator{T}.cs index dfddca617..46bfb52f4 100644 --- a/src/WinRT.Runtime2/Collections/WindowsRuntimeEnumerator{T}.cs +++ b/src/WinRT.Runtime2/Collections/WindowsRuntimeEnumerator{T}.cs @@ -22,7 +22,7 @@ namespace WindowsRuntime; DiagnosticId = WindowsRuntimeConstants.PrivateImplementationDetailObsoleteDiagnosticId, UrlFormat = WindowsRuntimeConstants.CsWinRTDiagnosticsUrlFormat)] [EditorBrowsable(EditorBrowsableState.Never)] -public abstract unsafe class WindowsRuntimeEnumerator : WindowsRuntimeObject, IEnumerator, IWindowsRuntimeInterface> +public abstract class WindowsRuntimeEnumerator : WindowsRuntimeObject, IEnumerator, IWindowsRuntimeInterface> where TIIteratorMethods : IIteratorMethodsImpl { /// diff --git a/src/WinRT.Runtime2/InteropServices/Activation/WindowsRuntimeActivationHelper.cs b/src/WinRT.Runtime2/InteropServices/Activation/WindowsRuntimeActivationHelper.cs index 33977b25f..bf1bbffd1 100644 --- a/src/WinRT.Runtime2/InteropServices/Activation/WindowsRuntimeActivationHelper.cs +++ b/src/WinRT.Runtime2/InteropServices/Activation/WindowsRuntimeActivationHelper.cs @@ -28,7 +28,7 @@ internal static unsafe class WindowsRuntimeActivationHelper /// /// [MethodImpl(MethodImplOptions.NoInlining)] - public static unsafe void ActivateInstanceUnsafe(WindowsRuntimeObjectReference activationFactoryObjectReference, out void* defaultInterface) + public static void ActivateInstanceUnsafe(WindowsRuntimeObjectReference activationFactoryObjectReference, out void* defaultInterface) { using WindowsRuntimeObjectReferenceValue activationFactoryValue = activationFactoryObjectReference.AsValue(); @@ -50,7 +50,7 @@ public static unsafe void ActivateInstanceUnsafe(WindowsRuntimeObjectReference a /// The resulting default interface pointer. /// Thrown if activating the instance fails. [MethodImpl(MethodImplOptions.NoInlining)] - public static unsafe void ActivateInstanceUnsafe( + public static void ActivateInstanceUnsafe( WindowsRuntimeObjectReference activationFactoryObjectReference, string? param0, out void* defaultInterface) @@ -85,7 +85,7 @@ public static unsafe void ActivateInstanceUnsafe( /// /// [MethodImpl(MethodImplOptions.NoInlining)] - public static unsafe void ActivateInstanceUnsafe( + public static void ActivateInstanceUnsafe( WindowsRuntimeObjectReference activationFactoryObjectReference, WindowsRuntimeObject? baseInterface, out void* innerInterface, @@ -113,7 +113,7 @@ public static unsafe void ActivateInstanceUnsafe( /// /// [MethodImpl(MethodImplOptions.NoInlining)] - public static unsafe void ActivateInstanceUnsafe( + public static void ActivateInstanceUnsafe( WindowsRuntimeObjectReference activationFactoryObjectReference, string? param0, WindowsRuntimeObject? baseInterface, @@ -150,7 +150,7 @@ public static unsafe void ActivateInstanceUnsafe( /// /// [MethodImpl(MethodImplOptions.NoInlining)] - public static unsafe void ActivateInstanceUnsafe( + public static void ActivateInstanceUnsafe( WindowsRuntimeObjectReference activationFactoryObjectReference, NotifyCollectionChangedAction param0, IList? param1, diff --git a/src/WinRT.Runtime2/InteropServices/ObjectReference/FreeThreadedObjectReference.cs b/src/WinRT.Runtime2/InteropServices/ObjectReference/FreeThreadedObjectReference.cs index 5225a59d4..a3481313a 100644 --- a/src/WinRT.Runtime2/InteropServices/ObjectReference/FreeThreadedObjectReference.cs +++ b/src/WinRT.Runtime2/InteropServices/ObjectReference/FreeThreadedObjectReference.cs @@ -60,7 +60,7 @@ internal override HRESULT DerivedTryAsNative(in Guid iid, out WindowsRuntimeObje } /// - private protected override unsafe void* GetThisPtrWithContextUnsafe() + private protected override void* GetThisPtrWithContextUnsafe() { // This method is never called for free-threaded objects return null; diff --git a/src/WinRT.Runtime2/InteropServices/Platform/WindowsRuntimeImports.cs b/src/WinRT.Runtime2/InteropServices/Platform/WindowsRuntimeImports.cs index d98a12d38..eb4d9cbc2 100644 --- a/src/WinRT.Runtime2/InteropServices/Platform/WindowsRuntimeImports.cs +++ b/src/WinRT.Runtime2/InteropServices/Platform/WindowsRuntimeImports.cs @@ -18,7 +18,7 @@ internal static unsafe partial class WindowsRuntimeImports /// [LibraryImport("api-ms-win-core-com-l1-1-0.dll")] - public static unsafe partial HRESULT CoIncrementMTAUsage(CO_MTA_USAGE_COOKIE* cookie); + public static partial HRESULT CoIncrementMTAUsage(CO_MTA_USAGE_COOKIE* cookie); /// [LibraryImport("api-ms-win-core-com-l1-1-0.dll")] diff --git a/src/WinRT.Runtime2/InteropServices/ProjectionImpls/IAgileObjectImpl.cs b/src/WinRT.Runtime2/InteropServices/ProjectionImpls/IAgileObjectImpl.cs index 920ac477f..f8db6bff2 100644 --- a/src/WinRT.Runtime2/InteropServices/ProjectionImpls/IAgileObjectImpl.cs +++ b/src/WinRT.Runtime2/InteropServices/ProjectionImpls/IAgileObjectImpl.cs @@ -15,7 +15,7 @@ namespace WindowsRuntime.InteropServices; DiagnosticId = WindowsRuntimeConstants.PrivateImplementationDetailObsoleteDiagnosticId, UrlFormat = WindowsRuntimeConstants.CsWinRTDiagnosticsUrlFormat)] [EditorBrowsable(EditorBrowsableState.Never)] -public static unsafe class IAgileObjectImpl +public static class IAgileObjectImpl { /// /// Gets a pointer to the managed IAgileObject implementation.