Tighten nullable annotations on RegistryKey.GetValue (#72679)
authorStephen Toub <stoub@microsoft.com>
Fri, 22 Jul 2022 21:48:03 +0000 (17:48 -0400)
committerGitHub <noreply@github.com>
Fri, 22 Jul 2022 21:48:03 +0000 (17:48 -0400)
* Tighten nullable annotations on RegistryKey.GetValue

* Use nameof in attributes

src/libraries/Microsoft.Win32.Registry/ref/Microsoft.Win32.Registry.cs
src/libraries/Microsoft.Win32.Registry/src/Microsoft/Win32/RegistryKey.FileSystem.cs
src/libraries/Microsoft.Win32.Registry/src/Microsoft/Win32/RegistryKey.Windows.cs
src/libraries/Microsoft.Win32.Registry/src/Microsoft/Win32/RegistryKey.cs

index 99acbd1..f92165d 100644 (file)
@@ -57,7 +57,9 @@ namespace Microsoft.Win32
         public System.Security.AccessControl.RegistrySecurity GetAccessControl(System.Security.AccessControl.AccessControlSections includeSections) { throw null; }
         public string[] GetSubKeyNames() { throw null; }
         public object? GetValue(string? name) { throw null; }
+        [return: System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute("defaultValue")]
         public object? GetValue(string? name, object? defaultValue) { throw null; }
+        [return: System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute("defaultValue")]
         public object? GetValue(string? name, object? defaultValue, Microsoft.Win32.RegistryValueOptions options) { throw null; }
         public Microsoft.Win32.RegistryValueKind GetValueKind(string? name) { throw null; }
         public string[] GetValueNames() { throw null; }
index 3d4274c..f91463f 100644 (file)
@@ -3,6 +3,7 @@
 
 using Microsoft.Win32.SafeHandles;
 using System;
+using System.Diagnostics.CodeAnalysis;
 
 namespace Microsoft.Win32
 {
@@ -91,6 +92,7 @@ namespace Microsoft.Win32
             throw new PlatformNotSupportedException(SR.PlatformNotSupported_Registry);
         }
 
+        [return: NotNullIfNotNull(nameof(defaultValue))]
         private static object InternalGetValueCore(string? name, object? defaultValue, bool doNotExpand)
         {
             throw new PlatformNotSupportedException(SR.PlatformNotSupported_Registry);
index c21cbb2..f3de353 100644 (file)
@@ -6,6 +6,7 @@ using System;
 using System.Buffers;
 using System.Collections.Generic;
 using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
 using System.IO;
 using System.Security;
 
@@ -528,6 +529,7 @@ namespace Microsoft.Win32
             return names.ToArray();
         }
 
+        [return: NotNullIfNotNull(nameof(defaultValue))]
         private object? InternalGetValueCore(string? name, object? defaultValue, bool doNotExpand)
         {
             object? data = defaultValue;
index 233b7bf..8412bb5 100644 (file)
@@ -509,11 +509,13 @@ namespace Microsoft.Win32
         /// <param name="name">Name of value to retrieve.</param>
         /// <param name="defaultValue">Value to return if <i>name</i> doesn't exist.</param>
         /// <returns>The data associated with the value.</returns>
+        [return: NotNullIfNotNull(nameof(defaultValue))]
         public object? GetValue(string? name, object? defaultValue)
         {
             return InternalGetValue(name, defaultValue, false);
         }
 
+        [return: NotNullIfNotNull(nameof(defaultValue))]
         public object? GetValue(string? name, object? defaultValue, RegistryValueOptions options)
         {
             if (options < RegistryValueOptions.None || options > RegistryValueOptions.DoNotExpandEnvironmentNames)
@@ -524,6 +526,7 @@ namespace Microsoft.Win32
             return InternalGetValue(name, defaultValue, doNotExpand);
         }
 
+        [return: NotNullIfNotNull(nameof(defaultValue))]
         private object? InternalGetValue(string? name, object? defaultValue, bool doNotExpand)
         {
             EnsureNotDisposed();