Skip to content

Commit

Permalink
Enable nullable annotations for System.Drawing.Primitives
Browse files Browse the repository at this point in the history
Contributes to: #40623
  • Loading branch information
maryamariyan committed Sep 18, 2019
1 parent cd0576d commit a9414ce
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/Common/src/System/Drawing/ColorConverterCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private static int IntFromString(string text, CultureInfo culture)
else
{
Debug.Assert(culture != null);
NumberFormatInfo formatInfo = (NumberFormatInfo)culture.GetFormat(typeof(NumberFormatInfo));
var formatInfo = (NumberFormatInfo?)culture.GetFormat(typeof(NumberFormatInfo));
return IntFromString(text, formatInfo);
}
}
Expand All @@ -130,7 +130,7 @@ private static int IntFromString(string value, int radix)
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);
}
Expand Down
8 changes: 7 additions & 1 deletion src/Common/src/System/Drawing/ColorTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;

namespace System.Drawing
Expand All @@ -21,10 +22,15 @@ private static Dictionary<string, Color> GetColors()

private static void FillWithProperties(Dictionary<string, Color> dictionary, Type typeWithColors)
{
object? value;
foreach (PropertyInfo prop in typeWithColors.GetProperties(BindingFlags.Public | BindingFlags.Static))
{
if (prop.PropertyType == typeof(Color))
dictionary[prop.Name] = (Color)prop.GetValue(null, null);
{
value = prop.GetValue(null, null);
Debug.Assert(value != null);
dictionary[prop.Name] = (Color)value;
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/Common/src/System/Drawing/ColorTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;

namespace System.Drawing
Expand All @@ -20,7 +21,7 @@ public static class ColorTranslator

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
Expand Down Expand Up @@ -260,6 +261,7 @@ public static Color FromHtml(string htmlColor)
InitializeHtmlSysColorTable();
}

Debug.Assert(s_htmlSysColorTable != null);
s_htmlSysColorTable.TryGetValue(htmlColor.ToLower(CultureInfo.InvariantCulture), out c);
}

Expand Down
14 changes: 7 additions & 7 deletions src/System.Drawing.Primitives/ref/System.Drawing.Primitives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -380,7 +380,7 @@ public partial struct Point : System.IEquatable<System.Drawing.Point>
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) { }
Expand All @@ -407,7 +407,7 @@ public partial struct PointF : System.IEquatable<System.Drawing.PointF>
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; }
Expand Down Expand Up @@ -448,7 +448,7 @@ public partial struct Rectangle : System.IEquatable<System.Drawing.Rectangle>
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; }
Expand Down Expand Up @@ -494,7 +494,7 @@ public partial struct RectangleF : System.IEquatable<System.Drawing.RectangleF>
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; }
Expand Down Expand Up @@ -524,7 +524,7 @@ public partial struct Size : System.IEquatable<System.Drawing.Size>
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; }
Expand Down Expand Up @@ -556,7 +556,7 @@ public partial struct SizeF : System.IEquatable<System.Drawing.SizeF>
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; }
Expand Down
Original file line number Diff line number Diff line change
@@ -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" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
11 changes: 6 additions & 5 deletions src/System.Drawing.Primitives/src/System/Drawing/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -344,7 +344,7 @@ internal Color(KnownColor knownColor)
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;
Expand Down Expand Up @@ -383,7 +383,8 @@ public string Name
{
if ((state & StateNameValid) != 0)
{
return name;
Debug.Assert(name != null);
return name; // safern: possible null reference return
}

if (IsKnownColor)
Expand Down Expand Up @@ -563,7 +564,7 @@ public override string ToString()

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;

Expand All @@ -576,7 +577,7 @@ public override int GetHashCode()
// 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());
Expand Down
2 changes: 1 addition & 1 deletion src/System.Drawing.Primitives/src/System/Drawing/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public int Y
/// 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;

Expand Down
2 changes: 1 addition & 1 deletion src/System.Drawing.Primitives/src/System/Drawing/PointF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public float Y
/// </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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public int Height
/// 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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public float Height
/// 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;

Expand Down
2 changes: 1 addition & 1 deletion src/System.Drawing.Primitives/src/System/Drawing/Size.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public static Size Round(SizeF value) =>
/// 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;

Expand Down
2 changes: 1 addition & 1 deletion src/System.Drawing.Primitives/src/System/Drawing/SizeF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public float Height
/// 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;

Expand Down

0 comments on commit a9414ce

Please sign in to comment.