From d80f171839958b3de47044feff7d77bba9e1c885 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Wed, 4 Dec 2019 22:11:12 -0500 Subject: [PATCH] Annotate System.IO.FileSystem.DriveInfo for nullable reference types (#464) * Annotate System.IO.FileSystem.DriveInfo for nullable reference types * Address PR feedback --- .../System.Native/Interop.MountPoints.FormatInfo.cs | 10 ++++------ .../Interop/Windows/Kernel32/Interop.SetVolumeLabel.cs | 2 +- .../ref/System.IO.FileSystem.DriveInfo.cs | 5 +++-- .../ref/System.IO.FileSystem.DriveInfo.csproj | 1 + .../src/System.IO.FileSystem.DriveInfo.csproj | 1 + .../src/System/IO/DriveInfo.Unix.cs | 2 ++ .../src/System/IO/DriveInfo.Windows.cs | 2 ++ .../src/System/IO/DriveNotFoundException.cs | 4 ++-- 8 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.MountPoints.FormatInfo.cs b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.MountPoints.FormatInfo.cs index 4240bd4853a..ee5e4272295 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.MountPoints.FormatInfo.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.MountPoints.FormatInfo.cs @@ -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 diff --git a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.SetVolumeLabel.cs b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.SetVolumeLabel.cs index e28a3e659a1..d23c7f33b8a 100644 --- a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.SetVolumeLabel.cs +++ b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.SetVolumeLabel.cs @@ -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); } } diff --git a/src/libraries/System.IO.FileSystem.DriveInfo/ref/System.IO.FileSystem.DriveInfo.cs b/src/libraries/System.IO.FileSystem.DriveInfo/ref/System.IO.FileSystem.DriveInfo.cs index d2c102e6737..f76bd6cc7f8 100644 --- a/src/libraries/System.IO.FileSystem.DriveInfo/ref/System.IO.FileSystem.DriveInfo.cs +++ b/src/libraries/System.IO.FileSystem.DriveInfo/ref/System.IO.FileSystem.DriveInfo.cs @@ -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 { diff --git a/src/libraries/System.IO.FileSystem.DriveInfo/ref/System.IO.FileSystem.DriveInfo.csproj b/src/libraries/System.IO.FileSystem.DriveInfo/ref/System.IO.FileSystem.DriveInfo.csproj index 97fd59eef52..2027e8706f5 100644 --- a/src/libraries/System.IO.FileSystem.DriveInfo/ref/System.IO.FileSystem.DriveInfo.csproj +++ b/src/libraries/System.IO.FileSystem.DriveInfo/ref/System.IO.FileSystem.DriveInfo.csproj @@ -1,5 +1,6 @@ + enable netcoreapp-Debug;netcoreapp-Release diff --git a/src/libraries/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj b/src/libraries/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj index 2d0b6ff1b39..cd18c3fb3a7 100644 --- a/src/libraries/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj +++ b/src/libraries/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj @@ -3,6 +3,7 @@ System.IO.FileSystem.DriveInfo System.IO.FileSystem.DriveInfo true + enable netcoreapp-Unix-Debug;netcoreapp-Unix-Release;netcoreapp-Windows_NT-Debug;netcoreapp-Windows_NT-Release diff --git a/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Unix.cs b/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Unix.cs index 1322cceb275..19aff6860ca 100644 --- a/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Unix.cs +++ b/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Unix.cs @@ -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 diff --git a/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Windows.cs b/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Windows.cs index 412959a55b3..ef454de2698 100644 --- a/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Windows.cs +++ b/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Windows.cs @@ -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 diff --git a/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveNotFoundException.cs b/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveNotFoundException.cs index 9cf3af8dc3e..c347dbbb169 100644 --- a/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveNotFoundException.cs +++ b/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveNotFoundException.cs @@ -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; -- 2.34.1