Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set all but main project to nullable #10170

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions csharp/src/AddressBook/AddPerson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ private static Person PromptForAddress(TextReader input, TextWriter output)
Person person = new Person();

output.Write("Enter person ID: ");
person.Id = int.Parse(input.ReadLine());
person.Id = int.Parse(input.ReadLine() ?? string.Empty);

output.Write("Enter name: ");
person.Name = input.ReadLine();

output.Write("Enter email address (blank for none): ");
string email = input.ReadLine();
string email = input.ReadLine() ?? string.Empty;
if (email.Length > 0)
{
person.Email = email;
Expand All @@ -60,7 +60,7 @@ private static Person PromptForAddress(TextReader input, TextWriter output)
while (true)
{
output.Write("Enter a phone number (or leave blank to finish): ");
string number = input.ReadLine();
string number = input.ReadLine() ?? string.Empty;
if (number.Length == 0)
{
break;
Expand All @@ -69,7 +69,7 @@ private static Person PromptForAddress(TextReader input, TextWriter output)
Person.Types.PhoneNumber phoneNumber = new Person.Types.PhoneNumber { Number = number };

output.Write("Is this a mobile, home, or work phone? ");
String type = input.ReadLine();
String type = input.ReadLine() ?? string.Empty;
switch (type)
{
case "mobile":
Expand Down
1 change: 1 addition & 0 deletions csharp/src/AddressBook/AddressBook.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<OutputType>Exe</OutputType>
<StartupObject>Google.Protobuf.Examples.AddressBook.Program</StartupObject>
<Nullable>Enable</Nullable>
<IsPackable>False</IsPackable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<OutputType>Exe</OutputType>
<Nullable>Enable</Nullable>
<IsPackable>False</IsPackable>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions csharp/src/Google.Protobuf.JsonDump/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static int Main(string[] args)
Console.Error.WriteLine("including assembly e.g. ProjectNamespace.Message,Company.Project");
return 1;
}
Type type = Type.GetType(args[0]);
Type? type = Type.GetType(args[0]);
if (type == null)
{
Console.Error.WriteLine("Unable to load type {0}.", args[0]);
Expand All @@ -61,7 +61,7 @@ private static int Main(string[] args)
Console.Error.WriteLine("Type {0} doesn't implement IMessage.", args[0]);
return 1;
}
IMessage message = (IMessage) Activator.CreateInstance(type);
IMessage message = (IMessage) Activator.CreateInstance(type)!;
using (var input = File.OpenRead(args[1]))
{
message.MergeFrom(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<TargetFrameworks>net462;netstandard1.1;netstandard2.0</TargetFrameworks>
<LangVersion>10.0</LangVersion>
<AssemblyOriginatorKeyFile>../../keys/Google.Protobuf.snk</AssemblyOriginatorKeyFile>
<Nullable>Enable</Nullable>
<SignAssembly>true</SignAssembly>
<IsPackable>False</IsPackable>
</PropertyGroup>
Expand Down
6 changes: 2 additions & 4 deletions csharp/src/Google.Protobuf.Test/ByteStringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
using System.Linq;
using System.Buffers;
using System.Runtime.InteropServices;
using System.Threading;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

namespace Google.Protobuf
Expand All @@ -66,15 +64,15 @@ public void Equality()
Assert.IsFalse(b1 == b3);
Assert.IsFalse(b1 == b4);
Assert.IsFalse(b1 == null);
Assert.IsTrue((ByteString) null == null);
Assert.IsTrue((ByteString?) null == null);
Assert.IsFalse(b1 != b1);
Assert.IsFalse(b1 != b2);
Assert.IsTrue(ByteString.Empty == ByteString.Empty);
#pragma warning restore 1718
Assert.IsTrue(b1 != b3);
Assert.IsTrue(b1 != b4);
Assert.IsTrue(b1 != null);
Assert.IsFalse((ByteString) null != null);
Assert.IsFalse((ByteString?) null != null);
}

[Test]
Expand Down
10 changes: 5 additions & 5 deletions csharp/src/Google.Protobuf.Test/CodedInputStreamTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ private static void AssertReadVarint(byte[] data, ulong value)
private static void AssertReadVarintFailure(InvalidProtocolBufferException expected, byte[] data)
{
CodedInputStream input = new CodedInputStream(data);
var exception = Assert.Throws<InvalidProtocolBufferException>(() => input.ReadRawVarint32());
var exception = Assert.Throws<InvalidProtocolBufferException>(() => input.ReadRawVarint32())!;
Assert.AreEqual(expected.Message, exception.Message);

input = new CodedInputStream(data);
exception = Assert.Throws<InvalidProtocolBufferException>(() => input.ReadRawVarint64());
exception = Assert.Throws<InvalidProtocolBufferException>(() => input.ReadRawVarint64())!;
Assert.AreEqual(expected.Message, exception.Message);

AssertReadFromParseContext(new ReadOnlySequence<byte>(data), (ref ParseContext ctx) =>
Expand Down Expand Up @@ -153,7 +153,7 @@ private static void AssertReadVarintFailure(InvalidProtocolBufferException expec
}, false);

// Make sure we get the same error when reading directly from a Stream.
exception = Assert.Throws<InvalidProtocolBufferException>(() => CodedInputStream.ReadRawVarint32(new MemoryStream(data)));
exception = Assert.Throws<InvalidProtocolBufferException>(() => CodedInputStream.ReadRawVarint32(new MemoryStream(data)))!;
Assert.AreEqual(expected.Message, exception.Message);
}

Expand Down Expand Up @@ -876,9 +876,9 @@ public void RecursionLimitAppliedWhileSkippingGroup()
[Test]
public void Construction_Invalid()
{
Assert.Throws<ArgumentNullException>(() => new CodedInputStream((byte[]) null));
Assert.Throws<ArgumentNullException>(() => new CodedInputStream((byte[]?) null));
Assert.Throws<ArgumentNullException>(() => new CodedInputStream(null, 0, 0));
Assert.Throws<ArgumentNullException>(() => new CodedInputStream((Stream) null));
Assert.Throws<ArgumentNullException>(() => new CodedInputStream((Stream?) null));
Assert.Throws<ArgumentOutOfRangeException>(() => new CodedInputStream(new byte[10], 100, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => new CodedInputStream(new byte[10], 5, 10));
}
Expand Down
26 changes: 13 additions & 13 deletions csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public void NullValuesProhibited()
private void TestNullValues<T>(T nonNullValue)
{
var map = new MapField<int, T>();
var nullValue = (T) (object) null;
Assert.Throws<ArgumentNullException>(() => map.Add(0, nullValue));
Assert.Throws<ArgumentNullException>(() => map[0] = nullValue);
var nullValue = (T?) (object?) null;
Assert.Throws<ArgumentNullException>(() => map.Add(0, nullValue!));
Assert.Throws<ArgumentNullException>(() => map[0] = nullValue!);
map.Add(1, nonNullValue);
map[1] = nonNullValue;
}
Expand All @@ -78,14 +78,14 @@ private void TestNullValues<T>(T nonNullValue)
public void Add_ForbidsNullKeys()
{
var map = new MapField<string, ForeignMessage>();
Assert.Throws<ArgumentNullException>(() => map.Add(null, new ForeignMessage()));
Assert.Throws<ArgumentNullException>(() => map.Add(null!, new ForeignMessage()));
}

[Test]
public void Indexer_ForbidsNullKeys()
{
var map = new MapField<string, ForeignMessage>();
Assert.Throws<ArgumentNullException>(() => map[null] = new ForeignMessage());
Assert.Throws<ArgumentNullException>(() => map[null!] = new ForeignMessage());
}

[Test]
Expand Down Expand Up @@ -230,7 +230,7 @@ public void Remove_Key()
Assert.AreEqual(1, map.Count);
Assert.IsTrue(map.Remove("foo"));
Assert.AreEqual(0, map.Count);
Assert.Throws<ArgumentNullException>(() => map.Remove(null));
Assert.Throws<ArgumentNullException>(() => map.Remove(null!));
}

[Test]
Expand All @@ -245,7 +245,7 @@ public void Remove_Pair()
Assert.AreEqual(1, map.Count);
Assert.IsTrue(collection.Remove(NewKeyValuePair("foo", "bar")));
Assert.AreEqual(0, map.Count);
Assert.Throws<ArgumentException>(() => collection.Remove(new KeyValuePair<string, string>(null, "")));
Assert.Throws<ArgumentException>(() => collection.Remove(new KeyValuePair<string, string>(null!, "")));
}

[Test]
Expand Down Expand Up @@ -356,7 +356,7 @@ public void IDictionary_Remove()
Assert.AreEqual(1, dictionary.Count);
dictionary.Remove("x");
Assert.AreEqual(0, dictionary.Count);
Assert.Throws<ArgumentNullException>(() => dictionary.Remove(null));
Assert.Throws<ArgumentNullException>(() => dictionary.Remove(null!));
}

[Test]
Expand All @@ -369,7 +369,7 @@ public void IDictionary_CopyTo()
CollectionAssert.AreEqual(new[] { default, new DictionaryEntry("x", "y"), default }, array);
var objectArray = new object[3];
dictionary.CopyTo(objectArray, 1);
CollectionAssert.AreEqual(new object[] { null, new DictionaryEntry("x", "y"), null }, objectArray);
CollectionAssert.AreEqual(new object[] { null!, new DictionaryEntry("x", "y"), null! }, objectArray);
}

[Test]
Expand Down Expand Up @@ -415,7 +415,7 @@ public void IDictionary_Indexer_Get()
Assert.AreEqual("y", dictionary["x"]);
Assert.IsNull(dictionary["a"]);
Assert.IsNull(dictionary[5]);
Assert.Throws<ArgumentNullException>(() => dictionary[null].GetHashCode());
Assert.Throws<ArgumentNullException>(() => dictionary[null!]!.GetHashCode());
}

