Skip to content

Fix a few more nullability warnings #1038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
Expand All @@ -15,7 +14,7 @@ public class BufferCountBenchmark
{
[Params(1, 10, 100, 1000, 10000, 100000, 1000000)]
public int N;
private IList<int> _store;
private IList<int>? _store;

[Benchmark]
public void Exact()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
Expand All @@ -18,8 +17,8 @@ public class IgnoreElementsBenchmark

private int _store;

private int[] _array;
private List<int> _list;
private int[]? _array;
private List<int>? _list;

[Benchmark]
public void Ignore()
Expand All @@ -32,15 +31,15 @@ public void Ignore()
[Benchmark]
public void IgnoreList()
{
_list
_list!
.IgnoreElements()
.Subscribe(v => Volatile.Write(ref _store, v));
}

[Benchmark]
public void IgnoreArray()
{
_array
_array!
.IgnoreElements()
.Subscribe(v => Volatile.Write(ref _store, v));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
Expand All @@ -16,7 +15,7 @@ public class MinMaxBenchmark
[Params(1, 10, 100, 1000, 10000, 100000, 1000000)]
public int N;
private int _store;
private IList<int> _listStore;
private IList<int>? _listStore;

private readonly IComparer<int> _comparer = Comparer<int>.Default;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private static ILookup<string, MethodInfo> GetMethods(Type type)
return type.GetMethods(BindingFlags.Static | BindingFlags.Public).ToLookup(m => m.Name);
}

private static bool ArgsMatch(MethodInfo method, IList<Expression> arguments, Type[] typeArgs)
private static bool ArgsMatch(MethodInfo method, IList<Expression> arguments, Type[]? typeArgs)
{
//
// Number of parameters should match. Notice we've sanitized IQueryProvider "this"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static IEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IEnu

private static IEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
{
var currentKey = default(TKey);
var currentKey = default(TKey)!;
var hasCurrentKey = false;

foreach (var item in source)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,12 @@ public static IEnumerable<TResult> Memoize<TSource, TResult>(this IEnumerable<TS

private sealed class MemoizedBuffer<T> : IBuffer<T>
{
private IRefCountList<T> _buffer;
private readonly object _gate = new object();
private readonly IRefCountList<T> _buffer;
private readonly IEnumerator<T> _source;

private bool _disposed;
private Exception? _error;
private IEnumerator<T> _source;
private bool _stopped;

public MemoizedBuffer(IEnumerator<T> source)
Expand Down Expand Up @@ -153,15 +155,12 @@ IEnumerator IEnumerable.GetEnumerator()

public void Dispose()
{
lock (_source)
lock (_gate)
{
if (!_disposed)
{
_source.Dispose();
_source = null;

_buffer.Clear();
_buffer = null;
}

_disposed = true;
Expand All @@ -180,9 +179,9 @@ private IEnumerator<T> GetEnumerator_()
throw new ObjectDisposedException("");

var hasValue = default(bool);
var current = default(T);
var current = default(T)!;

lock (_source)
lock (_gate)
{
if (i >= _buffer.Count)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private static IEnumerable<TSource> OnErrorResumeNextCore<TSource>(IEnumerable<I

while (true)
{
var value = default(TSource);
TSource value;
try
{
if (!innerEnumerator.MoveNext())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ public static IEnumerable<TResult> Publish<TSource, TResult>(this IEnumerable<TS
private sealed class PublishedBuffer<T> : IBuffer<T>
{
private readonly object _gate = new object();
private readonly RefCountList<T> _buffer;
private readonly IEnumerator<T> _source;

private RefCountList<T> _buffer;
private bool _disposed;
private Exception? _error;
private IEnumerator<T> _source;
private bool _stopped;

public PublishedBuffer(IEnumerator<T> source)
Expand Down Expand Up @@ -109,10 +109,7 @@ public void Dispose()
if (!_disposed)
{
_source.Dispose();
_source = null;

_buffer.Clear();
_buffer = null;
}

_disposed = true;
Expand All @@ -131,7 +128,7 @@ private IEnumerator<T> GetEnumeratorCore(int i)
}

var hasValue = default(bool);
var current = default(T);
var current = default(T)!;

lock (_gate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private static IEnumerable<TAccumulate> ScanCore<TSource, TAccumulate>(IEnumerab
private static IEnumerable<TSource> ScanCore<TSource>(IEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator)
{
var hasSeed = false;
var acc = default(TSource);
var acc = default(TSource)!;

foreach (var item in source)
{
Expand Down
13 changes: 8 additions & 5 deletions Ix.NET/Source/System.Interactive/System/Linq/Operators/Share.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public static IEnumerable<TResult> Share<TSource, TResult>(this IEnumerable<TSou

private class SharedBuffer<T> : IBuffer<T>
{
private readonly IEnumerator<T> _source;
private bool _disposed;
private IEnumerator<T> _source;

public SharedBuffer(IEnumerator<T> source)
{
Expand Down Expand Up @@ -89,7 +89,6 @@ public void Dispose()
if (!_disposed)
{
_source.Dispose();
_source = null;
}

_disposed = true;
Expand All @@ -104,11 +103,15 @@ private sealed class ShareEnumerator : IEnumerator<T>

private bool _disposed;

public ShareEnumerator(SharedBuffer<T> parent) => _parent = parent;
public ShareEnumerator(SharedBuffer<T> parent)
{
_parent = parent;
Current = default!;
}

public T Current { get; private set; }

object IEnumerator.Current => Current;
object? IEnumerator.Current => Current;

public void Dispose() => _disposed = true;

Expand Down Expand Up @@ -138,7 +141,7 @@ public bool MoveNext()
return true;
}
_disposed = true;
Current = default;
Current = default!;
return false;
}

Expand Down
12 changes: 9 additions & 3 deletions Ix.NET/Source/System.Interactive/System/Linq/RefCountList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public T this[int i]

public void Add(T item)
{
_list[Count] = new RefCount { Value = item, Count = ReaderCount };
_list[Count] = new RefCount(item, ReaderCount);

Count++;
}
Expand All @@ -62,8 +62,14 @@ public void Done(int index)

private sealed class RefCount
{
public int Count;
public T Value;
public RefCount(T value, int count)
{
Value = value;
Count = count;
}

public int Count { get; set; }
public T Value { get; }
}
}
}
6 changes: 5 additions & 1 deletion Ix.NET/Source/System.Interactive/System/Linq/Yielder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ internal sealed class Yielder<T> : IYielder<T>, IAwaitable, IAwaiter
private bool _running;
private bool _stopped;

public Yielder(Action<Yielder<T>> create) => _create = create;
public Yielder(Action<Yielder<T>> create)
{
_create = create;
Current = default!;
}

public T Current { get; private set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
return node;
}

var declType = node.Method.DeclaringType;
var typeArgs = node.Method.IsGenericMethod ? node.Method.GetGenericArguments() : null;

//
// Check whether the method is compatible with the recursively rewritten instance
// and arguments expressions. If so, create a new call expression.
//
if ((node.Method.IsStatic || node.Method.DeclaringType.IsAssignableFrom(obj.Type)) && ArgsMatch(node.Method, args, typeArgs))
if ((node.Method.IsStatic || declType != null && declType.IsAssignableFrom(obj.Type)) && ArgsMatch(node.Method, args, typeArgs))
{
return Expression.Call(obj, node.Method, args);
}
Expand All @@ -75,15 +76,20 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
// Find a corresponding method in the non-expression world, e.g. rewriting from
// the AsyncQueryable methods to the ones on AsyncEnumerable.
//
if (node.Method.DeclaringType == typeof(AsyncQueryable))
if (declType == typeof(AsyncQueryable))
{
method = FindEnumerableMethod(node.Method.Name, args, typeArgs);
args = FixupQuotedArgs(method, args);
return Expression.Call(obj, method, args);
}
else
{
method = FindMethod(node.Method.DeclaringType, node.Method.Name, args, typeArgs, BindingFlags.Static | (node.Method.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic));
if (declType == null)
{
throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "Could not rewrite method with name '{0}' without a DeclaringType.", node.Method.Name));
}

method = FindMethod(declType, node.Method.Name, args, typeArgs, BindingFlags.Static | (node.Method.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic));
args = FixupQuotedArgs(method, args);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ select GetDelegateKind(p.ParameterType))
static bool IsTaskLike(Type t) => IsTask(t) || IsValueTask(t);

static bool IsDelegate(Type t) => typeof(Delegate).IsAssignableFrom(t);
static bool TryGetInvoke(Type t, out MethodInfo m) => (m = t.GetMethod("Invoke")) != null;
static bool IsAsyncDelegate(Type t) => IsDelegate(t) && TryGetInvoke(t, out var i) && IsTaskLike(i.ReturnType);
static bool IsCancelableDelegate(Type t) => IsDelegate(t) && TryGetInvoke(t, out var i) && i.GetParameters().LastOrDefault()?.ParameterType == typeof(CancellationToken);
static bool TryGetInvoke(Type t, out MethodInfo? m) => (m = t.GetMethod("Invoke")) != null;
static bool IsAsyncDelegate(Type t) => IsDelegate(t) && TryGetInvoke(t, out var i) && IsTaskLike(i!.ReturnType);
static bool IsCancelableDelegate(Type t) => IsDelegate(t) && TryGetInvoke(t, out var i) && i!.GetParameters().LastOrDefault()?.ParameterType == typeof(CancellationToken);
static DelegateKind GetDelegateKind(Type t) => IsAsyncDelegate(t) ? (IsCancelableDelegate(t) ? DelegateKind.AsyncCancel : DelegateKind.Async) : DelegateKind.Sync;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ public SingleLinkedNode<TSource> GetNode(int index)
{
Debug.Assert(index >= 0 && index < GetCount());

SingleLinkedNode<TSource>? node = this;
SingleLinkedNode<TSource> node = this;
for (; index > 0; index--)
{
node = node!.Linked;
node = node.Linked!;
Debug.Assert(node != null);
}

Debug.Assert(node != null);
return node;
return node!;
}

/// <summary>
Expand Down