Skip to content

Commit

Permalink
Resolve warnings for assignment or explicit cast of possibly null typ…
Browse files Browse the repository at this point in the history
…e parameter value (#43925)
  • Loading branch information
cston authored Nov 10, 2020
1 parent e4ded7b commit c1a21a0
Show file tree
Hide file tree
Showing 51 changed files with 87 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ private bool TryAddWithNoTimeValidation(T item, int millisecondsTimeout, Cancell
/// <remarks>A call to <see cref="Take()"/> may block until an item is available to be removed.</remarks>
public T Take()
{
T item;
T? item;

if (!TryTake(out item, Timeout.Infinite, CancellationToken.None))
{
Expand All @@ -560,7 +560,7 @@ public T Take()
/// <remarks>A call to <see cref="Take(CancellationToken)"/> may block until an item is available to be removed.</remarks>
public T Take(CancellationToken cancellationToken)
{
T item;
T? item;

if (!TryTake(out item, Timeout.Infinite, cancellationToken))
{
Expand Down Expand Up @@ -1658,7 +1658,7 @@ public IEnumerable<T> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ public TValue this[TKey key]
{
get
{
if (!TryGetValue(key, out TValue value))
if (!TryGetValue(key, out TValue? value))
{
ThrowKeyNotFoundException(key);
}
Expand Down Expand Up @@ -1132,7 +1132,7 @@ public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
IEqualityComparer<TKey>? 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);
}
Expand Down Expand Up @@ -1171,7 +1171,7 @@ public TValue GetOrAdd<TArg>(TKey key, Func<TKey, TArg, TValue> valueFactory, TA
IEqualityComparer<TKey>? 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);
}
Expand Down Expand Up @@ -1201,7 +1201,7 @@ public TValue GetOrAdd(TKey key, TValue value)
IEqualityComparer<TKey>? 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);
}
Expand Down Expand Up @@ -1252,7 +1252,7 @@ public TValue AddOrUpdate<TArg>(

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);
Expand Down Expand Up @@ -1313,7 +1313,7 @@ public TValue AddOrUpdate(TKey key, Func<TKey, TValue> addValueFactory, Func<TKe

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);
Expand Down Expand Up @@ -1367,7 +1367,7 @@ public TValue AddOrUpdate(TKey key, TValue addValue, Func<TKey, TValue, TValue>

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);
Expand Down Expand Up @@ -1538,7 +1538,7 @@ void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
/// cref="ICollection{T}"/>; otherwise, false.</returns>
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> keyValuePair)
{
if (!TryGetValue(keyValuePair.Key, out TValue value))
if (!TryGetValue(keyValuePair.Key, out TValue? value))
{
return false;
}
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TK
throw new ArgumentNullException(nameof(dictionary));
}

TValue value;
TValue? value;
return dictionary.TryGetValue(key, out value) ? value : defaultValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ ICollection IDictionary.Values
{
if (IsCompatibleKey(key))
{
TValue value;
TValue? value;
if (TryGetValue((TKey)key, out value))
{
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ private void AddAllElements(IEnumerable<T> collection)

private void RemoveAllElements(IEnumerable<T> 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))
Expand Down Expand Up @@ -1031,7 +1031,7 @@ public virtual void IntersectWith(IEnumerable<T> 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)
{
Expand Down Expand Up @@ -1109,8 +1109,8 @@ public void ExceptWith(IEnumerable<T> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ internal static T GetValueAllowNull<T>(this AtomicComposition? atomicComposition

internal static T GetValueAllowNull<T>(this AtomicComposition? atomicComposition, object key, T defaultResult)
{
T result;
if (atomicComposition != null && atomicComposition.TryGetValue(key, out result))
T? result;
if (atomicComposition != null && atomicComposition.TryGetValue<T>(key, out result))
{
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public TValue this[TKey key]
{
Debug.Assert(key != null);

TValue res;
TValue? res;
if (TryGetValue(key, out res))
{
return res;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TInputOutput, THashKey> currentElement, ref TOrderKey currentKey)
internal override bool MoveNext(ref Pair<TInputOutput, THashKey> currentElement, [AllowNull] ref TOrderKey currentKey)
{
if (_partitionCount == 1)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ internal LongCountAggregationOperatorEnumerator(QueryOperatorEnumerator<TSource,

protected override bool MoveNextCore(ref long currentElement)
{
TSource elementUnused = default(TSource)!;
TKey keyUnused = default(TKey)!;
TSource? elementUnused = default(TSource);
TKey? keyUnused = default(TKey);

QueryOperatorEnumerator<TSource, TKey> source = _source;
if (source.MoveNext(ref elementUnused, ref keyUnused))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal abstract class QueryOperatorEnumerator<TElement, TKey>
{
// 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ internal DefaultIfEmptyQueryOperatorEnumerator(
// Straightforward IEnumerator<T> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TGroupKey, TElement> currentElement, ref TOrderKey currentKey)
internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref IGrouping<TGroupKey, TElement> currentElement, [AllowNull] ref TOrderKey currentKey)
{
Debug.Assert(_source != null);

Expand Down Expand Up @@ -459,7 +459,7 @@ protected OrderedGroupByQueryOperatorEnumerator(QueryOperatorEnumerator<Pair<TSo
// just enumerate the key-set from the hash-table, retrieving groupings of key-elements.
//

internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref IGrouping<TGroupKey, TElement> currentElement, ref TOrderKey currentKey)
internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref IGrouping<TGroupKey, TElement> currentElement, [AllowNull] ref TOrderKey currentKey)
{
Debug.Assert(_source != null);
Debug.Assert(_keySelector != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ internal ReverseQueryOperatorEnumerator(QueryOperatorEnumerator<TSource, TKey> s
// Straightforward IEnumerator<T> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ internal SelectQueryOperatorEnumerator(QueryOperatorEnumerator<TInput, TKey> sou
// Straightforward IEnumerator<T> 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)!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ internal SortQueryOperatorEnumerator(QueryOperatorEnumerator<TInputOutput, TKey>
// 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ internal TakeOrSkipQueryOperatorEnumerator(
// Straightforward IEnumerator<T> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ internal TakeOrSkipWhileQueryOperatorEnumerator(
// Straightforward IEnumerator<T> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ internal WhereQueryOperatorEnumerator(QueryOperatorEnumerator<TInputOutput, TKey
// Moves to the next matching element in the underlying data stream.
//

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(_predicate != null, "expected a compiled operator");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal HashLookup(IEqualityComparer<TKey>? 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
Expand All @@ -55,7 +55,7 @@ internal TValue this[TKey key]
{
set
{
TValue v = value;
TValue? v = value;
Find(key, false, true, ref v);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Linq/src/System/Linq/ElementAt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static TSource ElementAt<TSource>(this IEnumerable<TSource> source, int i

if (source is IPartition<TSource> partition)
{
TSource element = partition.TryGetElementAt(index, out bool found);
TSource? element = partition.TryGetElementAt(index, out bool found);
if (found)
{
return element!;
Expand Down
Loading

0 comments on commit c1a21a0

Please sign in to comment.