Skip to content

Commit

Permalink
Enable nullable annotations for System.Drawing.Primitives (dotnet/cor…
Browse files Browse the repository at this point in the history
…efx#41188)




Commit migrated from dotnet/corefx@aa72982
  • Loading branch information
maryamariyan authored Sep 20, 2019
1 parent 6fd7702 commit 9c6f615
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -115,7 +116,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 +131,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
3 changes: 2 additions & 1 deletion src/libraries/Common/src/System/Drawing/ColorTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -24,7 +25,7 @@ private static void FillWithProperties(Dictionary<string, Color> dictionary, Typ
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)!;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libraries/Common/src/System/Drawing/ColorTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,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,7 +260,7 @@ public static Color FromHtml(string htmlColor)
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
Expand Down
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
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,6 +383,7 @@ public string Name
{
if ((state & StateNameValid) != 0)
{
Debug.Assert(name != null);
return name;
}

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
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
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
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
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 9c6f615

Please sign in to comment.