Replace several Nullable<T>.Value with .GetValueOrDefault() (dotnet/coreclr#22297)
authorStephen Toub <stoub@microsoft.com>
Fri, 1 Feb 2019 04:20:14 +0000 (23:20 -0500)
committerGitHub <noreply@github.com>
Fri, 1 Feb 2019 04:20:14 +0000 (23:20 -0500)
The former does extra work that the latter doesn't do, and they're equivalent when we know it contains a value, such as immediately after a HasValue check.

Commit migrated from https://github.com/dotnet/coreclr/commit/1b2810b1ad92d2671da76d88245198b62f5b964e

src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/CLRIReferenceImpl.cs
src/libraries/System.Private.CoreLib/src/System/IO/FileStream.Unix.cs
src/libraries/System.Private.CoreLib/src/System/IO/FileStream.cs
src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.Unix.cs
src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.cs

index 7b70047..8b9e7f1 100644 (file)
@@ -296,7 +296,7 @@ namespace System.Runtime.InteropServices.WindowsRuntime
             if (propType.HasValue)
             {
                 Type specificType = typeof(CLRIReferenceImpl<>).MakeGenericType(type);
-                return Activator.CreateInstance(specificType, new object[] { propType.Value, obj });
+                return Activator.CreateInstance(specificType, new object[] { propType.GetValueOrDefault(), obj });
             }
 
             Debug.Fail("We should not see non-WinRT type here");
@@ -389,7 +389,7 @@ namespace System.Runtime.InteropServices.WindowsRuntime
             {
                 // All WinRT value type will be Property.Other
                 Type specificType = typeof(CLRIReferenceArrayImpl<>).MakeGenericType(type);
-                return Activator.CreateInstance(specificType, new object[] { propType.Value, obj });
+                return Activator.CreateInstance(specificType, new object[] { propType.GetValueOrDefault(), obj });
             }
             else
             {
index fc29134..3994989 100644 (file)
@@ -209,7 +209,7 @@ namespace System.IO
                 _canSeek = Interop.Sys.LSeek(fileHandle, 0, Interop.Sys.SeekWhence.SEEK_CUR) >= 0;
             }
 
-            return _canSeek.Value;
+            return _canSeek.GetValueOrDefault();
         }
 
         private long GetLengthInternal()
index ef8df81..5c8913b 100644 (file)
@@ -135,7 +135,7 @@ namespace System.IO
 
             if (handle.IsClosed)
                 throw new ObjectDisposedException(SR.ObjectDisposed_FileClosed);
-            if (handle.IsAsync.HasValue && isAsync != handle.IsAsync.Value)
+            if (handle.IsAsync.HasValue && isAsync != handle.IsAsync.GetValueOrDefault())
                 throw new ArgumentException(SR.Arg_HandleNotAsync, nameof(handle));
 
             _exposedHandle = true;
index 27b6326..9ca9e9c 100644 (file)
@@ -1095,7 +1095,7 @@ namespace System
                 TimeSpan? parsedBaseOffset = TZif_ParseOffsetString(standardOffset);
                 if (parsedBaseOffset.HasValue)
                 {
-                    TimeSpan baseOffset = parsedBaseOffset.Value.Negate(); // offsets are backwards in POSIX notation
+                    TimeSpan baseOffset = parsedBaseOffset.GetValueOrDefault().Negate(); // offsets are backwards in POSIX notation
                     baseOffset = TZif_CalculateTransitionOffsetFromBase(baseOffset, timeZoneBaseUtcOffset);
 
                     // having a daylightSavingsName means there is a DST rule
@@ -1110,7 +1110,7 @@ namespace System
                         }
                         else
                         {
-                            daylightSavingsTimeSpan = parsedDaylightSavings.Value.Negate(); // offsets are backwards in POSIX notation
+                            daylightSavingsTimeSpan = parsedDaylightSavings.GetValueOrDefault().Negate(); // offsets are backwards in POSIX notation
                             daylightSavingsTimeSpan = TZif_CalculateTransitionOffsetFromBase(daylightSavingsTimeSpan, timeZoneBaseUtcOffset);
                             daylightSavingsTimeSpan = TZif_CalculateTransitionOffsetFromBase(daylightSavingsTimeSpan, baseOffset);
                         }
@@ -1176,7 +1176,7 @@ namespace System
 
                 if (result.HasValue && negative)
                 {
-                    result = result.Value.Negate();
+                    result = result.GetValueOrDefault().Negate();
                 }
             }
 
@@ -1193,8 +1193,8 @@ namespace System
                 // Some time zones use time values like, "26", "144", or "-2".
                 // This allows the week to sometimes be week 4 and sometimes week 5 in the month.
                 // For now, strip off any 'days' in the offset, and just get the time of day correct
-                timeOffset = new TimeSpan(timeOffset.Value.Hours, timeOffset.Value.Minutes, timeOffset.Value.Seconds);
-                if (timeOffset.Value < TimeSpan.Zero)
+                timeOffset = new TimeSpan(timeOffset.GetValueOrDefault().Hours, timeOffset.GetValueOrDefault().Minutes, timeOffset.GetValueOrDefault().Seconds);
+                if (timeOffset.GetValueOrDefault() < TimeSpan.Zero)
                 {
                     timeOfDay = new DateTime(1, 1, 2, 0, 0, 0);
                 }
@@ -1203,7 +1203,7 @@ namespace System
                     timeOfDay = new DateTime(1, 1, 1, 0, 0, 0);
                 }
 
-                timeOfDay += timeOffset.Value;
+                timeOfDay += timeOffset.GetValueOrDefault();
             }
             else
             {
index bbb0c46..698fa21 100644 (file)
@@ -286,9 +286,9 @@ namespace System
         {
             Debug.Assert(rule.NoDaylightTransitions, "GetPreviousAdjustmentRule should only be used with NoDaylightTransitions rules.");
 
-            if (ruleIndex.HasValue && 0 < ruleIndex.Value && ruleIndex.Value < _adjustmentRules.Length)
+            if (ruleIndex.HasValue && 0 < ruleIndex.GetValueOrDefault() && ruleIndex.GetValueOrDefault() < _adjustmentRules.Length)
             {
-                return _adjustmentRules[ruleIndex.Value - 1];
+                return _adjustmentRules[ruleIndex.GetValueOrDefault() - 1];
             }
 
             AdjustmentRule result = rule;