Enable nullable annotations for System.Drawing.Primitives (dotnet/corefx#41188)
authorMaryam Ariyan <maryam.ariyan@microsoft.com>
Fri, 20 Sep 2019 12:28:52 +0000 (05:28 -0700)
committerGitHub <noreply@github.com>
Fri, 20 Sep 2019 12:28:52 +0000 (05:28 -0700)
Commit migrated from https://github.com/dotnet/corefx/commit/aa72982dcb20c4e346e08d72dae5abbaf6bba4f9

13 files changed:
src/libraries/Common/src/System/Drawing/ColorConverterCommon.cs
src/libraries/Common/src/System/Drawing/ColorTable.cs
src/libraries/Common/src/System/Drawing/ColorTranslator.cs
src/libraries/System.Drawing.Primitives/ref/System.Drawing.Primitives.cs
src/libraries/System.Drawing.Primitives/ref/System.Drawing.Primitives.csproj
src/libraries/System.Drawing.Primitives/src/System.Drawing.Primitives.csproj
src/libraries/System.Drawing.Primitives/src/System/Drawing/Color.cs
src/libraries/System.Drawing.Primitives/src/System/Drawing/Point.cs
src/libraries/System.Drawing.Primitives/src/System/Drawing/PointF.cs
src/libraries/System.Drawing.Primitives/src/System/Drawing/Rectangle.cs
src/libraries/System.Drawing.Primitives/src/System/Drawing/RectangleF.cs
src/libraries/System.Drawing.Primitives/src/System/Drawing/Size.cs
src/libraries/System.Drawing.Primitives/src/System/Drawing/SizeF.cs

index 397bc75..1875280 100644 (file)
@@ -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.
 
+#nullable enable
 using System.Diagnostics;
 using System.Globalization;
 
@@ -115,7 +116,7 @@ namespace System.Drawing
                 else
                 {
                     Debug.Assert(culture != null);
-                    NumberFormatInfo formatInfo = (NumberFormatInfo)culture.GetFormat(typeof(NumberFormatInfo));
+                    var formatInfo = (NumberFormatInfo?)culture.GetFormat(typeof(NumberFormatInfo));
                     return IntFromString(text, formatInfo);
                 }
             }
@@ -130,7 +131,7 @@ namespace System.Drawing
             return Convert.ToInt32(value, radix);
         }
 
-        private static int IntFromString(string value, NumberFormatInfo formatInfo)
+        private static int IntFromString(string value, NumberFormatInfo? formatInfo)
         {
             return int.Parse(value, NumberStyles.Integer, formatInfo);
         }
index 8bbf132..9dc9edc 100644 (file)
@@ -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.
 
+#nullable enable
 using System.Collections.Generic;
 using System.Reflection;
 
@@ -24,7 +25,7 @@ namespace System.Drawing
             foreach (PropertyInfo prop in typeWithColors.GetProperties(BindingFlags.Public | BindingFlags.Static))
             {
                 if (prop.PropertyType == typeof(Color))
-                    dictionary[prop.Name] = (Color)prop.GetValue(null, null);
+                    dictionary[prop.Name] = (Color)prop.GetValue(null, null)!;
             }
         }
 
index ef7ae94..47763c3 100644 (file)
@@ -20,7 +20,7 @@ namespace System.Drawing
 
         private const int OleSystemColorFlag = unchecked((int)0x80000000);
 
-        private static Dictionary<string, Color> s_htmlSysColorTable;
+        private static Dictionary<string, Color>? s_htmlSysColorTable;
 
         internal static uint COLORREFToARGB(uint value)
             => ((value >> COLORREF_RedShift) & 0xFF) << Color.ARGBRedShift
@@ -260,7 +260,7 @@ namespace System.Drawing
                     InitializeHtmlSysColorTable();
                 }
 
-                s_htmlSysColorTable.TryGetValue(htmlColor.ToLower(CultureInfo.InvariantCulture), out c);
+                s_htmlSysColorTable!.TryGetValue(htmlColor.ToLower(CultureInfo.InvariantCulture), out c);
             }
 
             // resort to type converter which will handle named colors
index 17aaa71..577cfbc 100644 (file)
@@ -163,7 +163,7 @@ namespace System.Drawing
         public static System.Drawing.Color Yellow { get { throw null; } }
         public static System.Drawing.Color YellowGreen { get { throw null; } }
         public bool Equals(System.Drawing.Color other) { throw null; }
-        public override bool Equals(object obj) { throw null; }
+        public override bool Equals(object? obj) { throw null; }
         public static System.Drawing.Color FromArgb(int argb) { throw null; }
         public static System.Drawing.Color FromArgb(int alpha, System.Drawing.Color baseColor) { throw null; }
         public static System.Drawing.Color FromArgb(int red, int green, int blue) { throw null; }
@@ -380,7 +380,7 @@ namespace System.Drawing
         public static System.Drawing.Point Add(System.Drawing.Point pt, System.Drawing.Size sz) { throw null; }
         public static System.Drawing.Point Ceiling(System.Drawing.PointF value) { throw null; }
         public bool Equals(System.Drawing.Point other) { throw null; }
