From 684f2dd0c1e98a69d8e6ac05e3b58471270e03bb Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Thu, 9 Jan 2020 08:05:35 -0800 Subject: [PATCH 1/7] Enable nullable reference types for System.Windows.Forms.Primitives --- .../src/Interop/ComCtl32/Interop.TTOOLINFOW.cs | 2 ++ .../src/Interop/Ole32/Interop.GPStream.cs | 2 ++ .../src/Interop/Ole32/Interop.OLEVERB.cs | 2 ++ .../src/Interop/Ole32/Interop.QACONTAINER.cs | 2 ++ .../src/Interop/Ole32/Interop.STATSTG.cs | 2 ++ .../src/Interop/Ole32/Interop.VARIANT.cs | 2 ++ .../src/Interop/Shell32/Interop.SHGetPathFromIDListLongPath.cs | 2 ++ .../src/Interop/User32/Interop.GetClipboardFormatNameW.cs | 2 ++ .../src/System.Windows.Forms.Primitives.csproj | 1 + .../src/System/ComponentModel/CompModSwitches.cs | 2 ++ .../src/System/Windows/Forms/FileDialogCustomPlace.cs | 2 ++ .../src/System/Windows/Forms/Internals/ClientUtils.cs | 2 ++ .../src/System/Windows/Forms/Internals/DpiHelper.cs | 2 ++ .../System/Windows/Forms/Internals/FileDialog_Vista_Interop.cs | 2 ++ .../src/System/Windows/Forms/Internals/MessageDecoder.cs | 2 ++ .../src/System/Windows/Forms/Internals/NativeMethods.cs | 2 ++ .../src/System/Windows/Forms/Internals/UnsafeNativeMethods.cs | 2 ++ .../src/System/Windows/Forms/Message.cs | 2 ++ .../src/System/Windows/Forms/Padding.cs | 2 ++ .../src/System/Windows/Forms/PaddingConverter.cs | 2 ++ 20 files changed, 39 insertions(+) diff --git a/src/System.Windows.Forms.Primitives/src/Interop/ComCtl32/Interop.TTOOLINFOW.cs b/src/System.Windows.Forms.Primitives/src/Interop/ComCtl32/Interop.TTOOLINFOW.cs index a89220408b7..a5420f89efe 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/ComCtl32/Interop.TTOOLINFOW.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/ComCtl32/Interop.TTOOLINFOW.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable disable + using System; using System.Runtime.InteropServices; diff --git a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.GPStream.cs b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.GPStream.cs index 6d9ec759d8b..855f3a36e8a 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.GPStream.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.GPStream.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable disable + using System; using System.Buffers; using System.IO; diff --git a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.OLEVERB.cs b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.OLEVERB.cs index e265d3e1655..684c82a2e57 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.OLEVERB.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.OLEVERB.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable disable + using System.Runtime.InteropServices; internal static partial class Interop diff --git a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.QACONTAINER.cs b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.QACONTAINER.cs index 0b37eed6373..a8d73784190 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.QACONTAINER.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.QACONTAINER.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable disable + using System; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; diff --git a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.STATSTG.cs b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.STATSTG.cs index 705df8d4a53..8363874bba8 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.STATSTG.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.STATSTG.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable disable + using System; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; diff --git a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.VARIANT.cs b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.VARIANT.cs index c473e9c7526..cb5109c6b4c 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.VARIANT.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.VARIANT.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable disable + using System; using System.Runtime.InteropServices; using System.Windows.Forms.Primitives.Resources; diff --git a/src/System.Windows.Forms.Primitives/src/Interop/Shell32/Interop.SHGetPathFromIDListLongPath.cs b/src/System.Windows.Forms.Primitives/src/Interop/Shell32/Interop.SHGetPathFromIDListLongPath.cs index fe91cb1d1aa..2005b19806b 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/Shell32/Interop.SHGetPathFromIDListLongPath.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/Shell32/Interop.SHGetPathFromIDListLongPath.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable disable + using System; using System.Runtime.InteropServices; diff --git a/src/System.Windows.Forms.Primitives/src/Interop/User32/Interop.GetClipboardFormatNameW.cs b/src/System.Windows.Forms.Primitives/src/Interop/User32/Interop.GetClipboardFormatNameW.cs index 440ce5ad200..089edfbb624 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/User32/Interop.GetClipboardFormatNameW.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/User32/Interop.GetClipboardFormatNameW.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable disable + using System; using System.Runtime.InteropServices; using System.Windows.Forms; diff --git a/src/System.Windows.Forms.Primitives/src/System.Windows.Forms.Primitives.csproj b/src/System.Windows.Forms.Primitives/src/System.Windows.Forms.Primitives.csproj index 20554aada81..5c7efa18638 100644 --- a/src/System.Windows.Forms.Primitives/src/System.Windows.Forms.Primitives.csproj +++ b/src/System.Windows.Forms.Primitives/src/System.Windows.Forms.Primitives.csproj @@ -4,6 +4,7 @@ System.Windows.Forms.Primitives true true + enable $(NoWarn);618 true diff --git a/src/System.Windows.Forms.Primitives/src/System/ComponentModel/CompModSwitches.cs b/src/System.Windows.Forms.Primitives/src/System/ComponentModel/CompModSwitches.cs index 60e71c1319a..b9efc1dd3c9 100644 --- a/src/System.Windows.Forms.Primitives/src/System/ComponentModel/CompModSwitches.cs +++ b/src/System.Windows.Forms.Primitives/src/System/ComponentModel/CompModSwitches.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable disable + using System.Diagnostics; namespace System.ComponentModel diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/FileDialogCustomPlace.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/FileDialogCustomPlace.cs index c10e0b37132..6955e9bf6aa 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/FileDialogCustomPlace.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/FileDialogCustomPlace.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable disable + using static Interop; namespace System.Windows.Forms diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/ClientUtils.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/ClientUtils.cs index b8034779e7f..da365447da3 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/ClientUtils.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/ClientUtils.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable disable + using System.Collections; using System.Diagnostics; using System.Globalization; diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/DpiHelper.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/DpiHelper.cs index 2c93d53e570..9c10c34d2c4 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/DpiHelper.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/DpiHelper.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable disable + using System.Drawing; using System.Drawing.Drawing2D; using static Interop; diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/FileDialog_Vista_Interop.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/FileDialog_Vista_Interop.cs index 6e69112e8f2..9e2fcc7dd2c 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/FileDialog_Vista_Interop.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/FileDialog_Vista_Interop.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable disable + using System.IO; using System.Runtime.InteropServices; diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/MessageDecoder.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/MessageDecoder.cs index 40367cfb69d..891a2878bd6 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/MessageDecoder.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/MessageDecoder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable disable + using static Interop; namespace System.Windows.Forms diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/NativeMethods.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/NativeMethods.cs index 460b60ab63b..f94a85221de 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/NativeMethods.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/NativeMethods.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable disable + using System.Drawing; using System.Runtime.InteropServices; using static Interop; diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/UnsafeNativeMethods.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/UnsafeNativeMethods.cs index c320bcdfd5f..57802121948 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/UnsafeNativeMethods.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/UnsafeNativeMethods.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable disable + using System.Diagnostics; using System.Drawing; using System.Runtime.CompilerServices; diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Message.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Message.cs index e24627bba44..24e2ee97b28 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Message.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Message.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable disable + using System.Diagnostics; using System.Runtime.InteropServices; using static Interop; diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Padding.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Padding.cs index 773c467d84e..33a57d4fe0a 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Padding.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Padding.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable disable + using System.ComponentModel; using System.Diagnostics; using System.Drawing; diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/PaddingConverter.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/PaddingConverter.cs index 376ec2283f5..7331cf5053a 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/PaddingConverter.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/PaddingConverter.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable disable + using System.Collections; using System.ComponentModel; using System.ComponentModel.Design.Serialization; From 7bd4aaaf67a61ed429c6b3bc24ea95561406bceb Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Thu, 9 Jan 2020 08:29:59 -0800 Subject: [PATCH 2/7] Add missing null checks in implementation code --- .../src/Interop/Ole32/Interop.VARIANT.cs | 2 +- .../src/System/Windows/Forms/Internals/UnsafeNativeMethods.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.VARIANT.cs b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.VARIANT.cs index cb5109c6b4c..8c43b943925 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.VARIANT.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.VARIANT.cs @@ -168,7 +168,7 @@ public object ToObject() case VARENUM.VARIANT: VARIANT varStruct = Marshal.PtrToStructure(val); - return varStruct.ToObject(); + return varStruct?.ToObject(); case VARENUM.CLSID: Guid guid = Marshal.PtrToStructure(val); diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/UnsafeNativeMethods.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/UnsafeNativeMethods.cs index 57802121948..8a853223824 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/UnsafeNativeMethods.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/UnsafeNativeMethods.cs @@ -83,7 +83,7 @@ public static int DragQueryFileLongPath(HandleRef hDrop, int iFile, StringBuilde } else { - return DragQueryFile(hDrop, iFile, lpszFile, lpszFile.Capacity); + return DragQueryFile(hDrop, iFile, lpszFile, lpszFile?.Capacity ?? 0); } } From 742548cdaf9fc65ea8f928546b523d3d04ed2ccb Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Thu, 9 Jan 2020 08:32:39 -0800 Subject: [PATCH 3/7] Annotate files for nullability where required for a clean build --- .../Interop/ComCtl32/Interop.TTOOLINFOW.cs | 8 +- .../src/Interop/Ole32/Interop.GPStream.cs | 2 - .../src/Interop/Ole32/Interop.OLEVERB.cs | 4 +- .../src/Interop/Ole32/Interop.QACONTAINER.cs | 14 ++- .../src/Interop/Ole32/Interop.STATSTG.cs | 6 +- .../src/Interop/Ole32/Interop.VARIANT.cs | 6 +- .../Interop.SHGetPathFromIDListLongPath.cs | 4 +- .../User32/Interop.GetClipboardFormatNameW.cs | 4 +- .../System/ComponentModel/CompModSwitches.cs | 68 +++++++------ .../Windows/Forms/FileDialogCustomPlace.cs | 4 +- .../Forms/FileDialogCustomPlacesCollection.cs | 2 +- .../Windows/Forms/Internals/ClientUtils.cs | 46 +++++---- .../Windows/Forms/Internals/DpiHelper.cs | 6 +- .../Internals/FileDialog_Vista_Interop.cs | 4 +- .../Windows/Forms/Internals/MessageDecoder.cs | 8 +- .../Windows/Forms/Internals/NativeMethods.cs | 98 +++++++++---------- .../Forms/Internals/UnsafeNativeMethods.cs | 8 +- .../src/System/Windows/Forms/Message.cs | 6 +- .../src/System/Windows/Forms/Padding.cs | 4 +- .../System/Windows/Forms/PaddingConverter.cs | 14 ++- 20 files changed, 144 insertions(+), 172 deletions(-) diff --git a/src/System.Windows.Forms.Primitives/src/Interop/ComCtl32/Interop.TTOOLINFOW.cs b/src/System.Windows.Forms.Primitives/src/Interop/ComCtl32/Interop.TTOOLINFOW.cs index a5420f89efe..f83dbc36dcd 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/ComCtl32/Interop.TTOOLINFOW.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/ComCtl32/Interop.TTOOLINFOW.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System; using System.Runtime.InteropServices; @@ -43,10 +41,10 @@ public struct ToolInfoWrapper where T : IHandle { public TTOOLINFOW Info; - public string Text { get; set; } + public string? Text { get; set; } private readonly T _handle; - public unsafe ToolInfoWrapper(T handle, TTF flags = default, string text = null) + public unsafe ToolInfoWrapper(T handle, TTF flags = default, string? text = null) { Info = new TTOOLINFOW { @@ -58,7 +56,7 @@ public unsafe ToolInfoWrapper(T handle, TTF flags = default, string text = null) _handle = handle; } - public unsafe ToolInfoWrapper(T handle, IntPtr id, TTF flags = default, string text = null, RECT rect = default) + public unsafe ToolInfoWrapper(T handle, IntPtr id, TTF flags = default, string? text = null, RECT rect = default) { Info = new TTOOLINFOW { diff --git a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.GPStream.cs b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.GPStream.cs index 855f3a36e8a..6d9ec759d8b 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.GPStream.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.GPStream.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System; using System.Buffers; using System.IO; diff --git a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.OLEVERB.cs b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.OLEVERB.cs index 684c82a2e57..4877b2a6467 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.OLEVERB.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.OLEVERB.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Runtime.InteropServices; internal static partial class Interop @@ -15,7 +13,7 @@ public class OLEVERB { public OLEIVERB lVerb; [MarshalAs(UnmanagedType.LPWStr)] - public string lpszVerbName; + public string? lpszVerbName; public User32.MF fuFlags; public OLEVERBATTRIB grfAttribs; } diff --git a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.QACONTAINER.cs b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.QACONTAINER.cs index a8d73784190..fd22bf652d4 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.QACONTAINER.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.QACONTAINER.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; @@ -16,20 +14,20 @@ internal static partial class Ole32 public sealed class QACONTAINER { public uint cbSize; - public IOleClientSite pClientSite; - public IAdviseSink pAdviseSink; - public IPropertyNotifySink pPropertyNotifySink; + public IOleClientSite? pClientSite; + public IAdviseSink? pAdviseSink; + public IPropertyNotifySink? pPropertyNotifySink; [MarshalAs(UnmanagedType.Interface)] - public object pUnkEventSink; + public object? pUnkEventSink; public QACONTAINERFLAGS dwAmbientFlags; public uint colorFore; public uint colorBack; - public IFont pFont; + public IFont? pFont; public IntPtr pUndoMgr; public uint dwAppearance; public int lcid; public IntPtr hpal; - public IServiceProvider pBindHost; + public IServiceProvider? pBindHost; } } } diff --git a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.STATSTG.cs b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.STATSTG.cs index 8363874bba8..5c56823fa7d 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.STATSTG.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.STATSTG.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; @@ -62,7 +60,7 @@ public struct STATSTG public uint grfStateBits; public uint reserved; - public string GetName() => Marshal.PtrToStringUni(pwcsName); + public string? GetName() => Marshal.PtrToStringUni(pwcsName); /// /// Caller is responsible for freeing the name memory. @@ -78,7 +76,7 @@ public void FreeName() /// /// Callee is repsonsible for allocating the name memory. /// - public void AllocName(string name) + public void AllocName(string? name) { pwcsName = Marshal.StringToCoTaskMemUni(name); } diff --git a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.VARIANT.cs b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.VARIANT.cs index 8c43b943925..90606cbcaea 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.VARIANT.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/Ole32/Interop.VARIANT.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System; using System.Runtime.InteropServices; using System.Windows.Forms.Primitives.Resources; @@ -44,7 +42,7 @@ public void Clear() ~VARIANT() => Clear(); - public object ToObject() + public object? ToObject() { IntPtr val = data1; long longVal; @@ -167,7 +165,7 @@ public object ToObject() return (val != IntPtr.Zero); case VARENUM.VARIANT: - VARIANT varStruct = Marshal.PtrToStructure(val); + VARIANT? varStruct = Marshal.PtrToStructure(val); return varStruct?.ToObject(); case VARENUM.CLSID: diff --git a/src/System.Windows.Forms.Primitives/src/Interop/Shell32/Interop.SHGetPathFromIDListLongPath.cs b/src/System.Windows.Forms.Primitives/src/Interop/Shell32/Interop.SHGetPathFromIDListLongPath.cs index 2005b19806b..1b36cddc786 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/Shell32/Interop.SHGetPathFromIDListLongPath.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/Shell32/Interop.SHGetPathFromIDListLongPath.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System; using System.Runtime.InteropServices; @@ -14,7 +12,7 @@ internal static partial class Shell32 [DllImport(Libraries.Shell32, ExactSpelling = true)] private static extern bool SHGetPathFromIDListEx(IntPtr pidl, IntPtr pszPath, int cchPath, int flags); - public static bool SHGetPathFromIDListLongPath(IntPtr pidl, out string path) + public static bool SHGetPathFromIDListLongPath(IntPtr pidl, out string? path) { IntPtr pszPath = Marshal.AllocHGlobal((Kernel32.MAX_PATH + 1) * sizeof(char)); int length = Kernel32.MAX_PATH; diff --git a/src/System.Windows.Forms.Primitives/src/Interop/User32/Interop.GetClipboardFormatNameW.cs b/src/System.Windows.Forms.Primitives/src/Interop/User32/Interop.GetClipboardFormatNameW.cs index 089edfbb624..131fb04f74d 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/User32/Interop.GetClipboardFormatNameW.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/User32/Interop.GetClipboardFormatNameW.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System; using System.Runtime.InteropServices; using System.Windows.Forms; @@ -15,7 +13,7 @@ internal static partial class User32 [DllImport(Libraries.User32, ExactSpelling = true)] private static unsafe extern int GetClipboardFormatNameW(uint format, char* lpszFormatName, int cchMaxCount); - public static unsafe string GetClipboardFormatNameW(uint format) + public static unsafe string? GetClipboardFormatNameW(uint format) { // The max length of the name of clipboard formats is equal to the max length // of a Win32 Atom of 255 chars. An additional null terminator character is added, diff --git a/src/System.Windows.Forms.Primitives/src/System/ComponentModel/CompModSwitches.cs b/src/System.Windows.Forms.Primitives/src/System/ComponentModel/CompModSwitches.cs index b9efc1dd3c9..dd78fd8c0b8 100644 --- a/src/System.Windows.Forms.Primitives/src/System/ComponentModel/CompModSwitches.cs +++ b/src/System.Windows.Forms.Primitives/src/System/ComponentModel/CompModSwitches.cs @@ -2,47 +2,45 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Diagnostics; namespace System.ComponentModel { internal static class CompModSwitches { - private static TraceSwitch activeX; - private static TraceSwitch flowLayout; - private static TraceSwitch dataCursor; - private static TraceSwitch dataGridCursor; - private static TraceSwitch dataGridEditing; - private static TraceSwitch dataGridKeys; - private static TraceSwitch dataGridLayout; - private static TraceSwitch dataGridPainting; - private static TraceSwitch dataGridParents; - private static TraceSwitch dataGridScrolling; - private static TraceSwitch dataGridSelection; - private static TraceSwitch dataObject; - private static TraceSwitch dataView; - private static TraceSwitch debugGridView; - private static TraceSwitch dgCaptionPaint; - private static TraceSwitch dgEditColumnEditing; - private static TraceSwitch dgRelationShpRowLayout; - private static TraceSwitch dgRelationShpRowPaint; - private static TraceSwitch dgRowPaint; - private static TraceSwitch dragDrop; - private static TraceSwitch imeMode; - private static TraceSwitch msaa; - private static TraceSwitch msoComponentManager; - private static TraceSwitch layoutPerformance; - private static TraceSwitch layoutSuspendResume; - private static TraceSwitch richLayout; - private static TraceSwitch setBounds; - - private static BooleanSwitch lifetimeTracing; - - private static TraceSwitch s_handleLeak; - private static BooleanSwitch s_traceCollect; - private static BooleanSwitch s_commonDesignerServices; + private static TraceSwitch? activeX; + private static TraceSwitch? flowLayout; + private static TraceSwitch? dataCursor; + private static TraceSwitch? dataGridCursor; + private static TraceSwitch? dataGridEditing; + private static TraceSwitch? dataGridKeys; + private static TraceSwitch? dataGridLayout; + private static TraceSwitch? dataGridPainting; + private static TraceSwitch? dataGridParents; + private static TraceSwitch? dataGridScrolling; + private static TraceSwitch? dataGridSelection; + private static TraceSwitch? dataObject; + private static TraceSwitch? dataView; + private static TraceSwitch? debugGridView; + private static TraceSwitch? dgCaptionPaint; + private static TraceSwitch? dgEditColumnEditing; + private static TraceSwitch? dgRelationShpRowLayout; + private static TraceSwitch? dgRelationShpRowPaint; + private static TraceSwitch? dgRowPaint; + private static TraceSwitch? dragDrop; + private static TraceSwitch? imeMode; + private static TraceSwitch? msaa; + private static TraceSwitch? msoComponentManager; + private static TraceSwitch? layoutPerformance; + private static TraceSwitch? layoutSuspendResume; + private static TraceSwitch? richLayout; + private static TraceSwitch? setBounds; + + private static BooleanSwitch? lifetimeTracing; + + private static TraceSwitch? s_handleLeak; + private static BooleanSwitch? s_traceCollect; + private static BooleanSwitch? s_commonDesignerServices; public static TraceSwitch ActiveX { diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/FileDialogCustomPlace.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/FileDialogCustomPlace.cs index 6955e9bf6aa..ffa5d0856bd 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/FileDialogCustomPlace.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/FileDialogCustomPlace.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using static Interop; namespace System.Windows.Forms @@ -60,7 +58,7 @@ public override string ToString() /// to an actual filesystem directory. /// The caller is responsible for handling these situations. /// - internal FileDialogNative.IShellItem GetNativePath() + internal FileDialogNative.IShellItem? GetNativePath() { string filePathString; if (!string.IsNullOrEmpty(_path)) diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/FileDialogCustomPlacesCollection.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/FileDialogCustomPlacesCollection.cs index 9ee2816c1aa..1a73663cab2 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/FileDialogCustomPlacesCollection.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/FileDialogCustomPlacesCollection.cs @@ -18,7 +18,7 @@ internal void Apply(FileDialogNative.IFileDialog dialog) try { - FileDialogNative.IShellItem shellItem = customPlace.GetNativePath(); + FileDialogNative.IShellItem? shellItem = customPlace.GetNativePath(); if (null != shellItem) { dialog.AddPlace(shellItem, 0); diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/ClientUtils.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/ClientUtils.cs index da365447da3..4542f907715 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/ClientUtils.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/ClientUtils.cs @@ -2,10 +2,9 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Collections; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Globalization; namespace System.Windows.Forms @@ -158,7 +157,7 @@ public static int GetWordBoundaryStart(char[] text, int endIndex) #if DEBUG [ThreadStatic] - private static Hashtable enumValueInfo; + private static Hashtable? enumValueInfo; public const int MAXCACHE = 300; // we think we're going to get O(100) of these, put in a tripwire if it gets larger. private class SequentialEnumInfo @@ -169,7 +168,9 @@ public SequentialEnumInfo(Type t) int actualMaximum = int.MinValue; int countEnumVals = 0; +#pragma warning disable CS8605 // Unboxing a possibly null value. foreach (int iVal in Enum.GetValues(t)) +#pragma warning restore CS8605 // Unboxing a possibly null value. { actualMinimum = Math.Min(actualMinimum, iVal); actualMaximum = Math.Max(actualMaximum, iVal); @@ -196,7 +197,7 @@ private static void Debug_SequentialEnumIsDefinedCheck(Enum value, int minVal, i enumValueInfo = new Hashtable(); } - SequentialEnumInfo sequentialEnumInfo = null; + SequentialEnumInfo? sequentialEnumInfo = null; if (enumValueInfo.ContainsKey(t)) { @@ -230,7 +231,9 @@ private static void Debug_ValidateMask(Enum value, uint mask) { Type t = value.GetType(); uint newmask = 0; +#pragma warning disable CS8605 // Unboxing a possibly null value. foreach (int iVal in Enum.GetValues(t)) +#pragma warning restore CS8605 // Unboxing a possibly null value. { newmask |= (uint)iVal; } @@ -244,7 +247,9 @@ private static void Debug_NonSequentialEnumIsDefinedCheck(Enum value, int minVal int checkedValue = Convert.ToInt32(value, CultureInfo.InvariantCulture); int maxBitsFound = 0; bool foundValue = false; +#pragma warning disable CS8605 // Unboxing a possibly null value. foreach (int iVal in Enum.GetValues(t)){ +#pragma warning restore CS8605 // Unboxing a possibly null value. actualMinimum = Math.Min(actualMinimum, iVal); actualMaximum = Math.Max(actualMaximum, iVal); maxBitsFound = Math.Max(maxBitsFound, GetBitCount((uint)iVal)); @@ -307,7 +312,7 @@ public WeakRefCollection(int size) /// public int RefCheckThreshold { get; set; } = int.MaxValue; // this means this is disabled by default. - public object this[int index] + public object? this[int index] { get { @@ -327,7 +332,7 @@ public void ScavengeReferences() int currentCount = Count; for (int i = 0; i < currentCount; i++) { - object item = this[currentIndex]; + object? item = this[currentIndex]; if (item == null) { @@ -341,9 +346,9 @@ public void ScavengeReferences() } } - public override bool Equals(object obj) + public override bool Equals(object? obj) { - WeakRefCollection other = obj as WeakRefCollection; + WeakRefCollection? other = obj as WeakRefCollection; if (other == this) { return true; @@ -358,7 +363,7 @@ public override bool Equals(object obj) { if (InnerList[i] != other.InnerList[i]) { - if (InnerList[i] == null || !InnerList[i].Equals(other.InnerList[i])) + if (InnerList[i] == null || !InnerList[i]!.Equals(other.InnerList[i])) { return false; } @@ -371,7 +376,7 @@ public override bool Equals(object obj) public override int GetHashCode() { var hash = new HashCode(); - foreach (object o in InnerList) + foreach (object? o in InnerList) { hash.Add(o); } @@ -379,7 +384,8 @@ public override int GetHashCode() return hash.ToHashCode(); } - private WeakRefObject CreateWeakRefObject(object value) + [return: NotNullIfNotNull("value")] + private WeakRefObject? CreateWeakRefObject(object? value) { if (value == null) { @@ -428,7 +434,7 @@ public void RemoveByHashCode(object value) int hash = value.GetHashCode(); for (int idx = 0; idx < InnerList.Count; idx++) { - if (InnerList[idx] != null && InnerList[idx].GetHashCode() == hash) + if (InnerList[idx] != null && InnerList[idx]!.GetHashCode() == hash) { RemoveAt(idx); return; @@ -440,17 +446,17 @@ public void RemoveByHashCode(object value) public bool IsFixedSize => InnerList.IsFixedSize; - public bool Contains(object value) => InnerList.Contains(CreateWeakRefObject(value)); + public bool Contains(object? value) => InnerList.Contains(CreateWeakRefObject(value)); public void RemoveAt(int index) => InnerList.RemoveAt(index); - public void Remove(object value) => InnerList.Remove(CreateWeakRefObject(value)); + public void Remove(object? value) => InnerList.Remove(CreateWeakRefObject(value)); - public int IndexOf(object value) => InnerList.IndexOf(CreateWeakRefObject(value)); + public int IndexOf(object? value) => InnerList.IndexOf(CreateWeakRefObject(value)); - public void Insert(int index, object value) => InnerList.Insert(index, CreateWeakRefObject(value)); + public void Insert(int index, object? value) => InnerList.Insert(index, CreateWeakRefObject(value)); - public int Add(object value) + public int Add(object? value) { if (Count > RefCheckThreshold) { @@ -495,7 +501,7 @@ internal bool IsAlive get { return weakHolder.IsAlive; } } - internal object Target + internal object? Target { get { @@ -505,9 +511,9 @@ internal object Target public override int GetHashCode() => hash; - public override bool Equals(object obj) + public override bool Equals(object? obj) { - WeakRefObject other = obj as WeakRefObject; + WeakRefObject? other = obj as WeakRefObject; if (other == this) { diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/DpiHelper.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/DpiHelper.cs index 9c10c34d2c4..a4f434db865 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/DpiHelper.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/DpiHelper.cs @@ -2,8 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - +using System.Diagnostics.CodeAnalysis; using System.Drawing; using System.Drawing.Drawing2D; using static Interop; @@ -289,7 +288,8 @@ public static Size LogicalToDeviceUnits(Size logicalSize, int deviceDpi = 0) /// /// The image to scale from logical units to device units /// The size to scale image to - public static Bitmap CreateResizedBitmap(Bitmap logicalImage, Size targetImageSize) + [return: NotNullIfNotNull("logicalImage")] + public static Bitmap? CreateResizedBitmap(Bitmap? logicalImage, Size targetImageSize) { if (logicalImage == null) { diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/FileDialog_Vista_Interop.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/FileDialog_Vista_Interop.cs index 9e2fcc7dd2c..fed2f3cd215 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/FileDialog_Vista_Interop.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/FileDialog_Vista_Interop.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.IO; using System.Runtime.InteropServices; @@ -13,7 +11,7 @@ internal static class FileDialogNative { internal static IShellItem GetShellItemForPath(string path) { - IShellItem ret = null; + IShellItem? ret = null; IntPtr pidl = IntPtr.Zero; uint zero = 0; if (UnsafeNativeMethods.Shell32.SHILCreateFromPath(path, out pidl, ref zero) >= 0) diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/MessageDecoder.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/MessageDecoder.cs index 891a2878bd6..a7426ba79ff 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/MessageDecoder.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/MessageDecoder.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using static Interop; namespace System.Windows.Forms @@ -18,9 +16,9 @@ internal static class MessageDecoder /// Returns the symbolic name of the msg value, or null if it isn't one of the /// existing constants. /// - private static string MsgToString(int msg) + private static string? MsgToString(int msg) { - string text; + string? text; switch (msg) { case WindowMessages.WM_NULL: @@ -840,7 +838,7 @@ private static string MsgToString(int msg) return text; } - private static string Parenthesize(string input) + private static string Parenthesize(string? input) { if (input == null) { diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/NativeMethods.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/NativeMethods.cs index f94a85221de..b4c3bd998e8 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/NativeMethods.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/NativeMethods.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Drawing; using System.Runtime.InteropServices; using static Interop; @@ -432,11 +430,11 @@ public class HH_AKLINK { internal int cbStruct = Marshal.SizeOf(); internal bool fReserved = false; - internal string pszKeywords = null; - internal string pszUrl = null; - internal string pszMsgText = null; - internal string pszMsgTitle = null; - internal string pszWindow = null; + internal string? pszKeywords = null; + internal string? pszUrl = null; + internal string? pszMsgText = null; + internal string? pszMsgTitle = null; + internal string? pszWindow = null; internal bool fIndexOnFail = false; } @@ -451,7 +449,7 @@ public class HH_POPUP internal int clrForeground = -1; internal int clrBackground = -1; internal RECT rcMargins = new RECT(-1, -1, -1, -1); // amount of space between edges of window and text, -1 for each member to ignore - internal string pszFont = null; + internal string? pszFont = null; } public const int HH_FTS_DEFAULT_PROXIMITY = -1; @@ -462,13 +460,13 @@ public class HH_FTS_QUERY internal int cbStruct = Marshal.SizeOf(); internal bool fUniCodeStrings = false; [MarshalAs(UnmanagedType.LPStr)] - internal string pszSearchQuery = null; + internal string? pszSearchQuery = null; internal int iProximity = NativeMethods.HH_FTS_DEFAULT_PROXIMITY; internal bool fStemmedSearch = false; internal bool fTitleOnly = false; internal bool fExecute = true; [MarshalAs(UnmanagedType.LPStr)] - internal string pszWindow = null; + internal string? pszWindow = null; } public delegate int EditStreamCallback(IntPtr dwCookie, IntPtr buf, int cb, out int transferred); @@ -478,7 +476,7 @@ public class EDITSTREAM { public IntPtr dwCookie = IntPtr.Zero; public int dwError = 0; - public EditStreamCallback pfnCallback = null; + public EditStreamCallback? pfnCallback = null; } [StructLayout(LayoutKind.Sequential)] @@ -538,11 +536,11 @@ public interface PRINTDLG IntPtr hInstance { get; set; } IntPtr lCustData { get; set; } - WndProc lpfnPrintHook { get; set; } - WndProc lpfnSetupHook { get; set; } + WndProc? lpfnPrintHook { get; set; } + WndProc? lpfnSetupHook { get; set; } - string lpPrintTemplateName { get; set; } - string lpSetupTemplateName { get; set; } + string? lpPrintTemplateName { get; set; } + string? lpSetupTemplateName { get; set; } IntPtr hPrintTemplate { get; set; } IntPtr hSetupTemplate { get; set; } @@ -569,11 +567,11 @@ public class PRINTDLG_32 : PRINTDLG IntPtr m_hInstance; IntPtr m_lCustData; - WndProc m_lpfnPrintHook; - WndProc m_lpfnSetupHook; + WndProc? m_lpfnPrintHook; + WndProc? m_lpfnSetupHook; - string m_lpPrintTemplateName; - string m_lpSetupTemplateName; + string? m_lpPrintTemplateName; + string? m_lpSetupTemplateName; IntPtr m_hPrintTemplate; IntPtr m_hSetupTemplate; @@ -596,11 +594,11 @@ public class PRINTDLG_32 : PRINTDLG public IntPtr hInstance { get { return m_hInstance; } set { m_hInstance = value; } } public IntPtr lCustData { get { return m_lCustData; } set { m_lCustData = value; } } - public WndProc lpfnPrintHook { get { return m_lpfnPrintHook; } set { m_lpfnPrintHook = value; } } - public WndProc lpfnSetupHook { get { return m_lpfnSetupHook; } set { m_lpfnSetupHook = value; } } + public WndProc? lpfnPrintHook { get { return m_lpfnPrintHook; } set { m_lpfnPrintHook = value; } } + public WndProc? lpfnSetupHook { get { return m_lpfnSetupHook; } set { m_lpfnSetupHook = value; } } - public string lpPrintTemplateName { get { return m_lpPrintTemplateName; } set { m_lpPrintTemplateName = value; } } - public string lpSetupTemplateName { get { return m_lpSetupTemplateName; } set { m_lpSetupTemplateName = value; } } + public string? lpPrintTemplateName { get { return m_lpPrintTemplateName; } set { m_lpPrintTemplateName = value; } } + public string? lpSetupTemplateName { get { return m_lpSetupTemplateName; } set { m_lpSetupTemplateName = value; } } public IntPtr hPrintTemplate { get { return m_hPrintTemplate; } set { m_hPrintTemplate = value; } } public IntPtr hSetupTemplate { get { return m_hSetupTemplate; } set { m_hSetupTemplate = value; } } @@ -627,11 +625,11 @@ public class PRINTDLG_64 : PRINTDLG IntPtr m_hInstance; IntPtr m_lCustData; - WndProc m_lpfnPrintHook; - WndProc m_lpfnSetupHook; + WndProc? m_lpfnPrintHook; + WndProc? m_lpfnSetupHook; - string m_lpPrintTemplateName; - string m_lpSetupTemplateName; + string? m_lpPrintTemplateName; + string? m_lpSetupTemplateName; IntPtr m_hPrintTemplate; IntPtr m_hSetupTemplate; @@ -654,11 +652,11 @@ public class PRINTDLG_64 : PRINTDLG public IntPtr hInstance { get { return m_hInstance; } set { m_hInstance = value; } } public IntPtr lCustData { get { return m_lCustData; } set { m_lCustData = value; } } - public WndProc lpfnPrintHook { get { return m_lpfnPrintHook; } set { m_lpfnPrintHook = value; } } - public WndProc lpfnSetupHook { get { return m_lpfnSetupHook; } set { m_lpfnSetupHook = value; } } + public WndProc? lpfnPrintHook { get { return m_lpfnPrintHook; } set { m_lpfnPrintHook = value; } } + public WndProc? lpfnSetupHook { get { return m_lpfnSetupHook; } set { m_lpfnSetupHook = value; } } - public string lpPrintTemplateName { get { return m_lpPrintTemplateName; } set { m_lpPrintTemplateName = value; } } - public string lpSetupTemplateName { get { return m_lpSetupTemplateName; } set { m_lpSetupTemplateName = value; } } + public string? lpPrintTemplateName { get { return m_lpPrintTemplateName; } set { m_lpPrintTemplateName = value; } } + public string? lpSetupTemplateName { get { return m_lpSetupTemplateName; } set { m_lpSetupTemplateName = value; } } public IntPtr hPrintTemplate { get { return m_hPrintTemplate; } set { m_hPrintTemplate = value; } } public IntPtr hSetupTemplate { get { return m_hSetupTemplate; } set { m_hSetupTemplate = value; } } @@ -690,9 +688,9 @@ public class PRINTDLGEX public IntPtr hInstance; [MarshalAs(UnmanagedType.LPStr)] - public string lpPrintTemplateName; + public string? lpPrintTemplateName; - public WndProc lpCallback = null; + public WndProc? lpCallback = null; public int nPropertyPages; @@ -720,14 +718,14 @@ public class NOTIFYICONDATA public int uCallbackMessage; public IntPtr hIcon; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] - public string szTip; + public string? szTip; public int dwState = 0; public int dwStateMask = 0; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] - public string szInfo; + public string? szInfo; public int uTimeoutOrVersion; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)] - public string szInfoTitle; + public string? szInfoTitle; public int dwInfoFlags; } @@ -743,7 +741,7 @@ public class MENUITEMINFO_T public IntPtr hbmpChecked; public IntPtr hbmpUnchecked; public IntPtr dwItemData; - public string dwTypeData; + public string? dwTypeData; public int cch; } @@ -788,7 +786,7 @@ public class OPENFILENAME_I public int lStructSize = Marshal.SizeOf(); //ndirect.DllLib.sizeOf(this); public IntPtr hwndOwner; public IntPtr hInstance; - public string lpstrFilter; // use embedded nulls to separate filters + public string? lpstrFilter; // use embedded nulls to separate filters public IntPtr lpstrCustomFilter = IntPtr.Zero; public int nMaxCustFilter = 0; public int nFilterIndex; @@ -796,15 +794,15 @@ public class OPENFILENAME_I public int nMaxFile = Kernel32.MAX_PATH; public IntPtr lpstrFileTitle = IntPtr.Zero; public int nMaxFileTitle = Kernel32.MAX_PATH; - public string lpstrInitialDir; - public string lpstrTitle; + public string? lpstrInitialDir; + public string? lpstrTitle; public int Flags; public short nFileOffset = 0; public short nFileExtension = 0; - public string lpstrDefExt; + public string? lpstrDefExt; public IntPtr lCustData = IntPtr.Zero; - public WndProc lpfnHook; - public string lpTemplateName = null; + public WndProc? lpfnHook; + public string? lpTemplateName = null; public IntPtr pvReserved = IntPtr.Zero; public int dwReserved = 0; public int FlagsEx; @@ -821,10 +819,10 @@ public class CHOOSEFONT public Comdlg32.CF Flags; public int rgbColors; public IntPtr lCustData = IntPtr.Zero; - public WndProc lpfnHook; - public string lpTemplateName = null; + public WndProc? lpfnHook; + public string? lpTemplateName = null; public IntPtr hInstance; - public string lpszStyle = null; + public string? lpszStyle = null; public short nFontType = 0; public short ___MISSING_ALIGNMENT__ = 0; public int nSizeMin; @@ -861,10 +859,10 @@ public override string ToString() public class TOOLTIPTEXT { public User32.NMHDR hdr; - public string lpszText; + public string? lpszText; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] - public string szText = null; + public string? szText = null; public IntPtr hinst; public int uFlags; @@ -979,14 +977,14 @@ public class PARAFORMAT public short cTabCount; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] - public int[] rgxTabs; + public int[]? rgxTabs; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public class FINDTEXT { public Richedit.CHARRANGE chrg; - public string lpstrText; + public string? lpstrText; } [StructLayout(LayoutKind.Sequential)] diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/UnsafeNativeMethods.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/UnsafeNativeMethods.cs index 8a853223824..10023b27718 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/UnsafeNativeMethods.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/UnsafeNativeMethods.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Diagnostics; using System.Drawing; using System.Runtime.CompilerServices; @@ -52,7 +50,7 @@ public static bool PrintDlg([In, Out] NativeMethods.PRINTDLG lppd) public static extern int PrintDlgEx([In, Out] NativeMethods.PRINTDLGEX lppdex); [DllImport(ExternDll.Shell32, CharSet = CharSet.Auto)] - public static extern int DragQueryFile(HandleRef hDrop, int iFile, StringBuilder lpszFile, int cch); + public static extern int DragQueryFile(HandleRef hDrop, int iFile, StringBuilder? lpszFile, int cch); public static int DragQueryFileLongPath(HandleRef hDrop, int iFile, StringBuilder lpszFile) { @@ -333,9 +331,9 @@ public static StringBuilder GetModuleFileNameLongPath(HandleRef hModule) [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)] public static extern int GetRegionData(HandleRef hRgn, int size, IntPtr lpRgnData); - public unsafe static RECT[] GetRectsFromRegion(IntPtr hRgn) + public unsafe static RECT[]? GetRectsFromRegion(IntPtr hRgn) { - RECT[] regionRects = null; + RECT[]? regionRects = null; IntPtr pBytes = IntPtr.Zero; try { diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Message.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Message.cs index 24e2ee97b28..a992bfaa39b 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Message.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Message.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Diagnostics; using System.Runtime.InteropServices; using static Interop; @@ -38,7 +36,7 @@ public struct Message /// /// Gets the value, and converts the value to an object. /// - public object GetLParam(Type cls) => Marshal.PtrToStructure(LParam, cls); + public object? GetLParam(Type cls) => Marshal.PtrToStructure(LParam, cls); internal static Message Create(IntPtr hWnd, User32.WindowMessage msg, IntPtr wparam, IntPtr lparam) => Create(hWnd, (int)msg, wparam, lparam); @@ -66,7 +64,7 @@ public static Message Create(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam) return m; } - public override bool Equals(object o) + public override bool Equals(object? o) { if (!(o is Message m)) { diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Padding.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Padding.cs index 33a57d4fe0a..5de20edf938 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Padding.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Padding.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.ComponentModel; using System.Diagnostics; using System.Drawing; @@ -132,7 +130,7 @@ public int Top public static Padding Subtract(Padding p1, Padding p2) => p1 - p2; - public override bool Equals(object other) + public override bool Equals(object? other) { if (!(other is Padding otherPadding)) { diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/PaddingConverter.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/PaddingConverter.cs index 7331cf5053a..ae20964befd 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/PaddingConverter.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/PaddingConverter.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Collections; using System.ComponentModel; using System.ComponentModel.Design.Serialization; @@ -41,7 +39,7 @@ public override bool CanConvertTo(ITypeDescriptorContext context, Type destinati /// /// Converts the given object to the converter's native type. /// - public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) + public override object? ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string stringValue) { @@ -136,7 +134,7 @@ public override object CreateInstance(ITypeDescriptorContext context, IDictionar Padding original = (Padding)context.PropertyDescriptor.GetValue(context.Instance); try { - int all = (int)propertyValues[nameof(Padding.All)]; + int all = (int)propertyValues[nameof(Padding.All)]!; if (original.All != all) { return new Padding(all); @@ -144,10 +142,10 @@ public override object CreateInstance(ITypeDescriptorContext context, IDictionar else { return new Padding( - (int)propertyValues[nameof(Padding.Left)], - (int)propertyValues[nameof(Padding.Top)], - (int)propertyValues[nameof(Padding.Right)], - (int)propertyValues[nameof(Padding.Bottom)] + (int)propertyValues[nameof(Padding.Left)]!, + (int)propertyValues[nameof(Padding.Top)]!, + (int)propertyValues[nameof(Padding.Right)]!, + (int)propertyValues[nameof(Padding.Bottom)]! ); } } From e50cb60bd0571c4f2eed0cb2da720aa5430bd28b Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Thu, 9 Jan 2020 08:35:05 -0800 Subject: [PATCH 4/7] Enable nullable reference types for System.Windows.Forms.Primitives.Tests --- .../tests/Interop/Mso/IMsoComponentManagerTests.cs | 2 ++ .../tests/Interop/User32/GetWindowTextTests.cs | 2 ++ .../tests/System.Windows.Forms.Primitives.Tests.csproj | 1 + 3 files changed, 5 insertions(+) diff --git a/src/System.Windows.Forms.Primitives/tests/Interop/Mso/IMsoComponentManagerTests.cs b/src/System.Windows.Forms.Primitives/tests/Interop/Mso/IMsoComponentManagerTests.cs index 9f7c93bfc7e..97296d8b0fd 100644 --- a/src/System.Windows.Forms.Primitives/tests/Interop/Mso/IMsoComponentManagerTests.cs +++ b/src/System.Windows.Forms.Primitives/tests/Interop/Mso/IMsoComponentManagerTests.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable disable + using System.Runtime.InteropServices; using Moq; using Xunit; diff --git a/src/System.Windows.Forms.Primitives/tests/Interop/User32/GetWindowTextTests.cs b/src/System.Windows.Forms.Primitives/tests/Interop/User32/GetWindowTextTests.cs index a98474140ef..f8b61d53a36 100644 --- a/src/System.Windows.Forms.Primitives/tests/Interop/User32/GetWindowTextTests.cs +++ b/src/System.Windows.Forms.Primitives/tests/Interop/User32/GetWindowTextTests.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable disable + using Xunit; using static Interop; using static Interop.User32; diff --git a/src/System.Windows.Forms.Primitives/tests/System.Windows.Forms.Primitives.Tests.csproj b/src/System.Windows.Forms.Primitives/tests/System.Windows.Forms.Primitives.Tests.csproj index 5f7dc1e0b12..9df3e7e3e48 100644 --- a/src/System.Windows.Forms.Primitives/tests/System.Windows.Forms.Primitives.Tests.csproj +++ b/src/System.Windows.Forms.Primitives/tests/System.Windows.Forms.Primitives.Tests.csproj @@ -3,6 +3,7 @@ System.Windows.Forms.Primitives.Tests true + enable From a445c6ec46177f79ecdb82b833bc1e758398fce2 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Thu, 9 Jan 2020 08:35:56 -0800 Subject: [PATCH 5/7] Annotate files for nullability where required for a clean build --- .../tests/Interop/Mso/IMsoComponentManagerTests.cs | 6 ++---- .../tests/Interop/User32/GetWindowTextTests.cs | 10 ++++------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/System.Windows.Forms.Primitives/tests/Interop/Mso/IMsoComponentManagerTests.cs b/src/System.Windows.Forms.Primitives/tests/Interop/Mso/IMsoComponentManagerTests.cs index 97296d8b0fd..204a3c62333 100644 --- a/src/System.Windows.Forms.Primitives/tests/Interop/Mso/IMsoComponentManagerTests.cs +++ b/src/System.Windows.Forms.Primitives/tests/Interop/Mso/IMsoComponentManagerTests.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System.Runtime.InteropServices; using Moq; using Xunit; @@ -16,8 +14,8 @@ public unsafe class IMsoComponentManagerTests { private IMsoComponentManager CreateComponentManager() => (IMsoComponentManager)Activator.CreateInstance( - typeof(Application).Assembly.GetType("System.Windows.Forms.Application+ComponentManager"), - nonPublic: true); + typeof(Application).Assembly.GetType("System.Windows.Forms.Application+ComponentManager")!, + nonPublic: true)!; [Fact] public void FDebugMessage_ReturnsTrue() diff --git a/src/System.Windows.Forms.Primitives/tests/Interop/User32/GetWindowTextTests.cs b/src/System.Windows.Forms.Primitives/tests/Interop/User32/GetWindowTextTests.cs index f8b61d53a36..d99f1a2fd85 100644 --- a/src/System.Windows.Forms.Primitives/tests/Interop/User32/GetWindowTextTests.cs +++ b/src/System.Windows.Forms.Primitives/tests/Interop/User32/GetWindowTextTests.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using Xunit; using static Interop; using static Interop.User32; @@ -52,13 +50,13 @@ private void CallGetWindowText(bool useBeforeGetTextLengthCallback) private class ChangeWindowTextForm : Form { - public Func BeforeGetTextLengthCallback + public Func? BeforeGetTextLengthCallback { get; set; } - public Func BeforeGetTextCallback + public Func? BeforeGetTextCallback { get; set; @@ -68,7 +66,7 @@ protected override void WndProc(ref Message m) { if (m.Msg == WindowMessages.WM_GETTEXTLENGTH) { - string text = BeforeGetTextLengthCallback?.Invoke(); + string? text = BeforeGetTextLengthCallback?.Invoke(); if (text != null) { SetWindowTextW(m.HWnd, text); @@ -76,7 +74,7 @@ protected override void WndProc(ref Message m) } else if (m.Msg == WindowMessages.WM_GETTEXT) { - string text = BeforeGetTextCallback?.Invoke(); + string? text = BeforeGetTextCallback?.Invoke(); if (text != null) { SetWindowTextW(m.HWnd, text); From 7f552c6453510ec92846550517df035299b2edae Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Thu, 9 Jan 2020 08:49:02 -0800 Subject: [PATCH 6/7] Mark struct fields nullable to account for default instances --- .../src/Interop/ComCtl32/Interop.MCGRIDINFO.cs | 2 +- .../src/Interop/ComCtl32/Interop.TTOOLINFOW.cs | 2 ++ .../src/Interop/Gdi32/Interop.BITMAPINFO.cs | 2 +- .../src/Interop/OleAut32/Interop.FONTDESC.cs | 2 +- .../src/Interop/Shell32/Interop.SHBrowseForFolder.cs | 6 +++--- .../src/Interop/User32/Interop.DEVMODEW.cs | 4 ++-- .../Windows/Forms/Internals/FileDialog_Vista_Interop.cs | 4 ++-- .../src/System/Windows/Forms/Internals/NativeMethods.cs | 2 +- 8 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/System.Windows.Forms.Primitives/src/Interop/ComCtl32/Interop.MCGRIDINFO.cs b/src/System.Windows.Forms.Primitives/src/Interop/ComCtl32/Interop.MCGRIDINFO.cs index 6406b870904..f383c016b45 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/ComCtl32/Interop.MCGRIDINFO.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/ComCtl32/Interop.MCGRIDINFO.cs @@ -27,7 +27,7 @@ public unsafe struct MCGRIDINFO public Kernel32.SYSTEMTIME stStart; public Kernel32.SYSTEMTIME stEnd; public RECT rc; - public string pszName; + public string? pszName; public uint cchName; } diff --git a/src/System.Windows.Forms.Primitives/src/Interop/ComCtl32/Interop.TTOOLINFOW.cs b/src/System.Windows.Forms.Primitives/src/Interop/ComCtl32/Interop.TTOOLINFOW.cs index f83dbc36dcd..0de632701f2 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/ComCtl32/Interop.TTOOLINFOW.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/ComCtl32/Interop.TTOOLINFOW.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; internal static partial class Interop @@ -42,6 +43,7 @@ public struct ToolInfoWrapper { public TTOOLINFOW Info; public string? Text { get; set; } + [MaybeNull] private readonly T _handle; public unsafe ToolInfoWrapper(T handle, TTF flags = default, string? text = null) diff --git a/src/System.Windows.Forms.Primitives/src/Interop/Gdi32/Interop.BITMAPINFO.cs b/src/System.Windows.Forms.Primitives/src/Interop/Gdi32/Interop.BITMAPINFO.cs index 95c894032f5..311e3739f0f 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/Gdi32/Interop.BITMAPINFO.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/Gdi32/Interop.BITMAPINFO.cs @@ -16,7 +16,7 @@ public struct BITMAPINFO public BITMAPINFOHEADER bmiHeader; [MarshalAs(UnmanagedType.ByValArray, SizeConst = MaxColorSize * 4)] - public byte[] bmiColors; // RGBQUAD structs: repeating Blue-Green-Red-Reserved + public byte[]? bmiColors; // RGBQUAD structs: repeating Blue-Green-Red-Reserved } } } diff --git a/src/System.Windows.Forms.Primitives/src/Interop/OleAut32/Interop.FONTDESC.cs b/src/System.Windows.Forms.Primitives/src/Interop/OleAut32/Interop.FONTDESC.cs index ec41290df97..462d969fbfa 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/OleAut32/Interop.FONTDESC.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/OleAut32/Interop.FONTDESC.cs @@ -13,7 +13,7 @@ internal static partial class Oleaut32 public struct FONTDESC { public uint cbSizeOfStruct; - public string lpstrName; + public string? lpstrName; public long cySize; public short sWeight; public short sCharset; diff --git a/src/System.Windows.Forms.Primitives/src/Interop/Shell32/Interop.SHBrowseForFolder.cs b/src/System.Windows.Forms.Primitives/src/Interop/Shell32/Interop.SHBrowseForFolder.cs index bf5302b144d..ff14b01570d 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/Shell32/Interop.SHBrowseForFolder.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/Shell32/Interop.SHBrowseForFolder.cs @@ -34,15 +34,15 @@ public unsafe struct BROWSEINFO { public IntPtr hwndOwner; - public CoTaskMemSafeHandle pidlRoot; + public CoTaskMemSafeHandle? pidlRoot; public char* pszDisplayName; - public string lpszTitle; + public string? lpszTitle; public uint ulFlags; - public BrowseCallbackProc lpfn; + public BrowseCallbackProc? lpfn; public IntPtr lParam; diff --git a/src/System.Windows.Forms.Primitives/src/Interop/User32/Interop.DEVMODEW.cs b/src/System.Windows.Forms.Primitives/src/Interop/User32/Interop.DEVMODEW.cs index a825ccfbe10..7f61dec940c 100644 --- a/src/System.Windows.Forms.Primitives/src/Interop/User32/Interop.DEVMODEW.cs +++ b/src/System.Windows.Forms.Primitives/src/Interop/User32/Interop.DEVMODEW.cs @@ -17,7 +17,7 @@ public struct DEVMODEW private const int CCHFORMNAME = 32; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)] - public string dmDeviceName; + public string? dmDeviceName; public ushort dmSpecVersion; public ushort dmDriverVersion; public ushort dmSize; @@ -33,7 +33,7 @@ public struct DEVMODEW public short dmTTOption; public short dmCollate; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)] - public string dmFormName; + public string? dmFormName; public ushort dmLogPixels; public uint dmBitsPerPel; public uint dmPelsWidth; diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/FileDialog_Vista_Interop.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/FileDialog_Vista_Interop.cs index fed2f3cd215..9a88c5b0e6d 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/FileDialog_Vista_Interop.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/FileDialog_Vista_Interop.cs @@ -411,9 +411,9 @@ public enum SIGDN : uint public struct COMDLG_FILTERSPEC { [MarshalAs(UnmanagedType.LPWStr)] - public string pszName; + public string? pszName; [MarshalAs(UnmanagedType.LPWStr)] - public string pszSpec; + public string? pszSpec; } [Flags] diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/NativeMethods.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/NativeMethods.cs index b4c3bd998e8..e0265c8b33f 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/NativeMethods.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/NativeMethods.cs @@ -770,7 +770,7 @@ public struct MSAAMENUINFO { public int dwMSAASignature; public int cchWText; - public string pszWText; + public string? pszWText; public MSAAMENUINFO(string text) { From 79169069a6e66ea623004ace710485a6e40a7995 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Thu, 9 Jan 2020 09:02:55 -0800 Subject: [PATCH 7/7] Review public APIs for nullability --- .../Windows/Forms/FileDialogCustomPlace.cs | 4 +++- .../Forms/FileDialogCustomPlacesCollection.cs | 2 +- .../src/System/Windows/Forms/PaddingConverter.cs | 16 ++++++++-------- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/FileDialogCustomPlace.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/FileDialogCustomPlace.cs index ffa5d0856bd..f2f97edcdce 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/FileDialogCustomPlace.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/FileDialogCustomPlace.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Diagnostics.CodeAnalysis; using static Interop; namespace System.Windows.Forms @@ -18,7 +19,7 @@ public class FileDialogCustomPlace private string _path = string.Empty; private Guid _knownFolderGuid = Guid.Empty; - public FileDialogCustomPlace(string path) + public FileDialogCustomPlace(string? path) { Path = path; } @@ -28,6 +29,7 @@ public FileDialogCustomPlace(Guid knownFolderGuid) KnownFolderGuid = knownFolderGuid; } + [AllowNull] public string Path { get => _path ?? string.Empty; diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/FileDialogCustomPlacesCollection.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/FileDialogCustomPlacesCollection.cs index 1a73663cab2..2742636796c 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/FileDialogCustomPlacesCollection.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/FileDialogCustomPlacesCollection.cs @@ -32,7 +32,7 @@ internal void Apply(FileDialogNative.IFileDialog dialog) } } - public void Add(string path) => Add(new FileDialogCustomPlace(path)); + public void Add(string? path) => Add(new FileDialogCustomPlace(path)); public void Add(Guid knownFolderGuid) => Add(new FileDialogCustomPlace(knownFolderGuid)); } diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/PaddingConverter.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/PaddingConverter.cs index ae20964befd..43f719994a8 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/PaddingConverter.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/PaddingConverter.cs @@ -16,7 +16,7 @@ public class PaddingConverter : TypeConverter /// Determines if this converter can convert an object in the given source type to /// the native type of the converter. /// - public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) + public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) { if (sourceType == typeof(string)) { @@ -26,7 +26,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT return base.CanConvertFrom(context, sourceType); } - public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) + public override bool CanConvertTo(ITypeDescriptorContext? context, Type destinationType) { if (destinationType == typeof(InstanceDescriptor)) { @@ -39,7 +39,7 @@ public override bool CanConvertTo(ITypeDescriptorContext context, Type destinati /// /// Converts the given object to the converter's native type. /// - public override object? ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) + public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value) { if (value is string stringValue) { @@ -75,7 +75,7 @@ public override bool CanConvertTo(ITypeDescriptorContext context, Type destinati return base.ConvertFrom(context, culture, value); } - public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) + public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType) { if (value is Padding padding) { @@ -120,7 +120,7 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul return base.ConvertTo(context, culture, value, destinationType); } - public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues) + public override object CreateInstance(ITypeDescriptorContext? context, IDictionary propertyValues) { if (context == null) { @@ -159,14 +159,14 @@ public override object CreateInstance(ITypeDescriptorContext context, IDictionar } } - public override bool GetCreateInstanceSupported(ITypeDescriptorContext context) => true; + public override bool GetCreateInstanceSupported(ITypeDescriptorContext? context) => true; - public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) + public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext? context, object value, Attribute[] attributes) { PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(Padding), attributes); return props.Sort(new string[] { nameof(Padding.All), nameof(Padding.Left), nameof(Padding.Top), nameof(Padding.Right), nameof(Padding.Bottom) }); } - public override bool GetPropertiesSupported(ITypeDescriptorContext context) => true; + public override bool GetPropertiesSupported(ITypeDescriptorContext? context) => true; } }