diff --git a/src/CodeStyle/Core/Analyzers/Microsoft.CodeAnalysis.CodeStyle.csproj b/src/CodeStyle/Core/Analyzers/Microsoft.CodeAnalysis.CodeStyle.csproj
index 5a85fcbdd0abb..53bc8c8dc1a39 100644
--- a/src/CodeStyle/Core/Analyzers/Microsoft.CodeAnalysis.CodeStyle.csproj
+++ b/src/CodeStyle/Core/Analyzers/Microsoft.CodeAnalysis.CodeStyle.csproj
@@ -27,12 +27,14 @@
+
+
diff --git a/src/Compilers/Core/MSBuildTask/Microsoft.Build.Tasks.CodeAnalysis.csproj b/src/Compilers/Core/MSBuildTask/Microsoft.Build.Tasks.CodeAnalysis.csproj
index 0efb1b2363aae..36b716e8cb01b 100644
--- a/src/Compilers/Core/MSBuildTask/Microsoft.Build.Tasks.CodeAnalysis.csproj
+++ b/src/Compilers/Core/MSBuildTask/Microsoft.Build.Tasks.CodeAnalysis.csproj
@@ -39,8 +39,10 @@
+
+ True
diff --git a/src/Compilers/Core/Portable/Collections/ArrayBuilderExtensions.cs b/src/Compilers/Core/Portable/Collections/ArrayBuilderExtensions.cs
index 9163eaa0a7f60..b87904c71a651 100644
--- a/src/Compilers/Core/Portable/Collections/ArrayBuilderExtensions.cs
+++ b/src/Compilers/Core/Portable/Collections/ArrayBuilderExtensions.cs
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.PooledObjects;
@@ -134,7 +136,7 @@ public static ImmutableArray SelectAsArray(this A
}
}
- public static void AddOptional(this ArrayBuilder builder, T item)
+ public static void AddOptional(this ArrayBuilder builder, T? item)
where T : class
{
if (item != null)
@@ -163,9 +165,9 @@ public static T Peek(this ArrayBuilder builder)
return builder[builder.Count - 1];
}
- public static ImmutableArray ToImmutableOrEmptyAndFree(this ArrayBuilder builderOpt)
+ public static ImmutableArray ToImmutableOrEmptyAndFree(this ArrayBuilder? builder)
{
- return builderOpt?.ToImmutableAndFree() ?? ImmutableArray.Empty;
+ return builder?.ToImmutableAndFree() ?? ImmutableArray.Empty;
}
public static void AddIfNotNull(this ArrayBuilder builder, T? value)
@@ -177,7 +179,7 @@ public static void AddIfNotNull(this ArrayBuilder builder, T? value)
}
}
- public static void AddIfNotNull(this ArrayBuilder builder, T value)
+ public static void AddIfNotNull(this ArrayBuilder builder, T? value)
where T : class
{
if (value != null)
@@ -186,6 +188,7 @@ public static void AddIfNotNull(this ArrayBuilder builder, T value)
}
}
+#nullable disable
public static void FreeAll(this ArrayBuilder builder, Func> getNested)
{
foreach (var item in builder)
@@ -194,5 +197,6 @@ public static void FreeAll(this ArrayBuilder builder, Func element)
// as JIT does not know if the write goes to a stack or a heap location.
// Assigning to Value directly easily avoids all this redundancy.
- public static ArrayElement[] MakeElementArray(T[] items)
+ [return: NotNullIfNotNull(parameterName: "items")]
+ public static ArrayElement[]? MakeElementArray(T[]? items)
{
if (items == null)
{
@@ -41,7 +45,8 @@ public static ArrayElement[] MakeElementArray(T[] items)
return array;
}
- public static T[] MakeArray(ArrayElement[] items)
+ [return: NotNullIfNotNull(parameterName: "items")]
+ public static T[]? MakeArray(ArrayElement[]? items)
{
if (items == null)
{
diff --git a/src/Compilers/Core/Portable/Collections/Boxes.cs b/src/Compilers/Core/Portable/Collections/Boxes.cs
index f116799df0d32..e96e930dabc0a 100644
--- a/src/Compilers/Core/Portable/Collections/Boxes.cs
+++ b/src/Compilers/Core/Portable/Collections/Boxes.cs
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System;
namespace Microsoft.CodeAnalysis
@@ -21,7 +23,7 @@ internal static class Boxes
public static readonly object BoxedDoubleZero = 0.0;
public static readonly object BoxedDecimalZero = 0m;
- private static readonly object[] s_boxedAsciiChars = new object[128];
+ private static readonly object?[] s_boxedAsciiChars = new object?[128];
public static object Box(bool b)
{
diff --git a/src/Compilers/Core/Portable/Collections/ByteSequenceComparer.cs b/src/Compilers/Core/Portable/Collections/ByteSequenceComparer.cs
index b3180b9ae077b..2d030dfd742f6 100644
--- a/src/Compilers/Core/Portable/Collections/ByteSequenceComparer.cs
+++ b/src/Compilers/Core/Portable/Collections/ByteSequenceComparer.cs
@@ -1,9 +1,12 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
@@ -41,7 +44,7 @@ internal static bool Equals(ImmutableArray x, ImmutableArray y)
return true;
}
- internal static bool Equals(byte[] left, int leftStart, byte[] right, int rightStart, int length)
+ internal static bool Equals(byte[]? left, int leftStart, byte[]? right, int rightStart, int length)
{
if (left == null || right == null)
{
@@ -64,7 +67,7 @@ internal static bool Equals(byte[] left, int leftStart, byte[] right, int rightS
return true;
}
- internal static bool Equals(byte[] left, byte[] right)
+ internal static bool Equals(byte[]? left, byte[]? right)
{
if (ReferenceEquals(left, right))
{
@@ -91,7 +94,7 @@ internal static bool Equals(byte[] left, byte[] right)
internal static int GetHashCode(byte[] x)
{
- Debug.Assert(x != null);
+ RoslynDebug.Assert(x != null);
return Hash.GetFNVHashCode(x);
}
@@ -101,7 +104,7 @@ internal static int GetHashCode(ImmutableArray x)
return Hash.GetFNVHashCode(x);
}
- bool IEqualityComparer.Equals(byte[] x, byte[] y)
+ bool IEqualityComparer.Equals(byte[]? x, byte[]? y)
{
return Equals(x, y);
}
diff --git a/src/Compilers/Core/Portable/Collections/CachingDictionary.cs b/src/Compilers/Core/Portable/Collections/CachingDictionary.cs
index c0d94d17afa33..26557fbd859ab 100644
--- a/src/Compilers/Core/Portable/Collections/CachingDictionary.cs
+++ b/src/Compilers/Core/Portable/Collections/CachingDictionary.cs
@@ -1,10 +1,13 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
using System.Threading;
using Microsoft.CodeAnalysis.PooledObjects;
@@ -25,6 +28,7 @@ namespace Microsoft.CodeAnalysis.Collections
/// Thread safe.
///
internal class CachingDictionary
+ where TKey : notnull
{
private readonly Func> _getElementsOfKey;
private readonly Func, HashSet> _getKeys;
@@ -34,7 +38,7 @@ internal class CachingDictionary
// or something frozen (usually a regular Dictionary). The frozen Dictionary is used only once the collection
// is fully populated. This is a memory optimization so that we don't hold onto relatively ConcurrentDictionary
// instances once the cache is fully populated.
- private IDictionary> _map;
+ private IDictionary>? _map;
// This is a special sentinel value that is placed inside the map to indicate that a key was looked
// up, but not found.
@@ -142,7 +146,7 @@ private IDictionary> CreateDictionaryForFullyPopu
private ImmutableArray GetOrCreateValue(TKey key)
{
ImmutableArray elements;
- ConcurrentDictionary> concurrentMap;
+ ConcurrentDictionary>? concurrentMap;
// Check if we're fully populated before trying to retrieve the elements. If we are
// and we don't get any elements back, then we don't have to go any further.
@@ -197,7 +201,7 @@ private ImmutableArray AddToConcurrentMap(ConcurrentDictionary
/// The map to test.
/// true if the map is fully populated.
- private static bool IsNotFullyPopulatedMap(IDictionary> existingMap)
+ private static bool IsNotFullyPopulatedMap([NotNullWhen(returnValue: false)] IDictionary>? existingMap)
{
return existingMap == null || existingMap is ConcurrentDictionary>;
}
@@ -207,7 +211,7 @@ private static bool IsNotFullyPopulatedMap(IDictionary
/// The existing map which may be null or a ConcurrentDictionary.
///
- private IDictionary> CreateFullyPopulatedMap(IDictionary> existingMap)
+ private IDictionary> CreateFullyPopulatedMap(IDictionary>? existingMap)
{
Debug.Assert(IsNotFullyPopulatedMap(existingMap));
@@ -250,7 +254,7 @@ private IDictionary> CreateFullyPopulatedMap(IDic
///
private IDictionary> EnsureFullyPopulated()
{
- IDictionary> fullyPopulatedMap = null;
+ IDictionary>? fullyPopulatedMap = null;
var currentMap = _map;
while (IsNotFullyPopulatedMap(currentMap))
diff --git a/src/Compilers/Core/Portable/Collections/ConsListExtensions.cs b/src/Compilers/Core/Portable/Collections/ConsListExtensions.cs
index 91eafacb1eec6..b38f872b6643e 100644
--- a/src/Compilers/Core/Portable/Collections/ConsListExtensions.cs
+++ b/src/Compilers/Core/Portable/Collections/ConsListExtensions.cs
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
@@ -10,7 +12,7 @@ namespace Microsoft.CodeAnalysis
///
internal static class ConsListExtensions
{
- public static ConsList Prepend(this ConsList list, T head)
+ public static ConsList Prepend(this ConsList? list, T head)
{
return new ConsList(head, list ?? ConsList.Empty);
}
diff --git a/src/Compilers/Core/Portable/Collections/HashSetExtensions.cs b/src/Compilers/Core/Portable/Collections/HashSetExtensions.cs
index 52b721789a0dc..7e47f5ad0bdf9 100644
--- a/src/Compilers/Core/Portable/Collections/HashSetExtensions.cs
+++ b/src/Compilers/Core/Portable/Collections/HashSetExtensions.cs
@@ -1,17 +1,21 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
namespace Microsoft.CodeAnalysis
{
internal static class HashSetExtensions
{
- internal static bool IsNullOrEmpty(this HashSet hashSet)
+ internal static bool IsNullOrEmpty([NotNullWhen(returnValue: false)] this HashSet? hashSet)
{
return hashSet == null || hashSet.Count == 0;
}
- internal static bool InitializeAndAdd(ref HashSet hashSet, T item) where T : class
+ internal static bool InitializeAndAdd([NotNullIfNotNull(parameterName: "item"), NotNullWhen(returnValue: true)] ref HashSet? hashSet, [NotNullWhen(returnValue: true)] T? item)
+ where T : class
{
if (item is null)
{
diff --git a/src/Compilers/Core/Portable/Collections/IOrderedReadOnlySet.cs b/src/Compilers/Core/Portable/Collections/IOrderedReadOnlySet.cs
index 4adb4530f1506..72c8df9ae1666 100644
--- a/src/Compilers/Core/Portable/Collections/IOrderedReadOnlySet.cs
+++ b/src/Compilers/Core/Portable/Collections/IOrderedReadOnlySet.cs
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System.Collections.Generic;
using Roslyn.Utilities;
diff --git a/src/Compilers/Core/Portable/Collections/ImmutableArrayExtensions.cs b/src/Compilers/Core/Portable/Collections/ImmutableArrayExtensions.cs
index e7a575337fd72..b61429abf7984 100644
--- a/src/Compilers/Core/Portable/Collections/ImmutableArrayExtensions.cs
+++ b/src/Compilers/Core/Portable/Collections/ImmutableArrayExtensions.cs
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
@@ -36,7 +38,7 @@ public static ImmutableArray AsImmutable(this IEnumerable items)
/// The sequence to convert.
/// An immutable copy of the contents of the sequence.
/// If the sequence is null, this will return an empty array.
- public static ImmutableArray AsImmutableOrEmpty(this IEnumerable items)
+ public static ImmutableArray AsImmutableOrEmpty(this IEnumerable? items)
{
if (items == null)
{
@@ -53,7 +55,7 @@ public static ImmutableArray AsImmutableOrEmpty(this IEnumerable items)
/// The sequence to convert.
/// An immutable copy of the contents of the sequence.
/// If the sequence is null, this will return the default (null) array.
- public static ImmutableArray AsImmutableOrNull(this IEnumerable items)
+ public static ImmutableArray AsImmutableOrNull(this IEnumerable? items)
{
if (items == null)
{
@@ -82,7 +84,7 @@ public static ImmutableArray AsImmutable(this T[] items)
/// The sequence to convert
///
/// If the sequence is null, this will return the default (null) array.
- public static ImmutableArray AsImmutableOrNull(this T[] items)
+ public static ImmutableArray AsImmutableOrNull(this T[]? items)
{
if (items == null)
{
@@ -98,7 +100,7 @@ public static ImmutableArray AsImmutableOrNull(this T[] items)
///
/// The sequence to convert
/// If the array is null, this will return an empty immutable array.
- public static ImmutableArray AsImmutableOrEmpty(this T[] items)
+ public static ImmutableArray AsImmutableOrEmpty(this T[]? items)
{
if (items == null)
{
@@ -260,7 +262,7 @@ public static ImmutableArray WhereAsArray(this ImmutableArray array, Fu
{
Debug.Assert(!array.IsDefault);
- ArrayBuilder builder = null;
+ ArrayBuilder? builder = null;
bool none = true;
bool all = true;
@@ -391,7 +393,7 @@ public static ImmutableArray NullToEmpty(this ImmutableArray array)
/// Returns an array of distinct elements, preserving the order in the original array.
/// If the array has no duplicates, the original array is returned. The original array must not be null.
///
- public static ImmutableArray Distinct(this ImmutableArray array, IEqualityComparer comparer = null)
+ public static ImmutableArray Distinct(this ImmutableArray array, IEqualityComparer? comparer = null)
{
Debug.Assert(!array.IsDefault);
@@ -448,7 +450,8 @@ internal static ImmutableArray ConditionallyDeOrder(this ImmutableArray
internal static ImmutableArray Flatten(
this Dictionary> dictionary,
- IComparer comparer = null)
+ IComparer? comparer = null)
+ where TKey : notnull
{
if (dictionary.Count == 0)
{
@@ -525,7 +528,7 @@ public static int Count(this ImmutableArray items, Func predicate
return count;
}
- internal static Dictionary> ToDictionary(this ImmutableArray items, Func keySelector, IEqualityComparer comparer = null)
+ internal static Dictionary> ToDictionary(this ImmutableArray items, Func keySelector, IEqualityComparer? comparer = null)
{
if (items.Length == 1)
{
diff --git a/src/Compilers/Core/Portable/Collections/KeyedStack.cs b/src/Compilers/Core/Portable/Collections/KeyedStack.cs
index 0f7be09dc0805..c3e7299390dab 100644
--- a/src/Compilers/Core/Portable/Collections/KeyedStack.cs
+++ b/src/Compilers/Core/Portable/Collections/KeyedStack.cs
@@ -1,7 +1,10 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System;
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -10,12 +13,13 @@
namespace Microsoft.CodeAnalysis.Collections
{
internal class KeyedStack
+ where T : notnull
{
private readonly Dictionary> _dict = new Dictionary>();
public void Push(T key, R value)
{
- Stack store;
+ Stack? store;
if (!_dict.TryGetValue(key, out store))
{
store = new Stack();
@@ -25,16 +29,16 @@ public void Push(T key, R value)
store.Push(value);
}
- public bool TryPop(T key, out R value)
+ public bool TryPop(T key, [MaybeNullWhen(returnValue: false)] out R value)
{
- Stack store;
+ Stack? store;
if (_dict.TryGetValue(key, out store) && store.Count > 0)
{
value = store.Pop();
return true;
}
- value = default(R);
+ value = default(R)!;
return false;
}
}
diff --git a/src/Compilers/Core/Portable/Collections/StaticCast.cs b/src/Compilers/Core/Portable/Collections/StaticCast.cs
index 2cfb6fce8b981..ce3d40d07bf4a 100644
--- a/src/Compilers/Core/Portable/Collections/StaticCast.cs
+++ b/src/Compilers/Core/Portable/Collections/StaticCast.cs
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System.Collections.Immutable;
namespace Microsoft.CodeAnalysis
diff --git a/src/Compilers/Core/Portable/Compilation/XmlReferenceResolver.cs b/src/Compilers/Core/Portable/Compilation/XmlReferenceResolver.cs
index 3c2047e66d105..9f9d6cd6a67d6 100644
--- a/src/Compilers/Core/Portable/Compilation/XmlReferenceResolver.cs
+++ b/src/Compilers/Core/Portable/Compilation/XmlReferenceResolver.cs
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System;
using System.IO;
@@ -14,7 +16,7 @@ protected XmlReferenceResolver()
{
}
- public abstract override bool Equals(object other);
+ public abstract override bool Equals(object? other);
public abstract override int GetHashCode();
///
@@ -23,7 +25,7 @@ protected XmlReferenceResolver()
/// The reference path to resolve. May be absolute or relative path.
/// Path of the source file that contains the (may also be relative), or null if not available.
/// Path to the XML artifact, or null if the file can't be resolved.
- public abstract string ResolveReference(string path, string baseFilePath);
+ public abstract string? ResolveReference(string path, string? baseFilePath);
///
/// Opens a that allows reading the content of the specified file.
diff --git a/src/Compilers/Core/Portable/DocumentationMode.cs b/src/Compilers/Core/Portable/DocumentationMode.cs
index 0e644b4fae11c..40eb83140b897 100644
--- a/src/Compilers/Core/Portable/DocumentationMode.cs
+++ b/src/Compilers/Core/Portable/DocumentationMode.cs
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis
diff --git a/src/Compilers/Core/Portable/IVTConclusion.cs b/src/Compilers/Core/Portable/IVTConclusion.cs
index 84cbb8e366696..ed65fa6e76f8a 100644
--- a/src/Compilers/Core/Portable/IVTConclusion.cs
+++ b/src/Compilers/Core/Portable/IVTConclusion.cs
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System.Collections.Immutable;
namespace Microsoft.CodeAnalysis
diff --git a/src/Compilers/Core/Portable/InternalUtilities/ArrayExtensions.cs b/src/Compilers/Core/Portable/InternalUtilities/ArrayExtensions.cs
index 617f50c70bce9..57c595b66dc8c 100644
--- a/src/Compilers/Core/Portable/InternalUtilities/ArrayExtensions.cs
+++ b/src/Compilers/Core/Portable/InternalUtilities/ArrayExtensions.cs
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System;
using System.Diagnostics;
diff --git a/src/Compilers/Core/Portable/InternalUtilities/AssemblyIdentityUtils.cs b/src/Compilers/Core/Portable/InternalUtilities/AssemblyIdentityUtils.cs
index 31aef7ba6a0b8..affcbd177130b 100644
--- a/src/Compilers/Core/Portable/InternalUtilities/AssemblyIdentityUtils.cs
+++ b/src/Compilers/Core/Portable/InternalUtilities/AssemblyIdentityUtils.cs
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System;
using System.Collections.Immutable;
using System.Reflection;
@@ -12,7 +14,7 @@ namespace Microsoft.CodeAnalysis
{
internal static class AssemblyIdentityUtils
{
- public static AssemblyIdentity TryGetAssemblyIdentity(string filePath)
+ public static AssemblyIdentity? TryGetAssemblyIdentity(string filePath)
{
try
{
@@ -27,7 +29,7 @@ public static AssemblyIdentity TryGetAssemblyIdentity(string filePath)
Version version = assemblyDefinition.Version;
StringHandle cultureHandle = assemblyDefinition.Culture;
- string cultureName = (!cultureHandle.IsNil) ? metadataReader.GetString(cultureHandle) : null;
+ string? cultureName = (!cultureHandle.IsNil) ? metadataReader.GetString(cultureHandle) : null;
AssemblyFlags flags = assemblyDefinition.Flags;
bool hasPublicKey = (flags & AssemblyFlags.PublicKey) != 0;
diff --git a/src/Compilers/Core/Portable/InternalUtilities/BitArithmeticUtilities.cs b/src/Compilers/Core/Portable/InternalUtilities/BitArithmeticUtilities.cs
index c620827158c75..65cce23989431 100644
--- a/src/Compilers/Core/Portable/InternalUtilities/BitArithmeticUtilities.cs
+++ b/src/Compilers/Core/Portable/InternalUtilities/BitArithmeticUtilities.cs
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System.Diagnostics;
namespace Roslyn.Utilities
diff --git a/src/Compilers/Core/Portable/InternalUtilities/CharMemoryEqualityComparer.cs b/src/Compilers/Core/Portable/InternalUtilities/CharMemoryEqualityComparer.cs
index 5c73939926077..0ae0a69548819 100644
--- a/src/Compilers/Core/Portable/InternalUtilities/CharMemoryEqualityComparer.cs
+++ b/src/Compilers/Core/Portable/InternalUtilities/CharMemoryEqualityComparer.cs
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System;
using System.Collections.Generic;
diff --git a/src/Compilers/Core/Portable/InternalUtilities/CommandLineUtilities.cs b/src/Compilers/Core/Portable/InternalUtilities/CommandLineUtilities.cs
index 5a8b91e08a959..8f2be002494bb 100644
--- a/src/Compilers/Core/Portable/InternalUtilities/CommandLineUtilities.cs
+++ b/src/Compilers/Core/Portable/InternalUtilities/CommandLineUtilities.cs
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System.Collections.Generic;
using System.Text;
diff --git a/src/Compilers/Core/Portable/InternalUtilities/CompilerOptionParseUtilities.cs b/src/Compilers/Core/Portable/InternalUtilities/CompilerOptionParseUtilities.cs
index 0a6b572d9df0b..ae3a1d439bad7 100644
--- a/src/Compilers/Core/Portable/InternalUtilities/CompilerOptionParseUtilities.cs
+++ b/src/Compilers/Core/Portable/InternalUtilities/CompilerOptionParseUtilities.cs
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System;
using System.Collections.Generic;
@@ -11,9 +13,9 @@ internal static class CompilerOptionParseUtilities
/// Parse the value provided to an MSBuild Feature option into a list of entries. This will
/// leave name=value in their raw form.
///
- public static IList ParseFeatureFromMSBuild(string features)
+ public static IList ParseFeatureFromMSBuild(string? features)
{
- if (string.IsNullOrEmpty(features))
+ if (RoslynString.IsNullOrEmpty(features))
{
return new List(capacity: 0);
}
diff --git a/src/Compilers/Core/Portable/InternalUtilities/ConcurrentDictionaryExtensions.cs b/src/Compilers/Core/Portable/InternalUtilities/ConcurrentDictionaryExtensions.cs
index 5aa1679ef05c2..9cddc596d154d 100644
--- a/src/Compilers/Core/Portable/InternalUtilities/ConcurrentDictionaryExtensions.cs
+++ b/src/Compilers/Core/Portable/InternalUtilities/ConcurrentDictionaryExtensions.cs
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System.Collections.Concurrent;
namespace Roslyn.Utilities
@@ -13,6 +15,7 @@ internal static class ConcurrentDictionaryExtensions
/// If unsure about adding unique items use APIs such as TryAdd, GetOrAdd, etc...
///
public static void Add(this ConcurrentDictionary dict, K key, V value)
+ where K : notnull
{
if (!dict.TryAdd(key, value))
{
diff --git a/src/Compilers/Core/Portable/InternalUtilities/ConcurrentLruCache.cs b/src/Compilers/Core/Portable/InternalUtilities/ConcurrentLruCache.cs
index 45c6043d6cdf5..92b3bcf53d1f5 100644
--- a/src/Compilers/Core/Portable/InternalUtilities/ConcurrentLruCache.cs
+++ b/src/Compilers/Core/Portable/InternalUtilities/ConcurrentLruCache.cs
@@ -1,8 +1,11 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System;
using System.Collections.Generic;
using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
namespace Microsoft.CodeAnalysis.InternalUtilities
{
@@ -11,6 +14,8 @@ namespace Microsoft.CodeAnalysis.InternalUtilities
/// Thread-safe.
///
internal class ConcurrentLruCache
+ where K : notnull
+ where V : notnull
{
private readonly int _capacity;
@@ -157,7 +162,7 @@ public V this[K key]
}
}
- public bool TryGetValue(K key, out V value)
+ public bool TryGetValue(K key, [MaybeNullWhen(returnValue: false)] out V value)
{
lock (_lockObject)
{
@@ -168,7 +173,7 @@ public bool TryGetValue(K key, out V value)
///
/// Doesn't lock.
///
- public bool UnsafeTryGetValue(K key, out V value)
+ public bool UnsafeTryGetValue(K key, [MaybeNullWhen(returnValue: false)] out V value)
{
if (_cache.TryGetValue(key, out var result))
{
@@ -178,7 +183,7 @@ public bool UnsafeTryGetValue(K key, out V value)
}
else
{
- value = default;
+ value = default!;
return false;
}
}
diff --git a/src/Compilers/Core/Portable/InternalUtilities/ConcurrentSet.cs b/src/Compilers/Core/Portable/InternalUtilities/ConcurrentSet.cs
index 302b46a49a41e..c471c4a667e16 100644
--- a/src/Compilers/Core/Portable/InternalUtilities/ConcurrentSet.cs
+++ b/src/Compilers/Core/Portable/InternalUtilities/ConcurrentSet.cs
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System;
using System.Collections;
using System.Collections.Concurrent;
@@ -13,6 +15,7 @@ namespace Roslyn.Utilities
///
[DebuggerDisplay("Count = {Count}")]
internal sealed class ConcurrentSet : ICollection
+ where T : notnull
{
///
/// The default concurrency level is 2. That means the collection can cope with up to two
@@ -82,7 +85,7 @@ public bool Add(T value)
return _dictionary.TryAdd(value, 0);
}
- public void AddRange(IEnumerable values)
+ public void AddRange(IEnumerable? values)
{
if (values != null)
{
diff --git a/src/Compilers/Core/Portable/InternalUtilities/ConsList`1.cs b/src/Compilers/Core/Portable/InternalUtilities/ConsList`1.cs
index 4358b91d97237..8646de6ec59ab 100644
--- a/src/Compilers/Core/Portable/InternalUtilities/ConsList`1.cs
+++ b/src/Compilers/Core/Portable/InternalUtilities/ConsList`1.cs
@@ -1,9 +1,12 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
using System.Text;
namespace Roslyn.Utilities
@@ -15,11 +18,13 @@ internal class ConsList : IEnumerable
{
public static readonly ConsList Empty = new ConsList();
+ [AllowNull, MaybeNull]
private readonly T _head;
- private readonly ConsList _tail;
+ private readonly ConsList? _tail;
internal struct Enumerator : IEnumerator
{
+ [AllowNull, MaybeNull]
private T _current;
private ConsList _tail;
@@ -34,7 +39,9 @@ public T Current
get
{
Debug.Assert(_tail != null);
- return _current;
+
+ // This never returns null after a proper call to `MoveNext` returned true.
+ return _current!;
}
}
@@ -45,7 +52,9 @@ public bool MoveNext()
if (newTail != null)
{
- _current = currentTail._head;
+ // Suppress false positive CS8717 reported for MaybeNull assignment to AllowNull
+ // https://github.com/dotnet/roslyn/issues/38926
+ _current = currentTail._head!;
_tail = newTail;
return true;
}
@@ -58,7 +67,7 @@ public void Dispose()
{
}
- object IEnumerator.Current
+ object? IEnumerator.Current
{
get
{
@@ -92,7 +101,7 @@ public T Head
get
{
Debug.Assert(this != Empty);
- return _head;
+ return _head!;
}
}
@@ -102,6 +111,7 @@ public ConsList Tail
get
{
Debug.Assert(this != Empty);
+ RoslynDebug.Assert(_tail is object);
return _tail;
}
}
@@ -142,7 +152,7 @@ public override string ToString()
result.Append(", ");
}
- result.Append(list._head);
+ result.Append(list.Head);
any = true;
}
diff --git a/src/Compilers/Core/Portable/InternalUtilities/DecimalUtilities.cs b/src/Compilers/Core/Portable/InternalUtilities/DecimalUtilities.cs
index 9e77c6735c1c4..62fc285951fde 100644
--- a/src/Compilers/Core/Portable/InternalUtilities/DecimalUtilities.cs
+++ b/src/Compilers/Core/Portable/InternalUtilities/DecimalUtilities.cs
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
namespace Roslyn.Utilities
{
internal static class DecimalUtilities
diff --git a/src/Compilers/Core/Portable/InternalUtilities/DocumentationCommentXmlNames.cs b/src/Compilers/Core/Portable/InternalUtilities/DocumentationCommentXmlNames.cs
index 4b33c0b00aeae..e1e5c481a584b 100644
--- a/src/Compilers/Core/Portable/InternalUtilities/DocumentationCommentXmlNames.cs
+++ b/src/Compilers/Core/Portable/InternalUtilities/DocumentationCommentXmlNames.cs
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System;
namespace Roslyn.Utilities
diff --git a/src/Compilers/Core/Portable/InternalUtilities/EmptyComparer.cs b/src/Compilers/Core/Portable/InternalUtilities/EmptyComparer.cs
index 3e829ccd756be..0acd13fae8639 100644
--- a/src/Compilers/Core/Portable/InternalUtilities/EmptyComparer.cs
+++ b/src/Compilers/Core/Portable/InternalUtilities/EmptyComparer.cs
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+#nullable enable
+
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
@@ -18,7 +20,7 @@ private EmptyComparer()
{
}
- bool IEqualityComparer