Annotate System.IO.FileSystem.DriveInfo for nullable reference types (#464)
authorStephen Toub <stoub@microsoft.com>
Thu, 5 Dec 2019 03:11:12 +0000 (22:11 -0500)
committerGitHub <noreply@github.com>
Thu, 5 Dec 2019 03:11:12 +0000 (22:11 -0500)
* Annotate System.IO.FileSystem.DriveInfo for nullable reference types

* Address PR feedback

src/libraries/Common/src/Interop/Unix/System.Native/Interop.MountPoints.FormatInfo.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.SetVolumeLabel.cs
src/libraries/System.IO.FileSystem.DriveInfo/ref/System.IO.FileSystem.DriveInfo.cs
src/libraries/System.IO.FileSystem.DriveInfo/ref/System.IO.FileSystem.DriveInfo.csproj
src/libraries/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj
src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Unix.cs
src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Windows.cs
src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveNotFoundException.cs

index 4240bd4..ee5e427 100644 (file)
@@ -163,14 +163,12 @@ internal static partial class Interop
 
         internal static int GetFormatInfoForMountPoint(string name, out string format)
         {
-            DriveType temp;
-            return GetFormatInfoForMountPoint(name, out format, out temp);
+            return GetFormatInfoForMountPoint(name, out format, out _);
         }
 
         internal static int GetFormatInfoForMountPoint(string name, out DriveType type)
         {
-            string temp;
-            return GetFormatInfoForMountPoint(name, out temp, out type);
+            return GetFormatInfoForMountPoint(name, out _, out type);
         }
 
         private static int GetFormatInfoForMountPoint(string name, out string format, out DriveType type)
@@ -184,8 +182,8 @@ internal static partial class Interop
                 {
                     // Check if we have a numeric answer or string
                     format = numericFormat != -1 ?
-                        Enum.GetName(typeof(UnixFileSystemTypes), numericFormat) :
-                        Marshal.PtrToStringAnsi((IntPtr)formatBuffer);
+                        Enum.GetName(typeof(UnixFileSystemTypes), numericFormat) ?? string.Empty :
+                        Marshal.PtrToStringAnsi((IntPtr)formatBuffer)!;
                     type = GetDriveType(format);
                 }
                 else
index e28a3e6..d23c7f3 100644 (file)
@@ -9,6 +9,6 @@ internal partial class Interop
     internal partial class Kernel32
     {
         [DllImport(Libraries.Kernel32, EntryPoint = "SetVolumeLabelW", CharSet = CharSet.Unicode, SetLastError = true, BestFitMapping = false)]
-        internal static extern bool SetVolumeLabel(string driveLetter, string volumeName);
+        internal static extern bool SetVolumeLabel(string driveLetter, string? volumeName);
     }
 }
index d2c102e..f76bd6c 100644 (file)
@@ -18,6 +18,7 @@ namespace System.IO
         public System.IO.DirectoryInfo RootDirectory { get { throw null; } }
         public long TotalFreeSpace { get { throw null; } }
         public long TotalSize { get { throw null; } }
+        [System.Diagnostics.CodeAnalysis.AllowNull]
         public string VolumeLabel { get { throw null; } set { } }
         public static System.IO.DriveInfo[] GetDrives() { throw null; }
         void System.Runtime.Serialization.ISerializable.GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
@@ -27,8 +28,8 @@ namespace System.IO
     {
         public DriveNotFoundException() { }
         protected DriveNotFoundException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
-        public DriveNotFoundException(string message) { }
-        public DriveNotFoundException(string message, System.Exception innerException) { }
+        public DriveNotFoundException(string? message) { }
+        public DriveNotFoundException(string? message, System.Exception? innerException) { }
     }
     public enum DriveType
     {
index 97fd59e..2027e87 100644 (file)
@@ -1,5 +1,6 @@
 <Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
+    <Nullable>enable</Nullable>
     <Configurations>netcoreapp-Debug;netcoreapp-Release</Configurations>
   </PropertyGroup>
   <ItemGroup>
index 2d0b6ff..cd18c3f 100644 (file)
@@ -3,6 +3,7 @@
     <RootNamespace>System.IO.FileSystem.DriveInfo</RootNamespace>
     <AssemblyName>System.IO.FileSystem.DriveInfo</AssemblyName>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+    <Nullable>enable</Nullable>
     <Configurations>netcoreapp-Unix-Debug;netcoreapp-Unix-Release;netcoreapp-Windows_NT-Debug;netcoreapp-Windows_NT-Release</Configurations>
   </PropertyGroup>
   <ItemGroup>
index 1322cce..19aff68 100644 (file)
@@ -3,6 +3,7 @@
 // See the LICENSE file in the project root for more information.
 
 using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
 using System.Security;
 
 namespace System.IO
@@ -104,6 +105,7 @@ namespace System.IO
             }
         }
 
+        [AllowNull]
         public string VolumeLabel
         {
             get
index 412959a..ef454de 100644 (file)
@@ -4,6 +4,7 @@
 
 using System;
 using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
 using System.Runtime.InteropServices;
 using System.Text;
 
@@ -121,6 +122,7 @@ namespace System.IO
         }
 
         // Null is a valid volume label.
+        [AllowNull]
         public unsafe string VolumeLabel
         {
             get
index 9cf3af8..c347dbb 100644 (file)
@@ -17,13 +17,13 @@ namespace System.IO
             HResult = HResults.COR_E_DIRECTORYNOTFOUND;
         }
 
-        public DriveNotFoundException(string message)
+        public DriveNotFoundException(string? message)
             : base(message)
         {
             HResult = HResults.COR_E_DIRECTORYNOTFOUND;
         }
 
-        public DriveNotFoundException(string message, Exception innerException)
+        public DriveNotFoundException(string? message, Exception? innerException)
             : base(message, innerException)
         {
             HResult = HResults.COR_E_DIRECTORYNOTFOUND;