[Test]
Expand All @@ -429,7 +429,7 @@ public void IDictionary_Indexer_Set()
Assert.AreEqual("c", map["a"]);
Assert.Throws<InvalidCastException>(() => dictionary[5] = "x");
Assert.Throws<InvalidCastException>(() => dictionary["x"] = 5);
Assert.Throws<ArgumentNullException>(() => dictionary[null] = "z");
Assert.Throws<ArgumentNullException>(() => dictionary[null!] = "z");
Assert.Throws<ArgumentNullException>(() => dictionary["x"] = null);
}

Expand Down Expand Up @@ -503,7 +503,7 @@ public void KeysContains()
Assert.IsFalse(keys.Contains("bar")); // It's a value!
Assert.IsFalse(keys.Contains("1"));
// Keys can't be null, so we should prevent contains check
Assert.Throws<ArgumentNullException>(() => keys.Contains(null));
Assert.Throws<ArgumentNullException>(() => keys.Contains(null!));
}

[Test]
Expand All @@ -523,7 +523,7 @@ public void ValuesContains()
Assert.IsFalse(values.Contains("foo")); // It's a key!
Assert.IsFalse(values.Contains("1"));
// Values can be null, so this makes sense
Assert.IsFalse(values.Contains(null));
Assert.IsFalse(values.Contains(null!));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void NullableSingleComparisons()