-        public override bool Equals(object obj) { throw null; }
+        public override bool Equals(object? obj) { throw null; }
         public override int GetHashCode() { throw null; }
         public void Offset(System.Drawing.Point p) { }
         public void Offset(int dx, int dy) { }
@@ -407,7 +407,7 @@ namespace System.Drawing
         public static System.Drawing.PointF Add(System.Drawing.PointF pt, System.Drawing.Size sz) { throw null; }
         public static System.Drawing.PointF Add(System.Drawing.PointF pt, System.Drawing.SizeF sz) { throw null; }
         public bool Equals(System.Drawing.PointF other) { throw null; }
-        public override bool Equals(object obj) { throw null; }
+        public override bool Equals(object? obj) { throw null; }
         public override int GetHashCode() { throw null; }
         public static System.Drawing.PointF operator +(System.Drawing.PointF pt, System.Drawing.Size sz) { throw null; }
         public static System.Drawing.PointF operator +(System.Drawing.PointF pt, System.Drawing.SizeF sz) { throw null; }
@@ -448,7 +448,7 @@ namespace System.Drawing
         public bool Contains(System.Drawing.Rectangle rect) { throw null; }
         public bool Contains(int x, int y) { throw null; }
         public bool Equals(System.Drawing.Rectangle other) { throw null; }
-        public override bool Equals(object obj) { throw null; }
+        public override bool Equals(object? obj) { throw null; }
         public static System.Drawing.Rectangle FromLTRB(int left, int top, int right, int bottom) { throw null; }
         public override int GetHashCode() { throw null; }
         public static System.Drawing.Rectangle Inflate(System.Drawing.Rectangle rect, int x, int y) { throw null; }
@@ -494,7 +494,7 @@ namespace System.Drawing
         public bool Contains(System.Drawing.RectangleF rect) { throw null; }
         public bool Contains(float x, float y) { throw null; }
         public bool Equals(System.Drawing.RectangleF other) { throw null; }
-        public override bool Equals(object obj) { throw null; }
+        public override bool Equals(object? obj) { throw null; }
         public static System.Drawing.RectangleF FromLTRB(float left, float top, float right, float bottom) { throw null; }
         public override int GetHashCode() { throw null; }
         public static System.Drawing.RectangleF Inflate(System.Drawing.RectangleF rect, float x, float y) { throw null; }
@@ -524,7 +524,7 @@ namespace System.Drawing
         public static System.Drawing.Size Add(System.Drawing.Size sz1, System.Drawing.Size sz2) { throw null; }
         public static System.Drawing.Size Ceiling(System.Drawing.SizeF value) { throw null; }
         public bool Equals(System.Drawing.Size other) { throw null; }
-        public override bool Equals(object obj) { throw null; }
+        public override bool Equals(object? obj) { throw null; }
         public override int GetHashCode() { throw null; }
         public static System.Drawing.Size operator +(System.Drawing.Size sz1, System.Drawing.Size sz2) { throw null; }
         public static System.Drawing.Size operator /(System.Drawing.Size left, int right) { throw null; }
@@ -556,7 +556,7 @@ namespace System.Drawing
         public float Width { get { throw null; } set { } }
         public static System.Drawing.SizeF Add(System.Drawing.SizeF sz1, System.Drawing.SizeF sz2) { throw null; }
         public bool Equals(System.Drawing.SizeF other) { throw null; }
-        public override bool Equals(object obj) { throw null; }
+        public override bool Equals(object? obj) { throw null; }
         public override int GetHashCode() { throw null; }
         public static System.Drawing.SizeF operator +(System.Drawing.SizeF sz1, System.Drawing.SizeF sz2) { throw null; }
         public static System.Drawing.SizeF operator /(System.Drawing.SizeF left, float right) { throw null; }
index 6a8031b..a901689 100644 (file)
@@ -1,6 +1,7 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
     <Configurations>netcoreapp-Debug;netcoreapp-Release;uap-Debug;uap-Release</Configurations>
+    <Nullable>enable</Nullable>
   </PropertyGroup>
   <ItemGroup>
     <Compile Include="System.Drawing.Primitives.cs" />
index c971983..1fff5f8 100644 (file)
@@ -5,6 +5,7 @@
     <AssemblyName>System.Drawing.Primitives</AssemblyName>
     <DefineConstants Condition="'$(TargetsWindows)' == 'true' And '$(TargetGroup)' != 'uap'">$(DefineConstants);FEATURE_WINDOWS_SYSTEM_COLORS</DefineConstants>
     <Configurations>netcoreapp-Unix-Debug;netcoreapp-Unix-Release;netcoreapp-Windows_NT-Debug;netcoreapp-Windows_NT-Release;uap-Windows_NT-Debug;uap-Windows_NT-Release</Configurations>
+    <nullable>enable</nullable>
   </PropertyGroup>
   <ItemGroup>
     <Reference Include="System.Collections" />
