diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Environment.CoreCLR.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Environment.CoreCLR.cs index 59b2bd9d79040b..f141b1e9cfeb73 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Environment.CoreCLR.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Environment.CoreCLR.cs @@ -16,6 +16,7 @@ public static partial class Environment // Terminates this process with the given exit code. [DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)] + [DoesNotReturn] private static extern void _Exit(int exitCode); [DoesNotReturn] diff --git a/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs b/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs index a21c8d894b12d5..7a9b6c0f1d50b8 100644 --- a/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs +++ b/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs @@ -2,6 +2,8 @@ // 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.CodeAnalysis; using System.Runtime.CompilerServices; namespace System.Collections.Generic @@ -15,7 +17,7 @@ private ReferenceEqualityComparer() { } - public bool Equals(T x, T y) + public bool Equals(T? x, T? y) { return ReferenceEquals(x, y); } diff --git a/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs b/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs index ff155065feee20..4ef5ecdf2927a3 100644 --- a/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs +++ b/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs @@ -918,11 +918,13 @@ public TValue this[TKey key] // as these are uncommonly needed and when inlined are observed to prevent the inlining // of important methods like TryGetValue and ContainsKey. + [DoesNotReturn] private static void ThrowKeyNotFoundException(object key) { throw new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key.ToString())); } + [DoesNotReturn] private static void ThrowKeyNullException() { throw new ArgumentNullException("key"); diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IImmutableListQueries.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IImmutableListQueries.cs index 36bcbe5e0e0021..0a6e1f904d34ad 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IImmutableListQueries.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IImmutableListQueries.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace System.Collections.Immutable { @@ -120,6 +121,7 @@ internal interface IImmutableListQueries : IReadOnlyList /// The first element that matches the conditions defined by the specified predicate, /// if found; otherwise, the default value for type . /// + [return: MaybeNull] T Find(Predicate match); /// @@ -193,6 +195,7 @@ internal interface IImmutableListQueries : IReadOnlyList /// The last element that matches the conditions defined by the specified predicate, /// if found; otherwise, the default value for type . /// + [return: MaybeNull] T FindLast(Predicate match); /// diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.cs index 8fc536af27da4a..e740e1afc442aa 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.cs @@ -221,7 +221,7 @@ public ImmutableHashSet Remove(T item) [Pure] public bool TryGetValue(T equalValue, out T actualValue) { - int hashCode = _equalityComparer.GetHashCode(equalValue); + int hashCode = equalValue != null ? _equalityComparer.GetHashCode(equalValue) : 0; HashBucket bucket; if (_root.TryGetValue(hashCode, out bucket)) { @@ -639,7 +639,7 @@ private static bool IsSupersetOf(IEnumerable other, MutationInput origin) private static MutationResult Add(T item, MutationInput origin) { OperationResult result; - int hashCode = origin.EqualityComparer.GetHashCode(item); + int hashCode = item != null ? origin.EqualityComparer.GetHashCode(item) : 0; HashBucket bucket = origin.Root.GetValueOrDefault(hashCode); var newBucket = bucket.Add(item, origin.EqualityComparer, out result); if (result == OperationResult.NoChangeRequired) @@ -658,7 +658,7 @@ private static MutationResult Add(T item, MutationInput origin) private static MutationResult Remove(T item, MutationInput origin) { var result = OperationResult.NoChangeRequired; - int hashCode = origin.EqualityComparer.GetHashCode(item); + int hashCode = item != null ? origin.EqualityComparer.GetHashCode(item) : 0; HashBucket bucket; var newRoot = origin.Root; if (origin.Root.TryGetValue(hashCode, out bucket)) @@ -680,7 +680,7 @@ private static MutationResult Remove(T item, MutationInput origin) /// private static bool Contains(T item, MutationInput origin) { - int hashCode = origin.EqualityComparer.GetHashCode(item); + int hashCode = item != null ? origin.EqualityComparer.GetHashCode(item) : 0; HashBucket bucket; if (origin.Root.TryGetValue(hashCode, out bucket)) { @@ -701,7 +701,7 @@ private static MutationResult Union(IEnumerable other, MutationInput origin) var newRoot = origin.Root; foreach (var item in other.GetEnumerableDisposable()) { - int hashCode = origin.EqualityComparer.GetHashCode(item); + int hashCode = item != null ? origin.EqualityComparer.GetHashCode(item) : 0; HashBucket bucket = newRoot.GetValueOrDefault(hashCode); OperationResult result; var newBucket = bucket.Add(item, origin.EqualityComparer, out result); @@ -812,7 +812,7 @@ private static MutationResult Except(IEnumerable other, IEqualityComparer var newRoot = root; foreach (var item in other.GetEnumerableDisposable()) { - int hashCode = equalityComparer.GetHashCode(item); + int hashCode = item != null ? equalityComparer.GetHashCode(item) : 0; HashBucket bucket; if (newRoot.TryGetValue(hashCode, out bucket)) { diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/HashSetEqualityComparer.cs b/src/libraries/System.Collections/src/System/Collections/Generic/HashSetEqualityComparer.cs index 5cfae13f3f31ec..8415e534569903 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/HashSetEqualityComparer.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/HashSetEqualityComparer.cs @@ -30,7 +30,10 @@ public int GetHashCode(HashSet? obj) { foreach (T t in obj) { - hashCode = hashCode ^ (_comparer.GetHashCode(t) & 0x7FFFFFFF); + if (t != null) + { + hashCode ^= (_comparer.GetHashCode(t) & 0x7FFFFFFF); + } } } // else returns hashcode of 0 for null hashsets return hashCode; diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs index bcbc46e8b7042b..4611785eb9ed80 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs @@ -341,7 +341,7 @@ internal override int TotalCount() // This passes functionality down to the underlying tree, clipping edges if necessary // There's nothing gained by having a nested subset. May as well draw it from the base // Cannot increase the bounds of the subset, can only decrease it - public override SortedSet GetViewBetween(T lowerValue, T upperValue) + public override SortedSet GetViewBetween([AllowNull] T lowerValue, [AllowNull] T upperValue) { if (_lBoundActive && Comparer.Compare(_min, lowerValue) > 0) { diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.cs b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.cs index d82c4052aa86ec..c896834c997103 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.cs @@ -806,7 +806,7 @@ public static IEqualityComparer> CreateSetComparer(IEqualityCompare /// The second set. /// The fallback comparer to use if the sets do not have equal comparers. /// true if the sets have equal contents; otherwise, false. - internal static bool SortedSetEquals(SortedSet set1, SortedSet set2, IComparer comparer) + internal static bool SortedSetEquals(SortedSet? set1, SortedSet? set2, IComparer comparer) { if (set1 == null) { diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSetEqualityComparer.cs b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSetEqualityComparer.cs index e0e59ff8704c95..dc262258981d24 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSetEqualityComparer.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSetEqualityComparer.cs @@ -27,7 +27,7 @@ private SortedSetEqualityComparer(IComparer? comparer, IEqualityComparer? } // Use _comparer to keep equals properties intact; don't want to choose one of the comparers. - public bool Equals(SortedSet x, SortedSet y) => SortedSet.SortedSetEquals(x, y, _comparer); + public bool Equals(SortedSet? x, SortedSet? y) => SortedSet.SortedSetEquals(x, y, _comparer); // IMPORTANT: this part uses the fact that GetHashCode() is consistent with the notion of equality in the set. public int GetHashCode(SortedSet obj) @@ -37,7 +37,10 @@ public int GetHashCode(SortedSet obj) { foreach (T t in obj) { - hashCode = hashCode ^ (_memberEqualityComparer.GetHashCode(t) & 0x7FFFFFFF); + if (t != null) + { + hashCode ^= (_memberEqualityComparer.GetHashCode(t) & 0x7FFFFFFF); + } } } // Returns 0 for null sets. diff --git a/src/libraries/System.ComponentModel.Composition/src/Microsoft/Internal/Collections/CollectionServices.cs b/src/libraries/System.ComponentModel.Composition/src/Microsoft/Internal/Collections/CollectionServices.cs index 71a388e0e25e3c..6a1af13a3d4ae2 100644 --- a/src/libraries/System.ComponentModel.Composition/src/Microsoft/Internal/Collections/CollectionServices.cs +++ b/src/libraries/System.ComponentModel.Composition/src/Microsoft/Internal/Collections/CollectionServices.cs @@ -6,6 +6,7 @@ using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Diagnostics; using System.Linq; namespace Microsoft.Internal.Collections @@ -144,11 +145,13 @@ private static List FastAppendToListAllowNulls(this List? source, T val } public static List? FastAppendToListAllowNulls( - this List? source, T value, + this List? source, T? value, IEnumerable? second) + where T : class { if (second == null) { + Debug.Assert(value != null); source = source.FastAppendToListAllowNulls(value); } else diff --git a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartDefinition.cs b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartDefinition.cs index 039e4642888196..31d7d84f6ced12 100644 --- a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartDefinition.cs +++ b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartDefinition.cs @@ -103,7 +103,7 @@ protected ComposablePartDefinition() /// public abstract ComposablePart CreatePart(); - internal virtual bool TryGetExports(ImportDefinition definition, [NotNullWhen(true)] out Tuple? singleMatch, out IEnumerable>? multipleMatches) + internal virtual bool TryGetExports(ImportDefinition definition, out Tuple? singleMatch, out IEnumerable>? multipleMatches) { singleMatch = null; multipleMatches = null; diff --git a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ImportType.cs b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ImportType.cs index ebc0705dd8cde5..0f02be5bb1d2e3 100644 --- a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ImportType.cs +++ b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ImportType.cs @@ -23,13 +23,13 @@ internal class ImportType private readonly bool _isOpenGeneric = false; [ThreadStatic] - internal static Dictionary>? _castSingleValueCache; + internal static Dictionary?>? _castSingleValueCache; - private static Dictionary> CastSingleValueCache + private static Dictionary?> CastSingleValueCache { get { - return _castSingleValueCache = _castSingleValueCache ?? new Dictionary>(); + return _castSingleValueCache = _castSingleValueCache ?? new Dictionary?>(); } } @@ -170,7 +170,7 @@ private static bool IsLazyGenericType(Type genericType) return (genericType == LazyOfTType) || (genericType == LazyOfTMType); } - private static bool TryGetCastFunction(Type genericType, bool isOpenGeneric, Type[] arguments, [NotNullWhen(true)] out Func? castFunction) + private static bool TryGetCastFunction(Type genericType, bool isOpenGeneric, Type[] arguments, out Func? castFunction) { castFunction = null; diff --git a/src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/CollectionExtensions.cs b/src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/CollectionExtensions.cs index cd655d9f0366cc..329e585226469f 100644 --- a/src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/CollectionExtensions.cs +++ b/src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/CollectionExtensions.cs @@ -86,7 +86,10 @@ public static int ListHashCode(this ReadOnlyCollection list) int h = 6551; foreach (T t in list) { - h ^= (h << 5) ^ cmp.GetHashCode(t); + if (t != null) + { + h ^= (h << 5) ^ cmp.GetHashCode(t); + } } return h; } diff --git a/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LightCompiler.cs b/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LightCompiler.cs index 61520cc3599251..4959f96de8267c 100644 --- a/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LightCompiler.cs +++ b/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LightCompiler.cs @@ -108,7 +108,7 @@ internal bool HasHandler(InterpretedFrame frame, Exception exception, [NotNullWh // Unreachable. // Want to assert that this case isn't hit, but an assertion failure here will be eaten because // we are in an exception filter. Therefore return true here and assert in the catch block. - handler = null; + handler = null!; unwrappedException = exception; return true; } @@ -215,8 +215,9 @@ internal sealed class DebugInfo private class DebugInfoComparer : IComparer { //We allow comparison between int and DebugInfo here - int IComparer.Compare(DebugInfo d1, DebugInfo d2) + int IComparer.Compare(DebugInfo? d1, DebugInfo? d2) { + Debug.Assert(d1 != null && d2 != null); if (d1.Index > d2.Index) return 1; else if (d1.Index == d2.Index) return 0; else return -1; diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Partitioning/HashRepartitionStream.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Partitioning/HashRepartitionStream.cs index 0314d2bac60742..e90a0a9002585b 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Partitioning/HashRepartitionStream.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Partitioning/HashRepartitionStream.cs @@ -91,19 +91,16 @@ internal int GetHashCode(TInputOutput element) { return (HashCodeMask & - (_elementComparer == null ? - (element == null ? NULL_ELEMENT_HASH_CODE : element.GetHashCode()) : - _elementComparer.GetHashCode(element))) - % _distributionMod; + (element == null ? NULL_ELEMENT_HASH_CODE : (_elementComparer?.GetHashCode(element) ?? element.GetHashCode()))) + % _distributionMod; } internal int GetHashCode(THashKey key) { return (HashCodeMask & - (_keyComparer == null ? - (key == null ? NULL_ELEMENT_HASH_CODE : key.GetHashCode()) : - _keyComparer.GetHashCode(key))) % _distributionMod; + (key == null ? NULL_ELEMENT_HASH_CODE : (_keyComparer?.GetHashCode(key) ?? key.GetHashCode()))) + % _distributionMod; } } } diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/ForAllOperator.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/ForAllOperator.cs index 116170fd1d9de1..c50b7fc481c033 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/ForAllOperator.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/ForAllOperator.cs @@ -147,7 +147,7 @@ internal ForAllEnumerator(QueryOperatorEnumerator source, Action comparer) _comparer = comparer; } - public int Compare(T x, T y) + public int Compare([AllowNull] T x, [AllowNull] T y) { return _comparer.Compare(y, x); } diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Utils/WrapperEqualityComparer.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Utils/WrapperEqualityComparer.cs index 4f447720e260c6..b203042f5791ad 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Utils/WrapperEqualityComparer.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Utils/WrapperEqualityComparer.cs @@ -43,7 +43,8 @@ public bool Equals(Wrapper x, Wrapper y) public int GetHashCode(Wrapper x) { Debug.Assert(_comparer != null); - return _comparer.GetHashCode(x.Value); + T value = x.Value; + return value == null ? 0 : _comparer.GetHashCode(value); } } } diff --git a/src/libraries/System.Linq/src/System/Linq/Partition.SpeedOpt.cs b/src/libraries/System.Linq/src/System/Linq/Partition.SpeedOpt.cs index 4c59749a7dea15..2f61a5d61670d9 100644 --- a/src/libraries/System.Linq/src/System/Linq/Partition.SpeedOpt.cs +++ b/src/libraries/System.Linq/src/System/Linq/Partition.SpeedOpt.cs @@ -36,7 +36,6 @@ private EmptyPartition() public bool MoveNext() => false; [ExcludeFromCodeCoverage] // Shouldn't be called, and as undefined can return or throw anything anyway. - [MaybeNull] public TElement Current => default!; [ExcludeFromCodeCoverage] // Shouldn't be called, and as undefined can return or throw anything anyway. diff --git a/src/libraries/System.Private.CoreLib/src/System/Action.cs b/src/libraries/System.Private.CoreLib/src/System/Action.cs index 54ca7aaf5323d5..1c4771a1b64ff7 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Action.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Action.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Diagnostics.CodeAnalysis; + namespace System { public delegate void Action(); @@ -24,7 +26,7 @@ namespace System public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7); public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8); - public delegate int Comparison(T x, T y); + public delegate int Comparison([AllowNull] T x, [AllowNull] T y); public delegate TOutput Converter(TInput input); diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Comparer.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Comparer.cs index ec2535e2010ec7..31cc4ece91b1d1 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Comparer.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Comparer.cs @@ -43,7 +43,7 @@ public ComparisonComparer(Comparison comparison) _comparison = comparison; } - public override int Compare(T x, T y) + public override int Compare([AllowNull] T x, [AllowNull] T y) { return _comparison(x, y); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IEqualityComparer.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IEqualityComparer.cs index 61a6bd1e7422f8..29fde08191e2ed 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IEqualityComparer.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IEqualityComparer.cs @@ -8,7 +8,7 @@ namespace System.Collections.Generic { // The generic IEqualityComparer interface implements methods to if check two objects are equal // and generate Hashcode for an object. - // It is use in Dictionary class. + // It is used in Dictionary class. public interface IEqualityComparer { bool Equals([AllowNull] T x, [AllowNull] T y); diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.cs index cf4dfe1effdb81..6d199d7f786d9a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.cs @@ -5,6 +5,8 @@ // Do not remove this, it is needed to retain calls to these conditional methods in release builds #define DEBUG +using System.Diagnostics.CodeAnalysis; + namespace System.Diagnostics { /// @@ -12,6 +14,7 @@ namespace System.Diagnostics /// public partial class DebugProvider { + [DoesNotReturn] public virtual void Fail(string? message, string? detailMessage) { string stackTrace; @@ -25,7 +28,9 @@ public virtual void Fail(string? message, string? detailMessage) } WriteAssert(stackTrace, message, detailMessage); FailCore(stackTrace, message, detailMessage, "Assertion failed."); +#pragma warning disable 8763 // "A method marked [DoesNotReturn] should not return." } +#pragma warning restore 8763 internal void WriteAssert(string stackTrace, string? message, string? detailMessage) { diff --git a/src/libraries/System.Private.CoreLib/src/System/HashCode.cs b/src/libraries/System.Private.CoreLib/src/System/HashCode.cs index 0ff94adf17b981..6ea2c67d0e1eb9 100644 --- a/src/libraries/System.Private.CoreLib/src/System/HashCode.cs +++ b/src/libraries/System.Private.CoreLib/src/System/HashCode.cs @@ -303,7 +303,7 @@ public void Add(T value) public void Add(T value, IEqualityComparer? comparer) { - Add(comparer != null ? comparer.GetHashCode(value) : (value?.GetHashCode() ?? 0)); + Add(value is null ? 0 : (comparer?.GetHashCode(value) ?? value.GetHashCode())); } private void Add(int value) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs index 16d431cc2d2fd7..6fbb2f8cb9791e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs @@ -307,7 +307,9 @@ public static bool TryGetMemoryManager(ReadOnlyMemory memory, [N { TManager? localManager; // Use register for null comparison rather than byref manager = localManager = memory.GetObjectStartLength(out _, out _) as TManager; +#pragma warning disable 8762 // "Parameter 'manager' may not have a null value when exiting with 'true'." return localManager != null; +#pragma warning restore 8762 } /// @@ -335,7 +337,9 @@ public static bool TryGetMemoryManager(ReadOnlyMemory memory, [N length = default; return false; } +#pragma warning disable 8762 // "Parameter 'manager' may not have a null value when exiting with 'true'." return true; +#pragma warning restore 8762 } /// diff --git a/src/libraries/System.Private.CoreLib/src/System/SpanHelpers.BinarySearch.cs b/src/libraries/System.Private.CoreLib/src/System/SpanHelpers.BinarySearch.cs index 385992fe45aeeb..b50bed9581f993 100644 --- a/src/libraries/System.Private.CoreLib/src/System/SpanHelpers.BinarySearch.cs +++ b/src/libraries/System.Private.CoreLib/src/System/SpanHelpers.BinarySearch.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -75,7 +76,7 @@ public ComparerComparable(T value, TComparer comparer) } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int CompareTo(T other) => _comparer.Compare(_value, other); + public int CompareTo([AllowNull] T other) => _comparer.Compare(_value, other); } } } diff --git a/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/ByteSequenceComparer.cs b/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/ByteSequenceComparer.cs index cd89135d49b20e..b97d68de6960ef 100644 --- a/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/ByteSequenceComparer.cs +++ b/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/ByteSequenceComparer.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; namespace System.Reflection.Internal { @@ -62,7 +63,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)) { @@ -99,7 +100,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/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/RoAssemblyName.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/RoAssemblyName.cs index 293fb2aee61fb1..904d3cd67ee878 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/RoAssemblyName.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/RoAssemblyName.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; namespace System.Reflection.TypeLoading { @@ -49,8 +50,9 @@ public RoAssemblyName(string? name, Version? version, string? cultureName, byte[ // Equality - this compares every bit of data in the RuntimeAssemblyName which is acceptable for use as keys in a cache // where semantic duplication is permissible. This method is *not* meant to define ref->def binding rules or // assembly binding unification rules. - public bool Equals(RoAssemblyName other) + public bool Equals(RoAssemblyName? other) { + Debug.Assert(other is object); if (Name != other.Name) return false; if (Version != other.Version) diff --git a/src/libraries/System.Resources.Writer/src/System/Resources/__FastResourceComparer.cs b/src/libraries/System.Resources.Writer/src/System/Resources/__FastResourceComparer.cs index d884dec2a1e8b5..628e2e4b7e2c4c 100644 --- a/src/libraries/System.Resources.Writer/src/System/Resources/__FastResourceComparer.cs +++ b/src/libraries/System.Resources.Writer/src/System/Resources/__FastResourceComparer.cs @@ -6,6 +6,7 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; namespace System.Resources { @@ -33,16 +34,14 @@ internal static int HashFunction(string key) return (int)hash; } - public int Compare(string a, string b) + public int Compare(string? a, string? b) { return string.CompareOrdinal(a, b); } - public bool Equals(string a, string b) + public bool Equals(string? a, string? b) { return string.Equals(a, b); } - - } } diff --git a/src/libraries/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs b/src/libraries/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs index a06a6075660e67..1414b85ce3145d 100644 --- a/src/libraries/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs +++ b/src/libraries/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs @@ -683,12 +683,12 @@ public static partial class Environment public static System.Version Version { get { throw null; } } public static long WorkingSet { get { throw null; } } [System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute] - public static void Exit(int exitCode) { } + public static void Exit(int exitCode) => throw null; public static string ExpandEnvironmentVariables(string name) { throw null; } [System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute] - public static void FailFast(string? message) { } + public static void FailFast(string? message) => throw null; [System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute] - public static void FailFast(string? message, System.Exception? exception) { } + public static void FailFast(string? message, System.Exception? exception) => throw null; public static string[] GetCommandLineArgs() { throw null; } public static string? GetEnvironmentVariable(string variable) { throw null; } public static string? GetEnvironmentVariable(string variable, System.EnvironmentVariableTarget target) { throw null; } diff --git a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/ObjectProgress.cs b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/ObjectProgress.cs index 64c0a193330f26..94febe485f0733 100644 --- a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/ObjectProgress.cs +++ b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/ObjectProgress.cs @@ -76,7 +76,7 @@ internal void Init() internal void ArrayCountIncrement(int value) => _count += value; // Specifies what is to parsed next from the wire. - internal bool GetNext(out BinaryTypeEnum outBinaryTypeEnum, [NotNullWhen(true)] out object? outTypeInformation) + internal bool GetNext(out BinaryTypeEnum outBinaryTypeEnum, out object? outTypeInformation) { //Initialize the out params up here. outBinaryTypeEnum = BinaryTypeEnum.Primitive; diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 122e0037232134..5ac59a7713aa42 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -608,7 +608,7 @@ public sealed partial class CLSCompliantAttribute : System.Attribute public CLSCompliantAttribute(bool isCompliant) { } public bool IsCompliant { get { throw null; } } } - public delegate int Comparison(T x, T y); + public delegate int Comparison([System.Diagnostics.CodeAnalysis.AllowNullAttribute] T x, [System.Diagnostics.CodeAnalysis.AllowNullAttribute] T y); public delegate TOutput Converter(TInput input); public readonly partial struct DateTime : System.IComparable, System.IComparable, System.IConvertible, System.IEquatable, System.IFormattable, System.Runtime.Serialization.ISerializable { @@ -4213,10 +4213,10 @@ public static void Assert([System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttrib public static void Close() { } [System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute] [System.Diagnostics.ConditionalAttribute("DEBUG")] - public static void Fail(string? message) { } + public static void Fail(string? message) => throw null; [System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute] [System.Diagnostics.ConditionalAttribute("DEBUG")] - public static void Fail(string? message, string? detailMessage) { } + public static void Fail(string? message, string? detailMessage) => throw null; [System.Diagnostics.ConditionalAttribute("DEBUG")] public static void Flush() { } [System.Diagnostics.ConditionalAttribute("DEBUG")] @@ -7447,9 +7447,9 @@ internal ExceptionDispatchInfo() { } public static System.Runtime.ExceptionServices.ExceptionDispatchInfo Capture(System.Exception source) { throw null; } public static System.Exception SetCurrentStackTrace(System.Exception source) { throw null; } [System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute] - public void Throw() { } + public void Throw() => throw null; [System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute] - public static void Throw(System.Exception source) { } + public static void Throw(System.Exception source) => throw null; } public partial class FirstChanceExceptionEventArgs : System.EventArgs { diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReferenceEqualsEqualityComparer.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReferenceEqualsEqualityComparer.cs index 6f3667c2380657..495f4c0d75f4c6 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReferenceEqualsEqualityComparer.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReferenceEqualsEqualityComparer.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; +using System.Diagnostics.CodeAnalysis; namespace System.Text.Json.Serialization { @@ -15,7 +16,7 @@ internal sealed class ReferenceEqualsEqualityComparer : IEqualityComparer { public static ReferenceEqualsEqualityComparer Comparer = new ReferenceEqualsEqualityComparer(); - bool IEqualityComparer.Equals(T x, T y) + bool IEqualityComparer.Equals([AllowNull] T x, [AllowNull] T y) { return ReferenceEquals(x, y); } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs b/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs index a03e0c3e233d31..18723094128d26 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs @@ -19,7 +19,6 @@ public static void ThrowArgumentException_DeserializeWrongType(Type type, object throw new ArgumentException(SR.Format(SR.DeserializeWrongType, type, value.GetType())); } - [DoesNotReturn] [MethodImpl(MethodImplOptions.NoInlining)] public static NotSupportedException GetNotSupportedException_SerializationNotSupportedCollection(Type propertyType, Type? parentType, MemberInfo? memberInfo) { diff --git a/src/mono/netcore/System.Private.CoreLib/src/System/TypeIdentifier.cs b/src/mono/netcore/System.Private.CoreLib/src/System/TypeIdentifier.cs index 0460777d058ff1..24e07936bebda9 100644 --- a/src/mono/netcore/System.Private.CoreLib/src/System/TypeIdentifier.cs +++ b/src/mono/netcore/System.Private.CoreLib/src/System/TypeIdentifier.cs @@ -21,6 +21,8 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // +using System.Diagnostics.CodeAnalysis; + namespace System { // A TypeName is wrapper around type names in display form @@ -68,7 +70,7 @@ internal abstract class ATypeName : TypeName public abstract TypeName NestedName (TypeIdentifier innerName); - public bool Equals (TypeName other) + public bool Equals ([AllowNull] TypeName other) { return other != null && DisplayName == other.DisplayName; }