private static void ValidateEqualityComparer<T>(EqualityComparer<T> comparer, IEnumerable<T> values)
{
var array = values.ToArray();
T[] array = values.ToArray();
// Each value should be equal to itself, but not to any other value.
for (int i = 0; i < array.Length; i++)
{
Expand All @@ -114,7 +114,7 @@ private static void ValidateEqualityComparer<T>(EqualityComparer<T> comparer, IE
{
Assert.IsFalse(comparer.Equals(array[i], array[j]),
"{0} and {1} should not be equal", array[i], array[j]);
Assert.AreNotEqual(comparer.GetHashCode(array[i]), comparer.GetHashCode(array[j]),
Assert.AreNotEqual(comparer.GetHashCode(array[i]!), comparer.GetHashCode(array[j]!),
"Hash codes for {0} and {1} should not be equal", array[i], array[j]);
}
}
Expand Down
26 changes: 13 additions & 13 deletions csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public class RepeatedFieldTest
public void NullValuesRejected()
{
var list = new RepeatedField<string>();
Assert.Throws<ArgumentNullException>(() => list.Add((string)null));
Assert.Throws<ArgumentNullException>(() => list.Add((IEnumerable<string>)null));
Assert.Throws<ArgumentNullException>(() => list.Add((RepeatedField<string>)null));
Assert.Throws<ArgumentNullException>(() => list.Contains(null));
Assert.Throws<ArgumentNullException>(() => list.IndexOf(null));
Assert.Throws<ArgumentNullException>(() => list.Add((string?)null!));
Assert.Throws<ArgumentNullException>(() => list.Add((IEnumerable<string>?)null));
Assert.Throws<ArgumentNullException>(() => list.Add((RepeatedField<string>?)null));
Assert.Throws<ArgumentNullException>(() => list.Contains(null!));
Assert.Throws<ArgumentNullException>(() => list.IndexOf(null!));
}

[Test]
Expand Down Expand Up @@ -88,7 +88,7 @@ public void AddRange_SlowPath_NullsProhibited_ReferenceType()
var list = new RepeatedField<string>();
// It's okay for this to throw ArgumentNullException if necessary.
// It's not ideal, but not awful.
Assert.Catch<ArgumentException>(() => list.AddRange(new[] { "foo", null }.Select(x => x)));
Assert.Catch<ArgumentException>(() => list.AddRange(new[] { "foo", null! }.Select(x => x)));
}

[Test]
Expand Down Expand Up @@ -137,7 +137,7 @@ public void AddRange_Optimized_NullsProhibited_ReferenceType()
var list = new RepeatedField<string>();
// It's okay for this to throw ArgumentNullException if necessary.
// It's not ideal, but not awful.
Assert.Catch<ArgumentException>(() => list.AddRange(new List<string> { "foo", null }));
Assert.Catch<ArgumentException>(() => list.AddRange(new List<string> { "foo", null! }));
}

[Test]
Expand Down Expand Up @@ -207,15 +207,15 @@ public void Insert_Invalid()
var list = new RepeatedField<string> { "first", "second" };
Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(-1, "foo"));
Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(3, "foo"));
Assert.Throws<ArgumentNullException>(() => list.Insert(0, null));
Assert.Throws<ArgumentNullException>(() => list.Insert(0, null!));
}

