From c1a21a031925ce6261fadcfed26e098ab1bf46a6 Mon Sep 17 00:00:00 2001 From: Charles Stoner Date: Mon, 9 Nov 2020 20:11:57 -0800 Subject: [PATCH] Resolve warnings for assignment or explicit cast of possibly null type parameter value (#43925) --- .../Concurrent/BlockingCollection.cs | 6 +++--- .../Collections/Concurrent/ConcurrentBag.cs | 2 +- .../Concurrent/ConcurrentDictionary.cs | 18 +++++++++--------- .../Immutable/ImmutableSortedDictionary_2.cs | 2 +- .../Generic/CollectionExtensions.cs | 2 +- .../Collections/Generic/SortedDictionary.cs | 2 +- .../System/Collections/Generic/SortedSet.cs | 10 +++++----- .../Hosting/AtomicCompositionExtensions.cs | 4 ++-- .../Linq/Expressions/Interpreter/Utilities.cs | 2 +- .../OrderedHashRepartitionEnumerator.cs | 2 +- .../Binary/ExceptQueryOperator.cs | 2 +- .../Binary/HashJoinQueryOperatorEnumerator.cs | 2 +- .../Binary/IntersectQueryOperator.cs | 2 +- .../Inlined/LongCountAggregationOperator.cs | 4 ++-- .../QueryOperators/QueryOperatorEnumerator.cs | 2 +- .../Unary/DefaultIfEmptyQueryOperator.cs | 2 +- .../Unary/DistinctQueryOperator.cs | 2 +- .../QueryOperators/Unary/ForAllOperator.cs | 4 ++-- .../Unary/GroupByQueryOperator.cs | 4 ++-- .../Unary/ReverseQueryOperator.cs | 2 +- .../Unary/SelectQueryOperator.cs | 2 +- .../QueryOperators/Unary/SortQueryOperator.cs | 2 +- .../Unary/TakeOrSkipQueryOperator.cs | 2 +- .../Unary/TakeOrSkipWhileQueryOperator.cs | 2 +- .../QueryOperators/Unary/WhereQueryOperator.cs | 2 +- .../System/Linq/Parallel/Utils/HashLookup.cs | 4 ++-- .../System.Linq/src/System/Linq/ElementAt.cs | 2 +- .../System.Linq/src/System/Linq/First.cs | 4 ++-- .../System.Linq/src/System/Linq/Last.cs | 4 ++-- .../src/System/Linq/Select.SpeedOpt.cs | 6 +++--- .../ObjectModel/ReadOnlyDictionary.cs | 2 +- .../CompilerServices/ConditionalWeakTable.cs | 6 +++--- .../src/System/WeakReference.T.cs | 2 +- .../src/System/Xml/Xsl/XPath/XPathParser.cs | 2 +- .../Collection/DictionaryDefaultConverter.cs | 6 +++--- .../Collection/IEnumerableDefaultConverter.cs | 6 +++--- ...hParameterizedConstructorConverter.Small.cs | 4 ++-- .../Serialization/JsonConverterOfT.ReadCore.cs | 2 +- .../Json/Serialization/JsonConverterOfT.cs | 4 ++-- .../Json/Serialization/JsonPropertyInfoOfT.cs | 8 ++++---- .../Serialization/JsonResumableConverterOfT.cs | 2 +- .../JsonSerializer.Read.Stream.cs | 2 +- .../JsonSerializer.Read.String.cs | 2 +- .../JsonSerializer.Read.Utf8JsonReader.cs | 2 +- .../Threading/Channels/AsyncOperation.cs | 2 +- .../System/Threading/Channels/ChannelReader.cs | 4 ++-- .../Channels/ChannelReader.netstandard21.cs | 2 +- .../Channels/SingleConsumerUnboundedChannel.cs | 2 +- .../Threading/Channels/UnboundedChannel.cs | 2 +- .../src/System/Threading/Interlocked.Mono.cs | 8 ++------ .../src/System/WeakReference.T.Mono.cs | 2 +- 51 files changed, 87 insertions(+), 91 deletions(-) diff --git a/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/BlockingCollection.cs b/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/BlockingCollection.cs index abd485f2a10654..40dc97191207ef 100644 --- a/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/BlockingCollection.cs +++ b/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/BlockingCollection.cs @@ -536,7 +536,7 @@ private bool TryAddWithNoTimeValidation(T item, int millisecondsTimeout, Cancell /// A call to may block until an item is available to be removed. public T Take() { - T item; + T? item; if (!TryTake(out item, Timeout.Infinite, CancellationToken.None)) { @@ -560,7 +560,7 @@ public T Take() /// A call to may block until an item is available to be removed. public T Take(CancellationToken cancellationToken) { - T item; + T? item; if (!TryTake(out item, Timeout.Infinite, cancellationToken)) { @@ -1658,7 +1658,7 @@ public IEnumerable GetConsumingEnumerable(CancellationToken cancellationToken linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _consumersCancellationTokenSource.Token); while (!IsCompleted) { - T item; + T? item; if (TryTakeWithNoTimeValidation(out item, Timeout.Infinite, cancellationToken, linkedTokenSource)) { yield return item; diff --git a/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentBag.cs b/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentBag.cs index 2ac047da258419..0328b9f0e526fc 100644 --- a/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentBag.cs +++ b/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentBag.cs @@ -448,7 +448,7 @@ public void Clear() FreezeBag(ref lockTaken); for (WorkStealingQueue? queue = _workStealingQueues; queue != null; queue = queue._nextQueue) { - T ignored; + T? ignored; while (queue.TrySteal(out ignored, take: true)); } } 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 affa3bfaa30b57..aa79092e64064a 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 @@ -1022,7 +1022,7 @@ public TValue this[TKey key] { get { - if (!TryGetValue(key, out TValue value)) + if (!TryGetValue(key, out TValue? value)) { ThrowKeyNotFoundException(key); } @@ -1132,7 +1132,7 @@ public TValue GetOrAdd(TKey key, Func valueFactory) IEqualityComparer? comparer = _comparer; int hashcode = comparer is null ? key.GetHashCode() : comparer.GetHashCode(key); - if (!TryGetValueInternal(key, hashcode, out TValue resultingValue)) + if (!TryGetValueInternal(key, hashcode, out TValue? resultingValue)) { TryAddInternal(key, hashcode, valueFactory(key), updateIfExists: false, acquireLock: true, out resultingValue); } @@ -1171,7 +1171,7 @@ public TValue GetOrAdd(TKey key, Func valueFactory, TA IEqualityComparer? comparer = _comparer; int hashcode = comparer is null ? key.GetHashCode() : comparer.GetHashCode(key); - if (!TryGetValueInternal(key, hashcode, out TValue resultingValue)) + if (!TryGetValueInternal(key, hashcode, out TValue? resultingValue)) { TryAddInternal(key, hashcode, valueFactory(key, factoryArgument), updateIfExists: false, acquireLock: true, out resultingValue); } @@ -1201,7 +1201,7 @@ public TValue GetOrAdd(TKey key, TValue value) IEqualityComparer? comparer = _comparer; int hashcode = comparer is null ? key.GetHashCode() : comparer.GetHashCode(key); - if (!TryGetValueInternal(key, hashcode, out TValue resultingValue)) + if (!TryGetValueInternal(key, hashcode, out TValue? resultingValue)) { TryAddInternal(key, hashcode, value, updateIfExists: false, acquireLock: true, out resultingValue); } @@ -1252,7 +1252,7 @@ public TValue AddOrUpdate( while (true) { - if (TryGetValueInternal(key, hashcode, out TValue oldValue)) + if (TryGetValueInternal(key, hashcode, out TValue? oldValue)) { // key exists, try to update TValue newValue = updateValueFactory(key, oldValue, factoryArgument); @@ -1313,7 +1313,7 @@ public TValue AddOrUpdate(TKey key, Func addValueFactory, Func while (true) { - if (TryGetValueInternal(key, hashcode, out TValue oldValue)) + if (TryGetValueInternal(key, hashcode, out TValue? oldValue)) { // key exists, try to update TValue newValue = updateValueFactory(key, oldValue); @@ -1538,7 +1538,7 @@ void IDictionary.Add(TKey key, TValue value) /// cref="ICollection{T}"/>; otherwise, false. bool ICollection>.Contains(KeyValuePair keyValuePair) { - if (!TryGetValue(keyValuePair.Key, out TValue value)) + if (!TryGetValue(keyValuePair.Key, out TValue? value)) { return false; } @@ -1730,7 +1730,7 @@ void IDictionary.Remove(object key) ThrowHelper.ThrowKeyNullException(); } - if (key is TKey tkey && TryGetValue(tkey, out TValue value)) + if (key is TKey tkey && TryGetValue(tkey, out TValue? value)) { return value; } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.cs index e734dbba8be0dc..31d2b34974ec42 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.cs @@ -196,7 +196,7 @@ public TValue this[TKey key] { Requires.NotNullAllowStructs(key, nameof(key)); - TValue value; + TValue? value; if (this.TryGetValue(key, out value)) { return value; diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/CollectionExtensions.cs b/src/libraries/System.Collections/src/System/Collections/Generic/CollectionExtensions.cs index 39158f2e18cde2..7a5bce9b0a1d4e 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/CollectionExtensions.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/CollectionExtensions.cs @@ -19,7 +19,7 @@ public static TValue GetValueOrDefault(this IReadOnlyDictionary collection) private void RemoveAllElements(IEnumerable collection) { - T min = Min; - T max = Max; + T? min = Min; + T? max = Max; foreach (T item in collection) { if (!(comparer.Compare(item, min) < 0 || comparer.Compare(item, max) > 0) && Contains(item)) @@ -1031,7 +1031,7 @@ public virtual void IntersectWith(IEnumerable other) Enumerator mine = this.GetEnumerator(); Enumerator theirs = asSorted.GetEnumerator(); bool mineEnded = !mine.MoveNext(), theirsEnded = !theirs.MoveNext(); - T max = Max; + T? max = Max; while (!mineEnded && !theirsEnded && Comparer.Compare(theirs.Current, max) <= 0) { @@ -1109,8 +1109,8 @@ public void ExceptWith(IEnumerable other) // Outside range, no point in doing anything if (comparer.Compare(asSorted.Max, Min) >= 0 && comparer.Compare(asSorted.Min, Max) <= 0) { - T min = Min; - T max = Max; + T? min = Min; + T? max = Max; foreach (T item in other) { if (comparer.Compare(item, min) < 0) diff --git a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Hosting/AtomicCompositionExtensions.cs b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Hosting/AtomicCompositionExtensions.cs index c3be4d17df16cf..e0d99e469252ac 100644 --- a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Hosting/AtomicCompositionExtensions.cs +++ b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Hosting/AtomicCompositionExtensions.cs @@ -18,8 +18,8 @@ internal static T GetValueAllowNull(this AtomicComposition? atomicComposition internal static T GetValueAllowNull(this AtomicComposition? atomicComposition, object key, T defaultResult) { - T result; - if (atomicComposition != null && atomicComposition.TryGetValue(key, out result)) + T? result; + if (atomicComposition != null && atomicComposition.TryGetValue(key, out result)) { return result; } diff --git a/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/Utilities.cs b/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/Utilities.cs index 7b757e955d2322..6f429ea1af398e 100644 --- a/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/Utilities.cs +++ b/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/Utilities.cs @@ -271,7 +271,7 @@ public TValue this[TKey key] { Debug.Assert(key != null); - TValue res; + TValue? res; if (TryGetValue(key, out res)) { return res; diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Partitioning/OrderedHashRepartitionEnumerator.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Partitioning/OrderedHashRepartitionEnumerator.cs index 2228f5ec82bbb7..595a7b15fdb982 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Partitioning/OrderedHashRepartitionEnumerator.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Partitioning/OrderedHashRepartitionEnumerator.cs @@ -104,7 +104,7 @@ internal OrderedHashRepartitionEnumerator( // anyway, so having the repartitioning operator do so isn't complicating matters much at all. // - internal override bool MoveNext(ref Pair currentElement, ref TOrderKey currentKey) + internal override bool MoveNext(ref Pair currentElement, [AllowNull] ref TOrderKey currentKey) { if (_partitionCount == 1) { diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Binary/ExceptQueryOperator.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Binary/ExceptQueryOperator.cs index eae881b425dc59..3df3f4ebb85008 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Binary/ExceptQueryOperator.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Binary/ExceptQueryOperator.cs @@ -257,7 +257,7 @@ internal OrderedExceptQueryOperatorEnumerator( // Walks the two data sources, left and then right, to produce the distinct set // - internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TInputOutput currentElement, ref TLeftKey currentKey) + internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TInputOutput currentElement, [AllowNull] ref TLeftKey currentKey) { Debug.Assert(_leftSource != null); Debug.Assert(_rightSource != null); diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Binary/HashJoinQueryOperatorEnumerator.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Binary/HashJoinQueryOperatorEnumerator.cs index 81cb908691e5a8..8055046879846c 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Binary/HashJoinQueryOperatorEnumerator.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Binary/HashJoinQueryOperatorEnumerator.cs @@ -90,7 +90,7 @@ internal HashJoinQueryOperatorEnumerator( // as we do for inner joins. // - internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TOutput currentElement, ref TOutputKey currentKey) + internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TOutput currentElement, [AllowNull] ref TOutputKey currentKey) { Debug.Assert(_resultSelector != null, "expected a compiled result selector"); Debug.Assert(_leftSource != null); diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Binary/IntersectQueryOperator.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Binary/IntersectQueryOperator.cs index 8a727b879dad99..3b6230f4419920 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Binary/IntersectQueryOperator.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Binary/IntersectQueryOperator.cs @@ -258,7 +258,7 @@ internal OrderedIntersectQueryOperatorEnumerator( // Walks the two data sources, left and then right, to produce the intersection. // - internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TInputOutput currentElement, ref TLeftKey currentKey) + internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TInputOutput currentElement, [AllowNull] ref TLeftKey currentKey) { Debug.Assert(_leftSource != null); Debug.Assert(_rightSource != null); diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Inlined/LongCountAggregationOperator.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Inlined/LongCountAggregationOperator.cs index 1d4f10b0ecb72d..e9ad61c8a8db88 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Inlined/LongCountAggregationOperator.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Inlined/LongCountAggregationOperator.cs @@ -95,8 +95,8 @@ internal LongCountAggregationOperatorEnumerator(QueryOperatorEnumerator source = _source; if (source.MoveNext(ref elementUnused, ref keyUnused)) diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/QueryOperatorEnumerator.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/QueryOperatorEnumerator.cs index bcbd1b78c26078..9f99e5e4c8d8be 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/QueryOperatorEnumerator.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/QueryOperatorEnumerator.cs @@ -23,7 +23,7 @@ internal abstract class QueryOperatorEnumerator { // Moves the position of the enumerator forward by one, and simultaneously returns // the (new) current element and key. If empty, false is returned. - internal abstract bool MoveNext([MaybeNullWhen(false), AllowNull] ref TElement currentElement, ref TKey currentKey); + internal abstract bool MoveNext([MaybeNullWhen(false), AllowNull] ref TElement currentElement, [AllowNull] ref TKey currentKey); // Standard implementation of the disposable pattern. public void Dispose() diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/DefaultIfEmptyQueryOperator.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/DefaultIfEmptyQueryOperator.cs index 75cde0a5634e13..a60892dc1edaf0 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/DefaultIfEmptyQueryOperator.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/DefaultIfEmptyQueryOperator.cs @@ -139,7 +139,7 @@ internal DefaultIfEmptyQueryOperatorEnumerator( // Straightforward IEnumerator methods. // - internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TSource currentElement, ref TKey currentKey) + internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TSource currentElement, [AllowNull] ref TKey currentKey) { Debug.Assert(_source != null); diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/DistinctQueryOperator.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/DistinctQueryOperator.cs index 3e2c37b52954dc..4608cecd5c2e56 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/DistinctQueryOperator.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/DistinctQueryOperator.cs @@ -220,7 +220,7 @@ internal OrderedDistinctQueryOperatorEnumerator( // Walks the single data source, skipping elements it has already seen. // - internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TInputOutput currentElement, ref TKey currentKey) + internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TInputOutput currentElement, [AllowNull] ref TKey currentKey) { Debug.Assert(_source != null); Debug.Assert(_hashLookup != null); 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 a45f43cf0618fe..4afdfc4402ecbe 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 @@ -155,8 +155,8 @@ internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TInput cur // Cancellation testing must be performed here as full enumeration occurs within this method. // We only need to throw a simple exception here.. marshalling logic handled via QueryTaskGroupState.QueryEnd (called by ForAllSpoolingTask) - TInput element = default(TInput)!; - TKey keyUnused = default(TKey)!; + TInput? element = default(TInput); + TKey? keyUnused = default(TKey); int i = 0; while (_source.MoveNext(ref element, ref keyUnused)) { diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/GroupByQueryOperator.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/GroupByQueryOperator.cs index bb073369c8b296..92f2a357da1ebb 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/GroupByQueryOperator.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/GroupByQueryOperator.cs @@ -253,7 +253,7 @@ protected GroupByQueryOperatorEnumerator( // just enumerate the key-set from the hash-table, retrieving groupings of key-elements. // - internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref IGrouping currentElement, ref TOrderKey currentKey) + internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref IGrouping currentElement, [AllowNull] ref TOrderKey currentKey) { Debug.Assert(_source != null); @@ -459,7 +459,7 @@ protected OrderedGroupByQueryOperatorEnumerator(QueryOperatorEnumerator currentElement, ref TOrderKey currentKey) + internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref IGrouping currentElement, [AllowNull] ref TOrderKey currentKey) { Debug.Assert(_source != null); Debug.Assert(_keySelector != null); diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/ReverseQueryOperator.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/ReverseQueryOperator.cs index 452ce0549453f7..e78c8a9a446ce1 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/ReverseQueryOperator.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/ReverseQueryOperator.cs @@ -124,7 +124,7 @@ internal ReverseQueryOperatorEnumerator(QueryOperatorEnumerator s // Straightforward IEnumerator methods. // - internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TSource currentElement, ref TKey currentKey) + internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TSource currentElement, [AllowNull] ref TKey currentKey) { // If the buffer has not been created, we will generate it lazily on demand. if (_buffer == null) diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/SelectQueryOperator.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/SelectQueryOperator.cs index f3fe3af8016c89..d338c6bb8ae2a0 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/SelectQueryOperator.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/SelectQueryOperator.cs @@ -111,7 +111,7 @@ internal SelectQueryOperatorEnumerator(QueryOperatorEnumerator sou // Straightforward IEnumerator methods. // - internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TOutput currentElement, ref TKey currentKey) + internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TOutput currentElement, [AllowNull] ref TKey currentKey) { // So long as the source has a next element, we have an element. TInput element = default(TInput)!; diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/SortQueryOperator.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/SortQueryOperator.cs index 988f279a3a5af6..779804b96b7aa3 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/SortQueryOperator.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/SortQueryOperator.cs @@ -198,7 +198,7 @@ internal SortQueryOperatorEnumerator(QueryOperatorEnumerator // in memory, and the data sorted. // - internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TInputOutput currentElement, ref TSortKey currentKey) + internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TInputOutput currentElement, [AllowNull] ref TSortKey currentKey) { Debug.Assert(_source != null); diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/TakeOrSkipQueryOperator.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/TakeOrSkipQueryOperator.cs index fe75414a63ce60..2a6eead9bada8d 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/TakeOrSkipQueryOperator.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/TakeOrSkipQueryOperator.cs @@ -187,7 +187,7 @@ internal TakeOrSkipQueryOperatorEnumerator( // Straightforward IEnumerator methods. // - internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TResult currentElement, ref TKey currentKey) + internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TResult currentElement, [AllowNull] ref TKey currentKey) { Debug.Assert(_sharedIndices != null); diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/TakeOrSkipWhileQueryOperator.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/TakeOrSkipWhileQueryOperator.cs index cc896d51ec29b8..2fcad2f945b009 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/TakeOrSkipWhileQueryOperator.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/TakeOrSkipWhileQueryOperator.cs @@ -247,7 +247,7 @@ internal TakeOrSkipWhileQueryOperatorEnumerator( // Straightforward IEnumerator methods. // - internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TResult currentElement, ref TKey currentKey) + internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TResult currentElement, [AllowNull] ref TKey currentKey) { // If the buffer has not been created, we will generate it lazily on demand. if (_buffer == null) diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/WhereQueryOperator.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/WhereQueryOperator.cs index e9e4fde76ef8ff..e5dcfdb68f7236 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/WhereQueryOperator.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/WhereQueryOperator.cs @@ -125,7 +125,7 @@ internal WhereQueryOperatorEnumerator(QueryOperatorEnumerator? comparer) // If value is not in set, add it and return true; otherwise return false internal bool Add(TKey key, TValue value) { - return !Find(key, true, false, ref value); + return !Find(key, true, false, ref value!); } // Check whether value is in set @@ -55,7 +55,7 @@ internal TValue this[TKey key] { set { - TValue v = value; + TValue? v = value; Find(key, false, true, ref v); } } diff --git a/src/libraries/System.Linq/src/System/Linq/ElementAt.cs b/src/libraries/System.Linq/src/System/Linq/ElementAt.cs index da8dc8f9287646..fa1256a6f22abb 100644 --- a/src/libraries/System.Linq/src/System/Linq/ElementAt.cs +++ b/src/libraries/System.Linq/src/System/Linq/ElementAt.cs @@ -17,7 +17,7 @@ public static TSource ElementAt(this IEnumerable source, int i if (source is IPartition partition) { - TSource element = partition.TryGetElementAt(index, out bool found); + TSource? element = partition.TryGetElementAt(index, out bool found); if (found) { return element!; diff --git a/src/libraries/System.Linq/src/System/Linq/First.cs b/src/libraries/System.Linq/src/System/Linq/First.cs index 295acb5e4d4150..64b89915c259f8 100644 --- a/src/libraries/System.Linq/src/System/Linq/First.cs +++ b/src/libraries/System.Linq/src/System/Linq/First.cs @@ -10,7 +10,7 @@ public static partial class Enumerable { public static TSource First(this IEnumerable source) { - TSource first = source.TryGetFirst(out bool found); + TSource? first = source.TryGetFirst(out bool found); if (!found) { ThrowHelper.ThrowNoElementsException(); @@ -21,7 +21,7 @@ public static TSource First(this IEnumerable source) public static TSource First(this IEnumerable source, Func predicate) { - TSource first = source.TryGetFirst(predicate, out bool found); + TSource? first = source.TryGetFirst(predicate, out bool found); if (!found) { ThrowHelper.ThrowNoMatchException(); diff --git a/src/libraries/System.Linq/src/System/Linq/Last.cs b/src/libraries/System.Linq/src/System/Linq/Last.cs index d7819614ab0753..f7cd74eb694bd4 100644 --- a/src/libraries/System.Linq/src/System/Linq/Last.cs +++ b/src/libraries/System.Linq/src/System/Linq/Last.cs @@ -10,7 +10,7 @@ public static partial class Enumerable { public static TSource Last(this IEnumerable source) { - TSource last = source.TryGetLast(out bool found); + TSource? last = source.TryGetLast(out bool found); if (!found) { ThrowHelper.ThrowNoElementsException(); @@ -21,7 +21,7 @@ public static TSource Last(this IEnumerable source) public static TSource Last(this IEnumerable source, Func predicate) { - TSource last = source.TryGetLast(predicate, out bool found); + TSource? last = source.TryGetLast(predicate, out bool found); if (!found) { ThrowHelper.ThrowNoMatchException(); diff --git a/src/libraries/System.Linq/src/System/Linq/Select.SpeedOpt.cs b/src/libraries/System.Linq/src/System/Linq/Select.SpeedOpt.cs index f93b7c082f38de..a941cc5b47586b 100644 --- a/src/libraries/System.Linq/src/System/Linq/Select.SpeedOpt.cs +++ b/src/libraries/System.Linq/src/System/Linq/Select.SpeedOpt.cs @@ -560,7 +560,7 @@ public IPartition Take(int count) public TResult? TryGetElementAt(int index, out bool found) { bool sourceFound; - TSource input = _source.TryGetElementAt(index, out sourceFound); + TSource? input = _source.TryGetElementAt(index, out sourceFound); found = sourceFound; return sourceFound ? _selector(input!) : default!; } @@ -568,7 +568,7 @@ public IPartition Take(int count) public TResult? TryGetFirst(out bool found) { bool sourceFound; - TSource input = _source.TryGetFirst(out sourceFound); + TSource? input = _source.TryGetFirst(out sourceFound); found = sourceFound; return sourceFound ? _selector(input!) : default!; } @@ -576,7 +576,7 @@ public IPartition Take(int count) public TResult? TryGetLast(out bool found) { bool sourceFound; - TSource input = _source.TryGetLast(out sourceFound); + TSource? input = _source.TryGetLast(out sourceFound); found = sourceFound; return sourceFound ? _selector(input!) : default!; } diff --git a/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/ReadOnlyDictionary.cs b/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/ReadOnlyDictionary.cs index db94eee6c30f20..28b4c06533fa19 100644 --- a/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/ReadOnlyDictionary.cs +++ b/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/ReadOnlyDictionary.cs @@ -161,7 +161,7 @@ void IDictionary.Remove(object key) return null; } - if (m_dictionary.TryGetValue((TKey)key, out TValue value)) + if (m_dictionary.TryGetValue((TKey)key, out TValue? value)) { return value; } diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/ConditionalWeakTable.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/ConditionalWeakTable.cs index d5caca5098f087..b3c72604977b68 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/ConditionalWeakTable.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/ConditionalWeakTable.cs @@ -187,7 +187,7 @@ public TValue GetValue(TKey key, CreateValueCallback createValueCallback) throw new ArgumentNullException(nameof(createValueCallback)); } - return TryGetValue(key, out TValue existingValue) ? + return TryGetValue(key, out TValue? existingValue) ? existingValue : GetValueLocked(key, createValueCallback); } @@ -201,7 +201,7 @@ private TValue GetValueLocked(TKey key, CreateValueCallback createValueCallback) lock (_lock) { // Now that we've taken the lock, must recheck in case we lost a race to add the key. - if (_container.TryGetValueWorker(key, out TValue existingValue)) + if (_container.TryGetValueWorker(key, out TValue? existingValue)) { return existingValue; } @@ -336,7 +336,7 @@ public bool MoveNext() while (_currentIndex < _maxIndexInclusive) { _currentIndex++; - if (c.TryGetEntry(_currentIndex, out TKey? key, out TValue value)) + if (c.TryGetEntry(_currentIndex, out TKey? key, out TValue? value)) { _current = new KeyValuePair(key, value); return true; diff --git a/src/libraries/System.Private.CoreLib/src/System/WeakReference.T.cs b/src/libraries/System.Private.CoreLib/src/System/WeakReference.T.cs index 53b204e37e3708..a2e5258f62b22c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/WeakReference.T.cs +++ b/src/libraries/System.Private.CoreLib/src/System/WeakReference.T.cs @@ -54,7 +54,7 @@ private WeakReference(SerializationInfo info, StreamingContext context) public bool TryGetTarget([MaybeNullWhen(false), NotNullWhen(true)] out T target) { // Call the worker method that has more performant but less user friendly signature. - T o = this.Target; + T? o = this.Target; target = o!; return o != null; } diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Xsl/XPath/XPathParser.cs b/src/libraries/System.Private.Xml/src/System/Xml/Xsl/XPath/XPathParser.cs index 5208144246306e..2c1a51cf9469e1 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Xsl/XPath/XPathParser.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Xsl/XPath/XPathParser.cs @@ -27,7 +27,7 @@ public Node Parse(XPathScanner scanner, IXPathBuilder builder, LexKind end Debug.Assert(_scanner == null && _builder == null); Debug.Assert(scanner != null && builder != null); - Node result = default(Node); + Node? result = default(Node); _scanner = scanner; _builder = builder; _posInfo.Clear(); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/DictionaryDefaultConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/DictionaryDefaultConverter.cs index ce41388ea59f49..7ada24a9941160 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/DictionaryDefaultConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/DictionaryDefaultConverter.cs @@ -88,7 +88,7 @@ internal sealed override bool OnTryRead( // Read the value and add. reader.ReadWithVerify(); - TValue element = valueConverter.Read(ref reader, ElementType, options); + TValue? element = valueConverter.Read(ref reader, ElementType, options); Add(key, element!, options, ref state); } } @@ -113,7 +113,7 @@ internal sealed override bool OnTryRead( reader.ReadWithVerify(); // Get the value from the converter and add it. - valueConverter.TryRead(ref reader, ElementType, options, ref state, out TValue element); + valueConverter.TryRead(ref reader, ElementType, options, ref state, out TValue? element); Add(key, element!, options, ref state); } } @@ -214,7 +214,7 @@ internal sealed override bool OnTryRead( if (state.Current.PropertyState < StackFramePropertyState.TryRead) { // Get the value from the converter and add it. - bool success = elementConverter.TryRead(ref reader, typeof(TValue), options, ref state, out TValue element); + bool success = elementConverter.TryRead(ref reader, typeof(TValue), options, ref state, out TValue? element); if (!success) { value = default; diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/IEnumerableDefaultConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/IEnumerableDefaultConverter.cs index 1848f5b70f0500..e7038ac9ec28b0 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/IEnumerableDefaultConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/IEnumerableDefaultConverter.cs @@ -65,7 +65,7 @@ internal override bool OnTryRead( } // Obtain the CLR value from the JSON and apply to the object. - TElement element = elementConverter.Read(ref reader, elementConverter.TypeToConvert, options); + TElement? element = elementConverter.Read(ref reader, elementConverter.TypeToConvert, options); Add(element!, ref state); } } @@ -81,7 +81,7 @@ internal override bool OnTryRead( } // Get the value from the converter and add it. - elementConverter.TryRead(ref reader, typeof(TElement), options, ref state, out TElement element); + elementConverter.TryRead(ref reader, typeof(TElement), options, ref state, out TElement? element); Add(element!, ref state); } } @@ -169,7 +169,7 @@ internal override bool OnTryRead( if (state.Current.PropertyState < StackFramePropertyState.TryRead) { // Get the value from the converter and add it. - if (!elementConverter.TryRead(ref reader, typeof(TElement), options, ref state, out TElement element)) + if (!elementConverter.TryRead(ref reader, typeof(TElement), options, ref state, out TElement? element)) { value = default; return false; diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectWithParameterizedConstructorConverter.Small.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectWithParameterizedConstructorConverter.Small.cs index be414b6667fd51..c3299c6878424a 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectWithParameterizedConstructorConverter.Small.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectWithParameterizedConstructorConverter.Small.cs @@ -63,10 +63,10 @@ private bool TryRead( var info = (JsonParameterInfo)jsonParameterInfo; var converter = (JsonConverter)jsonParameterInfo.ConverterBase; - bool success = converter.TryRead(ref reader, info.RuntimePropertyType, info.Options!, ref state, out TArg value); + bool success = converter.TryRead(ref reader, info.RuntimePropertyType, info.Options!, ref state, out TArg? value); arg = value == null && jsonParameterInfo.IgnoreDefaultValuesOnRead - ? (TArg)info.DefaultValue! // Use default value specified on parameter, if any. + ? (TArg?)info.DefaultValue! // Use default value specified on parameter, if any. : value!; return success; diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.ReadCore.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.ReadCore.cs index 57dfef601c8859..884a61d977085d 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.ReadCore.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.ReadCore.cs @@ -59,7 +59,7 @@ public partial class JsonConverter } JsonPropertyInfo jsonPropertyInfo = state.Current.JsonClassInfo.PropertyInfoForClassInfo; - bool success = TryRead(ref reader, jsonPropertyInfo.RuntimePropertyType!, options, ref state, out T value); + bool success = TryRead(ref reader, jsonPropertyInfo.RuntimePropertyType!, options, ref state, out T? value); if (success) { // Read any trailing whitespace. This will throw if JsonCommentHandling=Disallow. diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.cs index 348c558994aa63..49b4400eef3b6b 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.cs @@ -204,7 +204,7 @@ internal bool TryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSeriali if (JsonSerializer.TryGetReferenceFromJsonElement(ref state, element, out object? referenceValue)) { - value = (T)referenceValue; + value = (T?)referenceValue; } } @@ -289,7 +289,7 @@ internal bool TryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSeriali internal override sealed bool TryReadAsObject(ref Utf8JsonReader reader, JsonSerializerOptions options, ref ReadStack state, out object? value) { - bool success = TryRead(ref reader, TypeToConvert, options, ref state, out T typedValue); + bool success = TryRead(ref reader, TypeToConvert, options, ref state, out T? typedValue); value = typedValue; return success; } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfoOfT.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfoOfT.cs index 992f28b4bcfea0..797a96c43ceb05 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfoOfT.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfoOfT.cs @@ -243,7 +243,7 @@ public override bool ReadJsonAndSetMember(object obj, ref ReadStack state, ref U if (!IgnoreDefaultValuesOnRead) { - T value = default; + T? value = default; Set!(obj, value!); } @@ -257,7 +257,7 @@ public override bool ReadJsonAndSetMember(object obj, ref ReadStack state, ref U if (!isNullToken || !IgnoreDefaultValuesOnRead || !PropertyTypeCanBeNull) { // Optimize for internal converters by avoiding the extra call to TryRead. - T fastValue = Converter.Read(ref reader, RuntimePropertyType!, Options); + T? fastValue = Converter.Read(ref reader, RuntimePropertyType!, Options); Set!(obj, fastValue!); } @@ -268,7 +268,7 @@ public override bool ReadJsonAndSetMember(object obj, ref ReadStack state, ref U success = true; if (!isNullToken || !IgnoreDefaultValuesOnRead || !PropertyTypeCanBeNull || state.IsContinuation) { - success = Converter.TryRead(ref reader, RuntimePropertyType!, Options, ref state, out T value); + success = Converter.TryRead(ref reader, RuntimePropertyType!, Options, ref state, out T? value); if (success) { #if !DEBUG @@ -324,7 +324,7 @@ public override bool ReadJsonAsObject(ref ReadStack state, ref Utf8JsonReader re } else { - success = Converter.TryRead(ref reader, RuntimePropertyType!, Options, ref state, out T typedValue); + success = Converter.TryRead(ref reader, RuntimePropertyType!, Options, ref state, out T? typedValue); value = typedValue; } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonResumableConverterOfT.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonResumableConverterOfT.cs index fbe746d6b12826..05efb460b64f1b 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonResumableConverterOfT.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonResumableConverterOfT.cs @@ -22,7 +22,7 @@ internal abstract class JsonResumableConverter : JsonConverter ReadStack state = default; state.Initialize(typeToConvert, options, supportContinuation: false); - TryRead(ref reader, typeToConvert, options, ref state, out T value); + TryRead(ref reader, typeToConvert, options, ref state, out T? value); return value; } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Stream.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Stream.cs index b0c471cfcbd004..5c695a62c43f27 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Stream.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Stream.cs @@ -233,7 +233,7 @@ private static TValue ReadCore( state.ReadAhead = !isFinalBlock; state.BytesConsumed = 0; - TValue value = ReadCore(converterBase, ref reader, options, ref state); + TValue? value = ReadCore(converterBase, ref reader, options, ref state); readerState = reader.CurrentState; return value!; diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.String.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.String.cs index 2fe4f812ba3477..d5f08379664486 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.String.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.String.cs @@ -191,7 +191,7 @@ public static partial class JsonSerializer var readerState = new JsonReaderState(options.GetReaderOptions()); var reader = new Utf8JsonReader(utf8, isFinalBlock: true, readerState); - TValue value = ReadCore(ref reader, returnType, options); + TValue? value = ReadCore(ref reader, returnType, options); // The reader should have thrown if we have remaining bytes. Debug.Assert(reader.BytesConsumed == actualByteCount); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Utf8JsonReader.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Utf8JsonReader.cs index 4b424581307ecd..ad2b0427ef3080 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Utf8JsonReader.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Utf8JsonReader.cs @@ -316,7 +316,7 @@ private static void CheckSupportedOptions(JsonReaderOptions readerOptions, strin var newReader = new Utf8JsonReader(rentedSpan, originalReaderOptions); JsonConverter jsonConverter = state.Current.JsonPropertyInfo!.ConverterBase; - TValue value = ReadCore(jsonConverter, ref newReader, options, ref state); + TValue? value = ReadCore(jsonConverter, ref newReader, options, ref state); // The reader should have thrown if we have remaining bytes. Debug.Assert(newReader.BytesConsumed == length); diff --git a/src/libraries/System.Threading.Channels/src/System/Threading/Channels/AsyncOperation.cs b/src/libraries/System.Threading.Channels/src/System/Threading/Channels/AsyncOperation.cs index 9412cc28abfaf8..5a3f42616352b4 100644 --- a/src/libraries/System.Threading.Channels/src/System/Threading/Channels/AsyncOperation.cs +++ b/src/libraries/System.Threading.Channels/src/System/Threading/Channels/AsyncOperation.cs @@ -139,7 +139,7 @@ public TResult GetResult(short token) } ExceptionDispatchInfo? error = _error; - TResult result = _result; + TResult? result = _result; _currentId++; if (_pooled) diff --git a/src/libraries/System.Threading.Channels/src/System/Threading/Channels/ChannelReader.cs b/src/libraries/System.Threading.Channels/src/System/Threading/Channels/ChannelReader.cs index cc48adf7422a19..1c3f12e0993fb6 100644 --- a/src/libraries/System.Threading.Channels/src/System/Threading/Channels/ChannelReader.cs +++ b/src/libraries/System.Threading.Channels/src/System/Threading/Channels/ChannelReader.cs @@ -50,7 +50,7 @@ public virtual ValueTask ReadAsync(CancellationToken cancellationToken = defa try { - if (TryRead(out T fastItem)) + if (TryRead(out T? fastItem)) { return new ValueTask(fastItem); } @@ -71,7 +71,7 @@ async ValueTask ReadAsyncCore(CancellationToken ct) throw new ChannelClosedException(); } - if (TryRead(out T item)) + if (TryRead(out T? item)) { return item; } diff --git a/src/libraries/System.Threading.Channels/src/System/Threading/Channels/ChannelReader.netstandard21.cs b/src/libraries/System.Threading.Channels/src/System/Threading/Channels/ChannelReader.netstandard21.cs index 20ab8afd010cce..bbfda5cd03dee9 100644 --- a/src/libraries/System.Threading.Channels/src/System/Threading/Channels/ChannelReader.netstandard21.cs +++ b/src/libraries/System.Threading.Channels/src/System/Threading/Channels/ChannelReader.netstandard21.cs @@ -19,7 +19,7 @@ public virtual async IAsyncEnumerable ReadAllAsync([EnumeratorCancellation] C { while (await WaitToReadAsync(cancellationToken).ConfigureAwait(false)) { - while (TryRead(out T item)) + while (TryRead(out T? item)) { yield return item; } diff --git a/src/libraries/System.Threading.Channels/src/System/Threading/Channels/SingleConsumerUnboundedChannel.cs b/src/libraries/System.Threading.Channels/src/System/Threading/Channels/SingleConsumerUnboundedChannel.cs index 5d16bc986f2658..ec795e64b84b2c 100644 --- a/src/libraries/System.Threading.Channels/src/System/Threading/Channels/SingleConsumerUnboundedChannel.cs +++ b/src/libraries/System.Threading.Channels/src/System/Threading/Channels/SingleConsumerUnboundedChannel.cs @@ -72,7 +72,7 @@ public override ValueTask ReadAsync(CancellationToken cancellationToken) return new ValueTask(Task.FromCanceled(cancellationToken)); } - if (TryRead(out T item)) + if (TryRead(out T? item)) { return new ValueTask(item); } diff --git a/src/libraries/System.Threading.Channels/src/System/Threading/Channels/UnboundedChannel.cs b/src/libraries/System.Threading.Channels/src/System/Threading/Channels/UnboundedChannel.cs index 91672aafe5137f..b534aabe5b8df8 100644 --- a/src/libraries/System.Threading.Channels/src/System/Threading/Channels/UnboundedChannel.cs +++ b/src/libraries/System.Threading.Channels/src/System/Threading/Channels/UnboundedChannel.cs @@ -67,7 +67,7 @@ public override ValueTask ReadAsync(CancellationToken cancellationToken) // Dequeue an item if we can. UnboundedChannel parent = _parent; - if (parent._items.TryDequeue(out T item)) + if (parent._items.TryDequeue(out T? item)) { CompleteIfDone(parent); return new ValueTask(item); diff --git a/src/mono/netcore/System.Private.CoreLib/src/System/Threading/Interlocked.Mono.cs b/src/mono/netcore/System.Private.CoreLib/src/System/Threading/Interlocked.Mono.cs index 1bed27d4f2e929..281854521f08cc 100644 --- a/src/mono/netcore/System.Private.CoreLib/src/System/Threading/Interlocked.Mono.cs +++ b/src/mono/netcore/System.Private.CoreLib/src/System/Threading/Interlocked.Mono.cs @@ -104,9 +104,7 @@ public static T CompareExchange(ref T location1, T value, T comparand) where // // This is not entirely convincing due to lack of volatile. // -#pragma warning disable 8654 // null problems; is there another way? - T result = null; -#pragma warning restore 8654 + T? result = null; // T : class so call the object overload. CompareExchange(ref Unsafe.As(ref location1), ref Unsafe.As(ref value), ref Unsafe.As(ref comparand), ref Unsafe.As(ref result!)); return result; @@ -137,9 +135,7 @@ public static T Exchange([NotNullIfNotNull("value")] ref T location1, T value // // This is not entirely convincing due to lack of volatile. // -#pragma warning disable 8654 // null problems; is there another way? - T result = null; -#pragma warning restore 8654 + T? result = null; // T : class so call the object overload. Exchange(ref Unsafe.As(ref location1), ref Unsafe.As(ref value), ref Unsafe.As(ref result!)); return result; diff --git a/src/mono/netcore/System.Private.CoreLib/src/System/WeakReference.T.Mono.cs b/src/mono/netcore/System.Private.CoreLib/src/System/WeakReference.T.Mono.cs index e4c4b0af7bead4..73011f68122489 100644 --- a/src/mono/netcore/System.Private.CoreLib/src/System/WeakReference.T.Mono.cs +++ b/src/mono/netcore/System.Private.CoreLib/src/System/WeakReference.T.Mono.cs @@ -16,7 +16,7 @@ private T? Target get { GCHandle h = handle; - return h.IsAllocated ? (T)h.Target : null; + return h.IsAllocated ? (T?)h.Target : null; } }