Skip to content

Commit

Permalink
Merge pull request #65 from sharwell/nullable
Browse files Browse the repository at this point in the history
Enable nullable reference types
  • Loading branch information
sharwell authored Jul 20, 2020
2 parents 6cdf3fe + b0f7bf6 commit 3ee8664
Show file tree
Hide file tree
Showing 124 changed files with 1,558 additions and 1,341 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,6 @@ csharp_space_between_method_declaration_name_and_open_parenthesis = false
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_parentheses = false
csharp_space_between_square_brackets = false

# RS0041: Public members should not use oblivious types
dotnet_diagnostic.RS0041.severity = none
15 changes: 13 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@
<Project>

<PropertyGroup>
<LangVersion>7.3</LangVersion>
<LangVersion>8.0</LangVersion>
<Features>strict</Features>
</PropertyGroup>

<!-- Enable nullable reference types -->
<PropertyGroup>
<Nullable>enable</Nullable>
<GenerateNullableAttributes>false</GenerateNullableAttributes>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="TunnelVisionLabs.ReferenceAssemblyAnnotator" Version="1.0.0-alpha.154" PrivateAssets="all" />
<PackageDownload Include="Microsoft.NETCore.App.Ref" Version="[3.1.0]" />
</ItemGroup>

<PropertyGroup Condition="'$(BuildingInsideVisualStudio)' != 'true'">
<!-- Ideally this is always enabled, but that tends to hurt developer productivity -->
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down Expand Up @@ -58,7 +69,7 @@