[Test]
public void Equals_RepeatedField()
{
var list = new RepeatedField<string> { "first", "second" };
Assert.IsFalse(list.Equals((RepeatedField<string>) null));
Assert.IsTrue(list.Equals(list));
Assert.IsFalse(list.Equals((RepeatedField<string>?) null));
Assert.IsTrue(list!.Equals(list));
Assert.IsFalse(list.Equals(new RepeatedField<string> { "first", "third" }));
Assert.IsFalse(list.Equals(new RepeatedField<string> { "first" }));
Assert.IsTrue(list.Equals(new RepeatedField<string> { "first", "second" }));
Expand All @@ -225,8 +225,8 @@ public void Equals_RepeatedField()
public void Equals_Object()
{
var list = new RepeatedField<string> { "first", "second" };
Assert.IsFalse(list.Equals((object) null));
Assert.IsTrue(list.Equals((object) list));
Assert.IsFalse(list.Equals((object?) null));
Assert.IsTrue(list!.Equals((object) list));
Assert.IsFalse(list.Equals((object) new RepeatedField<string> { "first", "third" }));
Assert.IsFalse(list.Equals((object) new RepeatedField<string> { "first" }));
Assert.IsTrue(list.Equals((object) new RepeatedField<string> { "first", "second" }));
Expand Down Expand Up @@ -273,7 +273,7 @@ public void Indexer_Set()
var list = new RepeatedField<string> { "first", "second" };
list[0] = "changed";
Assert.AreEqual("changed", list[0]);
Assert.Throws<ArgumentNullException>(() => list[0] = null);
Assert.Throws<ArgumentNullException>(() => list[0] = null!);
Assert.Throws<ArgumentOutOfRangeException>(() => list[-1] = "bad");
Assert.Throws<ArgumentOutOfRangeException>(() => list[2] = "bad");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ namespace Google.Protobuf.Compatibility
{
public class PropertyInfoExtensionsTest
{
public string PublicReadWrite { get; set; }
private string PrivateReadWrite { get; set; }
public string PublicReadPrivateWrite { get; private set; }
public string PrivateReadPublicWrite { private get; set; }
public string PublicReadOnly { get { return null; } }
private string PrivateReadOnly { get { return null; } }
public string PublicWriteOnly { set { } }
private string PrivateWriteOnly { set { } }
public string? PublicReadWrite { get; set; }
private string? PrivateReadWrite { get; set; }
public string? PublicReadPrivateWrite { get; private set; }
public string? PrivateReadPublicWrite { private get; set; }
public string? PublicReadOnly => null;
private string? PrivateReadOnly => null;
public string? PublicWriteOnly { set { } }
private string? PrivateWriteOnly { set { } }

[Test]
[TestCase("PublicReadWrite")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ namespace Google.Protobuf.Compatibility
public class TypeExtensionsTest
{
public class DerivedList : List<string> { }
public string PublicProperty { get; set; }
private string PrivateProperty { get; set; }
public string? PublicProperty { get; set; }
private string? PrivateProperty { get; set; }

public void PublicMethod()
{
Expand Down
4 changes: 2 additions & 2 deletions csharp/src/Google.Protobuf.Test/DeprecatedMemberTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ namespace Google.Protobuf
{
public class DeprecatedMemberTest
{
private static void AssertIsDeprecated(MemberInfo member)
private static void AssertIsDeprecated(MemberInfo? member)
{
Assert.NotNull(member);
Assert.IsTrue(member.IsDefined(typeof(ObsoleteAttribute), false), "Member not obsolete: " + member);
Assert.IsTrue(member!.IsDefined(typeof(ObsoleteAttribute), false), "Member not obsolete: " + member);
}

[Test]
Expand Down
6 changes: 3 additions & 3 deletions csharp/src/Google.Protobuf.Test/EqualityTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public static void AssertEquality<T>(T first, T second) where T : IEquatable<T>
Assert.AreEqual(first.GetHashCode(), second.GetHashCode());
}

public static void AssertInequality<T>(T first, T second, bool checkHashcode = true) where T : IEquatable<T>
public static void AssertInequality<T>(T first, T? second, bool checkHashcode = true) where T : IEquatable<T>
{
Assert.IsFalse(first.Equals(second));
Assert.IsFalse(first.Equals((object) second));
Assert.IsFalse(first.Equals(second!));
Assert.IsFalse(first.Equals((object?) second));
// While this isn't a requirement, the chances of this test failing due to
// coincidence rather than a bug are very small.
// For such rare cases, an argument can be used to disable the check.
Expand Down
Loading