index 4f702e8..e909c45 100644 (file)
@@ -325,7 +325,7 @@ namespace System.Drawing
 
         // User supplied name of color. Will not be filled in if
         // we map to a "knowncolor"
-        private readonly string name; // Do not rename (binary serialization)
+        private readonly string? name; // Do not rename (binary serialization)
 
         // Standard 32bit sRGB (ARGB)
         private readonly long value; // Do not rename (binary serialization)
@@ -344,7 +344,7 @@ namespace System.Drawing
             this.knownColor = unchecked((short)knownColor);
         }
 
-        private Color(long value, short state, string name, KnownColor knownColor)
+        private Color(long value, short state, string? name, KnownColor knownColor)
         {
             this.value = value;
             this.state = state;
@@ -383,6 +383,7 @@ namespace System.Drawing
             {
                 if ((state & StateNameValid) != 0)
                 {
+                    Debug.Assert(name != null);
                     return name;
                 }
 
@@ -563,7 +564,7 @@ namespace System.Drawing
 
         public static bool operator !=(Color left, Color right) => !(left == right);
 
-        public override bool Equals(object obj) => obj is Color other && Equals(other);
+        public override bool Equals(object? obj) => obj is Color other && Equals(other);
 
         public bool Equals(Color other) => this == other;
 
@@ -576,7 +577,7 @@ namespace System.Drawing
             // an unnamed color with the same ARGB value.
             // 3. Have an unknown name. Will differ from other unknown-named colors only by name, so we
             // can usefully use the names hash code alone.
-            if (name != null & !IsKnownColor)
+            if (name != null && !IsKnownColor)
                 return name.GetHashCode();
 
             return HashCode.Combine(value.GetHashCode(), state.GetHashCode(), knownColor.GetHashCode());
index c8e92c1..eff3cf4 100644 (file)
@@ -135,7 +135,7 @@ namespace System.Drawing
         /// Specifies whether this <see cref='System.Drawing.Point'/> contains the same coordinates as the specified
         /// <see cref='object'/>.
         /// </summary>
-        public override bool Equals(object obj) => obj is Point && Equals((Point)obj);
+        public override bool Equals(object? obj) => obj is Point && Equals((Point)obj);
 
         public bool Equals(Point other) => this == other;
 
index 6d469f4..1ca26b2 100644 (file)
@@ -107,7 +107,7 @@ namespace System.Drawing
         /// </summary>
         public static PointF Subtract(PointF pt, SizeF sz) => new PointF(pt.X - sz.Width, pt.Y - sz.Height);
 
-        public override bool Equals(object obj) => obj is PointF && Equals((PointF)obj);
+        public override bool Equals(object? obj) => obj is PointF && Equals((PointF)obj);
 
         public bool Equals(PointF other) => this == other;
 
index 9680fc2..fe45289 100644 (file)
@@ -155,7 +155,7 @@ namespace System.Drawing
         /// Tests whether <paramref name="obj"/> is a <see cref='System.Drawing.Rectangle'/> with the same location
         /// and size of this Rectangle.
         /// </summary>
-        public override bool Equals(object obj) => obj is Rectangle && Equals((Rectangle)obj);
+        public override bool Equals(object? obj) => obj is Rectangle && Equals((Rectangle)obj);
 
         public bool Equals(Rectangle other) => this == other;
 
index e649eff..a9d5206 100644 (file)
@@ -158,7 +158,7 @@ namespace System.Drawing
         /// Tests whether <paramref name="obj"/> is a <see cref='System.Drawing.RectangleF'/> with the same location and
         /// size of this <see cref='System.Drawing.RectangleF'/>.
         /// </summary>
-        public override bool Equals(object obj) => obj is RectangleF && Equals((RectangleF)obj);
+        public override bool Equals(object? obj) => obj is RectangleF && Equals((RectangleF)obj);
 
         public bool Equals(RectangleF other) => this == other;
 
index 1a315cb..0ad652e 100644 (file)
@@ -176,7 +176,7 @@ namespace System.Drawing
         /// Tests to see whether the specified object is a <see cref='System.Drawing.Size'/>  with the same dimensions
         /// as this <see cref='System.Drawing.Size'/>.
         /// </summary>
-        public override bool Equals(object obj) => obj is Size && Equals((Size)obj);
+        public override bool Equals(object? obj) => obj is Size && Equals((Size)obj);
 
         public bool Equals(Size other) => this == other;
 
index 5d4cb5e..d378b76 100644 (file)
@@ -137,7 +137,7 @@ namespace System.Drawing
         /// Tests to see whether the specified object is a <see cref='System.Drawing.SizeF'/>  with the same dimensions
         /// as this <see cref='System.Drawing.SizeF'/>.
         /// </summary>
-        public override bool Equals(object obj) => obj is SizeF && Equals((SizeF)obj);
+        public override bool Equals(object? obj) => obj is SizeF && Equals((SizeF)obj);
 
         public bool Equals(SizeF other) => this == other;