<!-- Public API -->
<ItemGroup>
<PackageReference Include="DotNetAnalyzers.PublicApiAnalyzer" Version="1.0.0-beta.12" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.0-beta3.20367.1" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
Expand Down
11 changes: 11 additions & 0 deletions NuGet.Config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="dotnet5" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json" />
</packageSources>
<disabledPackageSources>
<clear />
</disabledPackageSources>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nullable enable
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nullable enable
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net45;netstandard1.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net45;netstandard1.1;netstandard2.0;netstandard2.1</TargetFrameworks>
<RootNamespace>TunnelVisionLabs.Collections.Trees</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
Expand Down
24 changes: 12 additions & 12 deletions TunnelVisionLabs.Collections.Trees.Test/AbstractSetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public abstract class AbstractSetTest
public void TestUnionWith()
{
ISet<int> set = CreateSet<int>();
Assert.Throws<ArgumentNullException>(() => set.UnionWith(null));
Assert.Throws<ArgumentNullException>(() => set.UnionWith(null!));

set.UnionWith(TransformEnumerableForSetOperation(Enumerable.Range(0, 7)));
set.UnionWith(TransformEnumerableForSetOperation(Enumerable.Range(5, 5)));
Expand All @@ -27,7 +27,7 @@ public void TestUnionWith()
public void TestExceptWith()
{
ISet<int> set = CreateSet<int>();
Assert.Throws<ArgumentNullException>(() => set.ExceptWith(null));
Assert.Throws<ArgumentNullException>(() => set.ExceptWith(null!));

// Return without iterating if the set is already empty
set.ExceptWith(EverythingThrowsEnumerable<int>.Instance);
Expand All @@ -49,7 +49,7 @@ public void TestExceptWith()
public void TestIntersectWith()
{
ISet<int> set = CreateSet<int>();
Assert.Throws<ArgumentNullException>(() => set.IntersectWith(null));
Assert.Throws<ArgumentNullException>(() => set.IntersectWith(null!));

// Return without iterating if the set is already empty
set.IntersectWith(EverythingThrowsEnumerable<int>.Instance);
Expand All @@ -76,7 +76,7 @@ public void TestSymmetricExceptWith()
{
ISet<int> set = CreateSet<int>();
ISet<int> second = CreateSet<int>();
Assert.Throws<ArgumentNullException>(() => set.SymmetricExceptWith(null));
Assert.Throws<ArgumentNullException>(() => set.SymmetricExceptWith(null!));

// Test behavior when the current set is empty
set.SymmetricExceptWith(TransformEnumerableForSetOperation(new[] { 1, 5, 3 }));
Expand Down Expand Up @@ -110,7 +110,7 @@ public void TestSymmetricExceptWith()
public void TestIsProperSubsetOf()
{
ISet<int> set = CreateSet<int>();
Assert.Throws<ArgumentNullException>(() => set.IsProperSubsetOf(null));
Assert.Throws<ArgumentNullException>(() => set.IsProperSubsetOf(null!));

// Test behavior when the current set is empty
Assert.False(set.IsProperSubsetOf(TransformEnumerableForSetOperation(Enumerable.Empty<int>())));
Expand Down Expand Up @@ -145,7 +145,7 @@ public void TestIsProperSubsetOf()
public void TestIsProperSupersetOf()
{
ISet<int> set = CreateSet<int>();
Assert.Throws<ArgumentNullException>(() => set.IsProperSupersetOf(null));
Assert.Throws<ArgumentNullException>(() => set.IsProperSupersetOf(null!));

// Return without iterating if the set is already empty
Assert.False(set.IsProperSupersetOf(EverythingThrowsEnumerable<int>.Instance));
Expand Down Expand Up @@ -183,7 +183,7 @@ public void TestIsProperSupersetOf()
public void TestIsSubsetOf()
{
ISet<int> set = CreateSet<int>();
Assert.Throws<ArgumentNullException>(() => set.IsSubsetOf(null));
Assert.Throws<ArgumentNullException>(() => set.IsSubsetOf(null!));

// Return without iterating if the set is already empty
Assert.True(set.IsSubsetOf(EverythingThrowsEnumerable<int>.Instance));
Expand Down Expand Up @@ -217,7 +217,7 @@ public void TestIsSubsetOf()
public void TestIsSupersetOf()
{
ISet<int> set = CreateSet<int>();
Assert.Throws<ArgumentNullException>(() => set.IsSupersetOf(null));
Assert.Throws<ArgumentNullException>(() => set.IsSupersetOf(null!));

// Test IsSupersetOf self
set.Add(1);
Expand Down Expand Up @@ -252,7 +252,7 @@ public void TestIsSupersetOf()
public void TestOverlaps()
{
ISet<int> set = CreateSet<int>();
Assert.Throws<ArgumentNullException>(() => set.Overlaps(null));
Assert.Throws<ArgumentNullException>(() => set.Overlaps(null!));

// Return without iterating if the set is already empty
Assert.False(set.Overlaps(EverythingThrowsEnumerable<int>.Instance));
Expand All @@ -272,7 +272,7 @@ public void TestOverlaps()
public void TestSetEquals()
{
ISet<int> set = CreateSet<int>();
Assert.Throws<ArgumentNullException>(() => set.SetEquals(null));
Assert.Throws<ArgumentNullException>(() => set.SetEquals(null!));

// Test behavior when the current set is empty
Assert.True(set.SetEquals(TransformEnumerableForSetOperation(Enumerable.Empty<int>())));
Expand Down Expand Up @@ -341,7 +341,7 @@ protected static void TestICollectionInterfaceImpl(ICollection collection, bool
{
var copy = new object[collection.Count];

Assert.Throws<ArgumentNullException>(() => collection.CopyTo(null, 0));
Assert.Throws<ArgumentNullException>(() => collection.CopyTo(null!, 0));
Assert.Throws<ArgumentException>(() => collection.CopyTo(new object[1, collection.Count], 0));
Assert.Throws<ArgumentException>(() => collection.CopyTo(Array.CreateInstance(typeof(object), lengths: new[] { collection.Count }, lowerBounds: new[] { -1 }), 0));
Assert.Throws<ArgumentOutOfRangeException>(() => collection.CopyTo(copy, -1));
Expand All @@ -368,7 +368,7 @@ protected static void TestICollectionInterfaceImpl(ICollection collection, bool
{
var copy = new int[collection.Count];

Assert.Throws<ArgumentNullException>(() => collection.CopyTo(null, 0));
Assert.Throws<ArgumentNullException>(() => collection.CopyTo(null!, 0));
Assert.Throws<ArgumentException>(() => collection.CopyTo(new int[1, collection.Count], 0));
Assert.Throws<ArgumentException>(() => collection.CopyTo(Array.CreateInstance(typeof(int), lengths: new[] { collection.Count }, lowerBounds: new[] { -1 }), 0));
Assert.Throws<ArgumentOutOfRangeException>(() => collection.CopyTo(copy, -1));
Expand Down
4 changes: 3 additions & 1 deletion TunnelVisionLabs.Collections.Trees.Test/GeneratorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace TunnelVisionLabs.Collections.Trees.Test
{
using System.Diagnostics;
using System.Reflection;
using Xunit;

Expand All @@ -23,7 +24,8 @@ public void TestSeed()

void ResetSeed(int? seed)
{
FieldInfo seedField = typeof(Generator).GetField("_seed", BindingFlags.Static | BindingFlags.NonPublic);
FieldInfo? seedField = typeof(Generator).GetField("_seed", BindingFlags.Static | BindingFlags.NonPublic);
Debug.Assert(seedField is object, $"Assertion failed: {nameof(seedField)} is object");
seedField.SetValue(null, seed);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public void TestRemove()
dictionary.Remove(1));
}

protected abstract IImmutableDictionary<TKey, TValue> CreateDictionary<TKey, TValue>();
protected abstract IImmutableDictionary<TKey, TValue> CreateDictionary<TKey, TValue>()
where TKey : notnull;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ protected static void TestICollectionInterfaceImpl(ICollection collection, bool
Assert.NotNull(collection.SyncRoot);
Assert.Same(collection, collection.SyncRoot);

Assert.Throws<ArgumentNullException>("array", () => collection.CopyTo(null, 0));
Assert.Throws<ArgumentNullException>("array", () => collection.CopyTo(null!, 0));
Assert.Throws<ArgumentException>(() => collection.CopyTo(new int[collection.Count, 1], 0));

void CopyToArrayWithNonZeroLowerBound() => collection.CopyTo(Array.CreateInstance(typeof(int), lengths: new[] { collection.Count }, lowerBounds: new[] { 1 }), 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void TestRemoveRange()
dictionary.RemoveRange(itemsToRemove);
Assert.Equal(new[] { 1, 3, 5, 7, 9 }.Select(x => new KeyValuePair<int, int>(x, x)), dictionary);

Assert.Throws<ArgumentNullException>("keys", () => dictionary.RemoveRange(null));
Assert.Throws<ArgumentNullException>("keys", () => dictionary.RemoveRange(null!));
}

[Fact]
Expand Down Expand Up @@ -132,7 +132,7 @@ public void TestIDictionaryT()
Assert.Equal(Enumerable.Range(0, 9), dictionary.Values);

Assert.Throws<NotSupportedException>(() => dictionary.Keys.Add(0));
Assert.Throws<ArgumentNullException>("array", () => dictionary.Keys.CopyTo(null, 0));
Assert.Throws<ArgumentNullException>("array", () => dictionary.Keys.CopyTo(null!, 0));
Assert.Throws<ArgumentOutOfRangeException>("arrayIndex", () => dictionary.Keys.CopyTo(new int[dictionary.Count], -1));
Assert.Throws<ArgumentOutOfRangeException>("arrayIndex", () => dictionary.Keys.CopyTo(new int[dictionary.Count], dictionary.Count + 1));
Assert.Throws<ArgumentException>(() => dictionary.Keys.CopyTo(new int[dictionary.Count], 1));
Expand Down Expand Up @@ -162,7 +162,7 @@ public void TestIDictionaryT()
Assert.Equal(0, keyEnumerator.Current);

Assert.Throws<NotSupportedException>(() => dictionary.Values.Add(0));
Assert.Throws<ArgumentNullException>("array", () => dictionary.Values.CopyTo(null, 0));
Assert.Throws<ArgumentNullException>("array", () => dictionary.Values.CopyTo(null!, 0));
Assert.Throws<ArgumentOutOfRangeException>("arrayIndex", () => dictionary.Values.CopyTo(new int[dictionary.Count], -1));
Assert.Throws<ArgumentOutOfRangeException>("arrayIndex", () => dictionary.Values.CopyTo(new int[dictionary.Count], dictionary.Count + 1));
Assert.Throws<ArgumentException>(() => dictionary.Values.CopyTo(new int[dictionary.Count], 1));
Expand Down Expand Up @@ -215,7 +215,7 @@ public void TestIDictionary()
Assert.False(dictionary.IsReadOnly);
Assert.False(dictionary.IsSynchronized);

Assert.Throws<ArgumentNullException>("key", () => dictionary.Add(key: null, value: 1));
Assert.Throws<ArgumentNullException>("key", () => dictionary.Add(key: null!, value: 1));
Assert.Throws<ArgumentException>("value", () => dictionary.Add(key: 1, value: null));
Assert.Throws<ArgumentException>("key", () => dictionary.Add(key: "string value", value: 0));
Assert.Throws<ArgumentException>("value", () => dictionary.Add(key: 0, value: "string value"));
Expand All @@ -228,11 +228,11 @@ public void TestIDictionary()
dictionary.Add(10, 11);
Assert.Equal(11, dictionary.Count);

Assert.Throws<ArgumentNullException>("key", () => dictionary[key: null]);
Assert.Throws<ArgumentNullException>("key", () => dictionary[key: null!]);
Assert.Null(dictionary["string key"]);
Assert.Equal(11, dictionary[10]);

Assert.Throws<ArgumentNullException>("key", () => dictionary[key: null] = 12);
Assert.Throws<ArgumentNullException>("key", () => dictionary[key: null!] = 12);
Assert.Throws<ArgumentException>("key", () => dictionary["string key"] = 12);
Assert.Throws<ArgumentException>("value", () => dictionary[10] = null);
Assert.Throws<ArgumentException>("value", () => dictionary[10] = "string value");
Expand All @@ -247,11 +247,11 @@ public void TestIDictionary()
Assert.Equal(entries.Select(i => i.Key), dictionary.Keys.Cast<object>());
Assert.Equal(entries.Select(i => i.Value), dictionary.Values.Cast<object>());

Assert.Throws<ArgumentNullException>(() => dictionary.Contains(null));
Assert.Throws<ArgumentNullException>(() => dictionary.Contains(null!));
Assert.False(dictionary.Contains("string value"));
Assert.True(dictionary.Contains(10));

Assert.Throws<ArgumentNullException>(() => dictionary.Remove(null));
Assert.Throws<ArgumentNullException>(() => dictionary.Remove(null!));
Assert.Equal(11, dictionary.Count);
dictionary.Remove("string value");
Assert.Equal(11, dictionary.Count);
Expand Down Expand Up @@ -300,7 +300,7 @@ void TestCollection<T>(ICollection collection, Func<int, T> indexToExpectedValue
Assert.False(collection.IsSynchronized);
Assert.Same(dictionary, collection.SyncRoot);

Assert.Throws<ArgumentNullException>("array", () => collection.CopyTo(null, 0));
Assert.Throws<ArgumentNullException>("array", () => collection.CopyTo(null!, 0));
Assert.Throws<ArgumentException>(() => collection.CopyTo(new int[collection.Count, 1], 0));
Assert.Throws<ArgumentException>(() => collection.CopyTo(Array.CreateInstance(typeof(int), lengths: new[] { collection.Count }, lowerBounds: new[] { 1 }), 0));
Assert.Throws<ArgumentOutOfRangeException>("index", () => collection.CopyTo(new int[collection.Count], -1));
Expand Down
Loading

0 comments on commit 3ee8664

Please sign in to comment.