Reduce code in SafeHandleAddRef (dotnet/coreclr#9724)
authorStephen Toub <stoub@microsoft.com>
Thu, 23 Feb 2017 07:22:43 +0000 (02:22 -0500)
committerDan Moseley <danmose@microsoft.com>
Thu, 23 Feb 2017 07:22:43 +0000 (23:22 -0800)
If DangerousAddRef returns rather than throws, its argument will be true, so there's no need to branch on it.

Commit migrated from https://github.com/dotnet/coreclr/commit/6af4182d058df3520103d81c3912899e8d29f884

src/coreclr/src/mscorlib/src/System/StubHelpers.cs
src/coreclr/src/mscorlib/src/System/ThrowHelper.cs

index 2d5926b..b076331 100644 (file)
@@ -1618,13 +1618,11 @@ namespace  System.StubHelpers {
         {
             if (pHandle == null)
             {
-                throw new ArgumentNullException(nameof(pHandle), Environment.GetResourceString("ArgumentNull_SafeHandle"));
+                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.pHandle, ExceptionResource.ArgumentNull_SafeHandle);
             }
-            Contract.EndContractBlock();
 
             pHandle.DangerousAddRef(ref success);
-
-            return (success ? pHandle.DangerousGetHandle() : IntPtr.Zero);
+            return pHandle.DangerousGetHandle();
         }
 
         // Releases the SH (to be called from finally block).
@@ -1632,9 +1630,8 @@ namespace  System.StubHelpers {
         {
             if (pHandle == null)
             {
-                throw new ArgumentNullException(nameof(pHandle), Environment.GetResourceString("ArgumentNull_SafeHandle"));
+                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.pHandle, ExceptionResource.ArgumentNull_SafeHandle);
             }
-            Contract.EndContractBlock();
 
             try
             {
index 99f074d..f187193 100644 (file)
@@ -136,6 +136,11 @@ namespace System {
             throw new ArgumentNullException(GetResourceString(resource));
         }
 
+        internal static void ThrowArgumentNullException(ExceptionArgument argument, ExceptionResource resource)
+        {
+            throw new ArgumentNullException(GetArgumentName(argument), GetResourceString(resource));
+        }
+
         internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument) {
             throw new ArgumentOutOfRangeException(GetArgumentName(argument));
         }
@@ -363,6 +368,7 @@ namespace System {
         callBack,
         type,
         stateMachine,
+        pHandle,
     }
 
     //
@@ -469,6 +475,7 @@ namespace System {
         ArgumentOutOfRange_Enum,
         InvalidOperation_HandleIsNotInitialized,
         AsyncMethodBuilder_InstanceNotInitialized,
+        ArgumentNull_SafeHandle,
     }
 }