From 2a8c658d8ba1af1fe21dd70e73fba4d12a85f04f Mon Sep 17 00:00:00 2001 From: Ian Griffiths Date: Fri, 17 Mar 2023 09:16:22 +0000 Subject: [PATCH] Fix compiler diagnostics in AsyncRx.NET (#1899) There were various warnings and messages due either to inconsistent style, or because in modern C#, newer idioms are preferred. This changes the code to be consistent with current standards. In a couple of cases, the warnings were spurious, and have been suppressed. --- AsyncRx.NET/ApiCompare/Program.cs | 4 +- AsyncRx.NET/Playground/Program.cs | 66 +++++++++---------- .../AsyncObservableBase.cs | 4 +- .../Concurrency/AsyncScheduler.cs | 2 +- .../Concurrency/AsyncSchedulerBase.cs | 2 +- .../Concurrency/ImmediateAsyncScheduler.cs | 2 +- .../SynchronizationContextAsyncScheduler.cs | 2 +- .../Concurrency/TaskPoolAsyncScheduler.cs | 2 +- .../Disposables/CompositeAsyncDisposable.cs | 2 +- .../Disposables/RefCountAsyncDisposable.cs | 2 +- .../Disposables/SerialAsyncDisposable.cs | 2 +- .../Internal/AsyncQueueLockAsyncObserver.cs | 2 +- .../EventPatternSourceBaseInternal.cs | 5 +- .../Internal/EventSource.cs | 5 +- .../Internal/FastImmediateAsyncObserver.cs | 6 +- .../System.Reactive.Async/Internal/Lookup.cs | 24 +++---- .../Internal/ScheduledAsyncObserver.cs | 6 +- .../Internal/ScheduledAsyncObserverBase.cs | 4 +- .../Internal/UnsafeAsyncObserver.cs | 13 +--- .../Joins/ActiveAsyncPlan.cs | 6 +- .../Joins/AsyncJoinObserver.cs | 4 +- .../AsyncMethodBuilderAttribute.cs | 2 + .../AsyncObservableMethodBuilder.cs | 1 - .../Subjects/AsyncAsyncSubject.cs | 4 +- .../Subjects/BehaviorAsyncSubject.cs | 4 +- .../Subjects/ConcurrentAsyncAsyncSubject.cs | 6 +- .../ConcurrentBehaviorAsyncSubject.cs | 6 +- .../Subjects/ConcurrentSimpleAsyncSubject.cs | 6 +- .../Subjects/ConnectableAsyncObservable.cs | 42 ++++++------ .../Subjects/ReplayAsyncSubject.cs | 8 +-- .../Subjects/SimpleAsyncSubject.cs | 4 +- .../Threading/AsyncGate.cs | 8 +-- .../Threading/AsyncQueueLock.cs | 4 +- .../Threading/Tasks/TaskAwaitable.cs | 4 +- .../Threading/Tasks/ValueTaskAwaitable.cs | 4 +- 35 files changed, 128 insertions(+), 140 deletions(-) diff --git a/AsyncRx.NET/ApiCompare/Program.cs b/AsyncRx.NET/ApiCompare/Program.cs index a5fca2129a..d3e4d745ac 100644 --- a/AsyncRx.NET/ApiCompare/Program.cs +++ b/AsyncRx.NET/ApiCompare/Program.cs @@ -9,9 +9,9 @@ namespace ApiCompare { - class Program + internal class Program { - static void Main() + private static void Main() { var observable = typeof(Observable).GetMethods(BindingFlags.Public | BindingFlags.Static).Select(m => m.Name).Distinct(); var asyncObservable = typeof(AsyncObservable).GetMethods(BindingFlags.Public | BindingFlags.Static).Select(m => m.Name).Distinct(); diff --git a/AsyncRx.NET/Playground/Program.cs b/AsyncRx.NET/Playground/Program.cs index bb3d96ad35..e56fc96883 100644 --- a/AsyncRx.NET/Playground/Program.cs +++ b/AsyncRx.NET/Playground/Program.cs @@ -2,6 +2,11 @@ // The .NET Foundation licenses this file to you under the MIT License. // See the LICENSE file in the project root for more information. +// The intention is that people will uncomment whichever method call in Main they want to try. +// The following suppressions prevent warnings due to 'unused' members, and the fact that all of +// the await statements in Main are commented out to start with +#pragma warning disable IDE0051, CS1998 + using System; using System.Linq; using System.Reactive; @@ -12,16 +17,9 @@ namespace Playground { - static class Program + internal static class Program { - static void Main() - { - MainAsync().GetAwaiter().GetResult(); - - Console.ReadLine(); - } - - static async Task MainAsync() + private static async Task Main() { //await AggregateAsync(); //await AllAsync(); @@ -45,34 +43,36 @@ static async Task MainAsync() //await TakeUntilAsync(); //await TimerAsync(); //await WhileAsync(); + + Console.ReadLine(); } - static async Task AggregateAsync() + private static async Task AggregateAsync() { await AsyncObservable.Range(0, 10).Aggregate(0, (sum, x) => sum + x).SubscribeAsync(Print()); } - static async Task AllAsync() + private static async Task AllAsync() { await AsyncObservable.Range(0, 10).All(x => x < 10).SubscribeAsync(Print()); } - static async Task AnyAsync() + private static async Task AnyAsync() { await AsyncObservable.Range(0, 10).Any(x => x == 5).SubscribeAsync(Print()); } - static async Task AppendAsync() + private static async Task AppendAsync() { await AsyncObservable.Range(0, 10).Append(42).SubscribeAsync(Print()); } - static async Task AwaitAsync() + private static async Task AwaitAsync() { Console.WriteLine(await AsyncObservable.Range(0, 10)); } - static async Task BufferTimeHoppingAsync() + private static async Task BufferTimeHoppingAsync() { await AsyncObservable @@ -82,7 +82,7 @@ static async Task BufferTimeHoppingAsync() .SubscribeAsync(Print()); // TODO: Use ForEachAsync. } - static async Task BufferTimeSlidingAsync() + private static async Task BufferTimeSlidingAsync() { await AsyncObservable @@ -93,7 +93,7 @@ static async Task BufferTimeSlidingAsync() .SubscribeAsync(Print()); // TODO: Use ForEachAsync. } - static async Task CombineLatestAsync() + private static async Task CombineLatestAsync() { await AsyncObservable.CombineLatest( @@ -104,7 +104,7 @@ static async Task CombineLatestAsync() .SubscribeAsync(Print()); // TODO: Use ForEachAsync. } - static async Task ConcatAsync() + private static async Task ConcatAsync() { await AsyncObservable.Concat( @@ -116,7 +116,7 @@ static async Task ConcatAsync() .SubscribeAsync(Print()); // TODO: Use ForEachAsync. } - static async Task DelayAsync() + private static async Task DelayAsync() { await AsyncObservable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1)) @@ -127,7 +127,7 @@ static async Task DelayAsync() .SubscribeAsync(Print()); // TODO: Use ForEachAsync. } - static async Task GroupByAsync() + private static async Task GroupByAsync() { await AsyncObservable.Interval(TimeSpan.FromMilliseconds(250)) @@ -140,7 +140,7 @@ static async Task GroupByAsync() }); } - static async Task GroupBySelectManyAsync() + private static async Task GroupBySelectManyAsync() { await AsyncObservable.Interval(TimeSpan.FromMilliseconds(250)) @@ -151,7 +151,7 @@ static async Task GroupBySelectManyAsync() .SubscribeAsync(Print()); } - static async Task MergeAsync() + private static async Task MergeAsync() { var subject = new SequentialSimpleAsyncSubject>(); @@ -167,17 +167,17 @@ static async Task MergeAsync() await subject.OnCompletedAsync(); } - static async Task PrependAsync() + private static async Task PrependAsync() { await AsyncObservable.Range(0, 10).Prepend(42).SubscribeAsync(Print()); } - static async Task RangeAsync() + private static async Task RangeAsync() { await AsyncObservable.Range(0, 10).SubscribeAsync(PrintAsync()); // TODO: Use ForEachAsync. } - static async Task ReplaySubjectAsync() + private static async Task ReplaySubjectAsync() { var sub = new SequentialReplayAsyncSubject(5); @@ -208,12 +208,12 @@ static async Task ReplaySubjectAsync() await sub.OnNextAsync(47); } - static async Task ReturnAsync() + private static async Task ReturnAsync() { await AsyncObservable.Return(42).SubscribeAsync(Print()); } - static async Task SelectManyAsync() + private static async Task SelectManyAsync() { var res = from i in AsyncObservable.Range(0, 10) from j in AsyncObservable.Range(i * 10, 10) @@ -222,7 +222,7 @@ from j in AsyncObservable.Range(i * 10, 10) await res.SubscribeAsync(Print()); } - static async Task SubjectAsync() + private static async Task SubjectAsync() { var subject = new SequentialSimpleAsyncSubject(); @@ -238,24 +238,24 @@ static async Task SubjectAsync() await subject.OnCompletedAsync(); } - static async Task TakeUntilAsync() + private static async Task TakeUntilAsync() { await AsyncObservable.Range(0, int.MaxValue).TakeUntil(DateTimeOffset.Now.AddSeconds(5)).SubscribeAsync(Print()); // TODO: Use ForEachAsync. } - static async Task TimerAsync() + private static async Task TimerAsync() { await AsyncObservable.Timer(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2)).Take(5).Select(_ => DateTimeOffset.Now).SubscribeAsync(Print()); // TODO: Use ForEachAsync. } - static async Task WhileAsync() + private static async Task WhileAsync() { var i = 0; await AsyncObservable.While(() => ++i < 5, AsyncObservable.Range(0, 5)).SubscribeAsync(Print()); // TODO: Use ForEachAsync. } - static IAsyncObserver Print() + private static IAsyncObserver Print() { return AsyncObserver.Create( x => @@ -276,7 +276,7 @@ static IAsyncObserver Print() ); } - static IAsyncObserver PrintAsync() + private static IAsyncObserver PrintAsync() { return AsyncObserver.Create( async x => diff --git a/AsyncRx.NET/System.Reactive.Async/AsyncObservableBase.cs b/AsyncRx.NET/System.Reactive.Async/AsyncObservableBase.cs index 02def9d21c..08d0a62a14 100644 --- a/AsyncRx.NET/System.Reactive.Async/AsyncObservableBase.cs +++ b/AsyncRx.NET/System.Reactive.Async/AsyncObservableBase.cs @@ -27,7 +27,7 @@ public async ValueTask SubscribeAsync(IAsyncObserver observ private sealed class AutoDetachAsyncObserver : AsyncObserverBase, IAsyncDisposable { private readonly IAsyncObserver _observer; - private readonly object _gate = new object(); + private readonly object _gate = new(); private IAsyncDisposable _subscription; private ValueTask _task; @@ -62,7 +62,7 @@ public async ValueTask AssignAsync(IAsyncDisposable subscription) public async ValueTask DisposeAsync() { - var task = default(ValueTask); + ValueTask task; var subscription = default(IAsyncDisposable); lock (_gate) diff --git a/AsyncRx.NET/System.Reactive.Async/Concurrency/AsyncScheduler.cs b/AsyncRx.NET/System.Reactive.Async/Concurrency/AsyncScheduler.cs index 455f4c2ac3..7ad1f65439 100644 --- a/AsyncRx.NET/System.Reactive.Async/Concurrency/AsyncScheduler.cs +++ b/AsyncRx.NET/System.Reactive.Async/Concurrency/AsyncScheduler.cs @@ -208,7 +208,7 @@ public RendezVousAwaitable(IAsyncScheduler scheduler, CancellationToken token) _token = token; } - public RendezVousAwaiter GetAwaiter() => new RendezVousAwaiter(_scheduler, _token); + public RendezVousAwaiter GetAwaiter() => new(_scheduler, _token); public sealed class RendezVousAwaiter : INotifyCompletion { diff --git a/AsyncRx.NET/System.Reactive.Async/Concurrency/AsyncSchedulerBase.cs b/AsyncRx.NET/System.Reactive.Async/Concurrency/AsyncSchedulerBase.cs index e073de0c7a..410ac16208 100644 --- a/AsyncRx.NET/System.Reactive.Async/Concurrency/AsyncSchedulerBase.cs +++ b/AsyncRx.NET/System.Reactive.Async/Concurrency/AsyncSchedulerBase.cs @@ -66,7 +66,7 @@ protected virtual async ValueTask ScheduleAsyncCore(Func _cts.Token; diff --git a/AsyncRx.NET/System.Reactive.Async/Concurrency/ImmediateAsyncScheduler.cs b/AsyncRx.NET/System.Reactive.Async/Concurrency/ImmediateAsyncScheduler.cs index 03f9407ddf..0631c2db78 100644 --- a/AsyncRx.NET/System.Reactive.Async/Concurrency/ImmediateAsyncScheduler.cs +++ b/AsyncRx.NET/System.Reactive.Async/Concurrency/ImmediateAsyncScheduler.cs @@ -13,7 +13,7 @@ public sealed class ImmediateAsyncScheduler : AsyncSchedulerBase private ImmediateAsyncScheduler() { } - protected override ValueTask Delay(TimeSpan dueTime, CancellationToken token) => new ValueTask(Task.Delay(dueTime)); + protected override ValueTask Delay(TimeSpan dueTime, CancellationToken token) => new(Task.Delay(dueTime)); protected override ValueTask ScheduleAsyncCore(Func action, CancellationToken token) => action(token); } diff --git a/AsyncRx.NET/System.Reactive.Async/Concurrency/SynchronizationContextAsyncScheduler.cs b/AsyncRx.NET/System.Reactive.Async/Concurrency/SynchronizationContextAsyncScheduler.cs index 78df2439a1..e482a0889e 100644 --- a/AsyncRx.NET/System.Reactive.Async/Concurrency/SynchronizationContextAsyncScheduler.cs +++ b/AsyncRx.NET/System.Reactive.Async/Concurrency/SynchronizationContextAsyncScheduler.cs @@ -16,7 +16,7 @@ public SynchronizationContextAsyncScheduler(SynchronizationContext context) _context = context ?? throw new ArgumentNullException(nameof(context)); } - protected override ValueTask Delay(TimeSpan dueTime, CancellationToken token) => new ValueTask(Task.Delay(dueTime, token)); + protected override ValueTask Delay(TimeSpan dueTime, CancellationToken token) => new(Task.Delay(dueTime, token)); protected override ValueTask ScheduleAsyncCore(Func action, CancellationToken token) { diff --git a/AsyncRx.NET/System.Reactive.Async/Concurrency/TaskPoolAsyncScheduler.cs b/AsyncRx.NET/System.Reactive.Async/Concurrency/TaskPoolAsyncScheduler.cs index 0f809f4381..01c900c6fe 100644 --- a/AsyncRx.NET/System.Reactive.Async/Concurrency/TaskPoolAsyncScheduler.cs +++ b/AsyncRx.NET/System.Reactive.Async/Concurrency/TaskPoolAsyncScheduler.cs @@ -27,7 +27,7 @@ public TaskPoolAsyncScheduler(TaskFactory factory) _factory = factory ?? throw new ArgumentNullException(nameof(factory)); } - protected override ValueTask Delay(TimeSpan dueTime, CancellationToken token) => new ValueTask(Task.Delay(dueTime, token)); + protected override ValueTask Delay(TimeSpan dueTime, CancellationToken token) => new(Task.Delay(dueTime, token)); protected override ValueTask ScheduleAsyncCore(Func action, CancellationToken token) { diff --git a/AsyncRx.NET/System.Reactive.Async/Disposables/CompositeAsyncDisposable.cs b/AsyncRx.NET/System.Reactive.Async/Disposables/CompositeAsyncDisposable.cs index 297bfa6b61..ab78b7a00c 100644 --- a/AsyncRx.NET/System.Reactive.Async/Disposables/CompositeAsyncDisposable.cs +++ b/AsyncRx.NET/System.Reactive.Async/Disposables/CompositeAsyncDisposable.cs @@ -11,7 +11,7 @@ namespace System.Reactive.Disposables { public sealed class CompositeAsyncDisposable : IAsyncDisposable { - private readonly AsyncGate _gate = new AsyncGate(); + private readonly AsyncGate _gate = new(); private readonly List _disposables; private bool _disposed; diff --git a/AsyncRx.NET/System.Reactive.Async/Disposables/RefCountAsyncDisposable.cs b/AsyncRx.NET/System.Reactive.Async/Disposables/RefCountAsyncDisposable.cs index e93e783dc3..93f801af2f 100644 --- a/AsyncRx.NET/System.Reactive.Async/Disposables/RefCountAsyncDisposable.cs +++ b/AsyncRx.NET/System.Reactive.Async/Disposables/RefCountAsyncDisposable.cs @@ -9,7 +9,7 @@ namespace System.Reactive.Disposables { public sealed class RefCountAsyncDisposable : IAsyncDisposable { - private readonly AsyncGate _gate = new AsyncGate(); + private readonly AsyncGate _gate = new(); private IAsyncDisposable _disposable; private bool _primaryDisposed; private int _count; diff --git a/AsyncRx.NET/System.Reactive.Async/Disposables/SerialAsyncDisposable.cs b/AsyncRx.NET/System.Reactive.Async/Disposables/SerialAsyncDisposable.cs index bb1c69ce87..94bb1a6b7d 100644 --- a/AsyncRx.NET/System.Reactive.Async/Disposables/SerialAsyncDisposable.cs +++ b/AsyncRx.NET/System.Reactive.Async/Disposables/SerialAsyncDisposable.cs @@ -9,7 +9,7 @@ namespace System.Reactive.Disposables { public sealed class SerialAsyncDisposable : IAsyncDisposable { - private readonly AsyncGate _gate = new AsyncGate(); + private readonly AsyncGate _gate = new(); private IAsyncDisposable _disposable; private bool _disposed; diff --git a/AsyncRx.NET/System.Reactive.Async/Internal/AsyncQueueLockAsyncObserver.cs b/AsyncRx.NET/System.Reactive.Async/Internal/AsyncQueueLockAsyncObserver.cs index fa0ff6ff29..53cf85d9f9 100644 --- a/AsyncRx.NET/System.Reactive.Async/Internal/AsyncQueueLockAsyncObserver.cs +++ b/AsyncRx.NET/System.Reactive.Async/Internal/AsyncQueueLockAsyncObserver.cs @@ -9,7 +9,7 @@ namespace System.Reactive { internal sealed class AsyncQueueLockAsyncObserver : AsyncObserverBase { - private readonly AsyncQueueLock _gate = new AsyncQueueLock(); + private readonly AsyncQueueLock _gate = new(); private readonly IAsyncObserver _observer; public AsyncQueueLockAsyncObserver(IAsyncObserver observer) diff --git a/AsyncRx.NET/System.Reactive.Async/Internal/EventPatternSourceBaseInternal.cs b/AsyncRx.NET/System.Reactive.Async/Internal/EventPatternSourceBaseInternal.cs index c86286f297..544fe986be 100644 --- a/AsyncRx.NET/System.Reactive.Async/Internal/EventPatternSourceBaseInternal.cs +++ b/AsyncRx.NET/System.Reactive.Async/Internal/EventPatternSourceBaseInternal.cs @@ -120,10 +120,7 @@ protected void Remove(Delegate handler) } } - if (d != null) - { - d.DisposeAsync().GetAwaiter().GetResult(); - } + d?.DisposeAsync().GetAwaiter().GetResult(); } } } diff --git a/AsyncRx.NET/System.Reactive.Async/Internal/EventSource.cs b/AsyncRx.NET/System.Reactive.Async/Internal/EventSource.cs index 67b0b4d543..3296b4682c 100644 --- a/AsyncRx.NET/System.Reactive.Async/Internal/EventSource.cs +++ b/AsyncRx.NET/System.Reactive.Async/Internal/EventSource.cs @@ -91,10 +91,7 @@ private void Remove(Delegate handler) } } - if (d != null) - { - d.DisposeAsync().GetAwaiter().GetResult(); - } + d?.DisposeAsync().GetAwaiter().GetResult(); } } } diff --git a/AsyncRx.NET/System.Reactive.Async/Internal/FastImmediateAsyncObserver.cs b/AsyncRx.NET/System.Reactive.Async/Internal/FastImmediateAsyncObserver.cs index 0e433248c0..8e16e61167 100644 --- a/AsyncRx.NET/System.Reactive.Async/Internal/FastImmediateAsyncObserver.cs +++ b/AsyncRx.NET/System.Reactive.Async/Internal/FastImmediateAsyncObserver.cs @@ -10,7 +10,7 @@ namespace System.Reactive { internal sealed class FastImmediateAsyncObserver : ScheduledAsyncObserverBase { - private readonly CancellationAsyncDisposable _disposable = new CancellationAsyncDisposable(); + private readonly CancellationAsyncDisposable _disposable = new(); public FastImmediateAsyncObserver(IAsyncObserver observer) : base(observer) @@ -19,9 +19,9 @@ public FastImmediateAsyncObserver(IAsyncObserver observer) public override ValueTask DisposeAsync() => _disposable.DisposeAsync(); - protected override ValueTaskAwaitable RendezVous(ValueTask task) => new ValueTaskAwaitable(task, continueOnCapturedContext: false, scheduler: null, CancellationToken.None); + protected override ValueTaskAwaitable RendezVous(ValueTask task) => new(task, continueOnCapturedContext: false, scheduler: null, CancellationToken.None); - protected override ValueTaskAwaitable RendezVous(ValueTask task) => new ValueTaskAwaitable(task, continueOnCapturedContext: false, scheduler: null, CancellationToken.None); + protected override ValueTaskAwaitable RendezVous(ValueTask task) => new(task, continueOnCapturedContext: false, scheduler: null, CancellationToken.None); protected override ValueTask ScheduleAsync() => RunAsync(_disposable.Token); } diff --git a/AsyncRx.NET/System.Reactive.Async/Internal/Lookup.cs b/AsyncRx.NET/System.Reactive.Async/Internal/Lookup.cs index 9aaf8d4ed4..f42b13f429 100644 --- a/AsyncRx.NET/System.Reactive.Async/Internal/Lookup.cs +++ b/AsyncRx.NET/System.Reactive.Async/Internal/Lookup.cs @@ -10,30 +10,30 @@ namespace System.Reactive { internal sealed class Lookup : ILookup { - private readonly Dictionary> d; + private readonly Dictionary> _d; public Lookup(IEqualityComparer comparer) { - d = new Dictionary>(comparer); + _d = new Dictionary>(comparer); } public void Add(K key, E element) { - if (!d.TryGetValue(key, out var list)) - d[key] = list = new List(); + if (!_d.TryGetValue(key, out var list)) + _d[key] = list = new List(); list.Add(element); } - public bool Contains(K key) => d.ContainsKey(key); + public bool Contains(K key) => _d.ContainsKey(key); - public int Count => d.Count; + public int Count => _d.Count; public IEnumerable this[K key] { get { - if (!d.TryGetValue(key, out var list)) + if (!_d.TryGetValue(key, out var list)) return Enumerable.Empty(); return Hide(list); @@ -48,22 +48,22 @@ private IEnumerable Hide(List elements) public IEnumerator> GetEnumerator() { - foreach (var kv in d) + foreach (var kv in _d) yield return new Grouping(kv); } private sealed class Grouping : IGrouping { - private readonly KeyValuePair> kv; + private readonly KeyValuePair> _kv; public Grouping(KeyValuePair> kv) { - this.kv = kv; + _kv = kv; } - public K Key => kv.Key; + public K Key => _kv.Key; - public IEnumerator GetEnumerator() => kv.Value.GetEnumerator(); + public IEnumerator GetEnumerator() => _kv.Value.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } diff --git a/AsyncRx.NET/System.Reactive.Async/Internal/ScheduledAsyncObserver.cs b/AsyncRx.NET/System.Reactive.Async/Internal/ScheduledAsyncObserver.cs index eb31ed3c6d..e20eac29d2 100644 --- a/AsyncRx.NET/System.Reactive.Async/Internal/ScheduledAsyncObserver.cs +++ b/AsyncRx.NET/System.Reactive.Async/Internal/ScheduledAsyncObserver.cs @@ -13,7 +13,7 @@ internal sealed class ScheduledAsyncObserver : ScheduledAsyncObserverBase { private readonly IAsyncScheduler _scheduler; - private readonly SerialAsyncDisposable _disposable = new SerialAsyncDisposable(); + private readonly SerialAsyncDisposable _disposable = new(); public ScheduledAsyncObserver(IAsyncObserver observer, IAsyncScheduler scheduler) : base(observer) @@ -23,9 +23,9 @@ public ScheduledAsyncObserver(IAsyncObserver observer, IAsyncScheduler schedu public override ValueTask DisposeAsync() => _disposable.DisposeAsync(); - protected override ValueTaskAwaitable RendezVous(ValueTask task) => new ValueTaskAwaitable(task, continueOnCapturedContext: false, _scheduler, CancellationToken.None); + protected override ValueTaskAwaitable RendezVous(ValueTask task) => new(task, continueOnCapturedContext: false, _scheduler, CancellationToken.None); - protected override ValueTaskAwaitable RendezVous(ValueTask task) => new ValueTaskAwaitable(task, continueOnCapturedContext: false, _scheduler, CancellationToken.None); + protected override ValueTaskAwaitable RendezVous(ValueTask task) => new(task, continueOnCapturedContext: false, _scheduler, CancellationToken.None); protected override async ValueTask ScheduleAsync() { diff --git a/AsyncRx.NET/System.Reactive.Async/Internal/ScheduledAsyncObserverBase.cs b/AsyncRx.NET/System.Reactive.Async/Internal/ScheduledAsyncObserverBase.cs index b255b98de5..36fe60b463 100644 --- a/AsyncRx.NET/System.Reactive.Async/Internal/ScheduledAsyncObserverBase.cs +++ b/AsyncRx.NET/System.Reactive.Async/Internal/ScheduledAsyncObserverBase.cs @@ -12,8 +12,8 @@ internal abstract class ScheduledAsyncObserverBase : AsyncObserverBase, IS { private readonly IAsyncObserver _observer; - private readonly AsyncGate _lock = new AsyncGate(); - private readonly Queue _queue = new Queue(); + private readonly AsyncGate _lock = new(); + private readonly Queue _queue = new(); private bool _hasFaulted = false; private bool _busy = false; diff --git a/AsyncRx.NET/System.Reactive.Async/Internal/UnsafeAsyncObserver.cs b/AsyncRx.NET/System.Reactive.Async/Internal/UnsafeAsyncObserver.cs index f8d85c3de5..9e5765de9b 100644 --- a/AsyncRx.NET/System.Reactive.Async/Internal/UnsafeAsyncObserver.cs +++ b/AsyncRx.NET/System.Reactive.Async/Internal/UnsafeAsyncObserver.cs @@ -14,16 +14,9 @@ public class UnsafeAsyncObserver : IAsyncObserver public UnsafeAsyncObserver(Func onNextAsync, Func onErrorAsync, Func onCompletedAsync) { - if (onNextAsync == null) - throw new ArgumentNullException(nameof(onNextAsync)); - if (onErrorAsync == null) - throw new ArgumentNullException(nameof(onErrorAsync)); - if (onCompletedAsync == null) - throw new ArgumentNullException(nameof(onCompletedAsync)); - - _onNextAsync = onNextAsync; - _onErrorAsync = onErrorAsync; - _onCompletedAsync = onCompletedAsync; + _onNextAsync = onNextAsync ?? throw new ArgumentNullException(nameof(onNextAsync)); + _onErrorAsync = onErrorAsync ?? throw new ArgumentNullException(nameof(onErrorAsync)); + _onCompletedAsync = onCompletedAsync ?? throw new ArgumentNullException(nameof(onCompletedAsync)); } public ValueTask OnCompletedAsync() => _onCompletedAsync(); diff --git a/AsyncRx.NET/System.Reactive.Async/Joins/ActiveAsyncPlan.cs b/AsyncRx.NET/System.Reactive.Async/Joins/ActiveAsyncPlan.cs index 2054aa15a2..7ba4a93393 100644 --- a/AsyncRx.NET/System.Reactive.Async/Joins/ActiveAsyncPlan.cs +++ b/AsyncRx.NET/System.Reactive.Async/Joins/ActiveAsyncPlan.cs @@ -9,18 +9,18 @@ namespace System.Reactive.Joins { internal abstract class ActiveAsyncPlan { - private readonly Dictionary joinObservers = new Dictionary(); + private readonly Dictionary _joinObservers = new(); internal abstract Task Match(); protected void AddJoinObserver(IAsyncJoinObserver joinObserver) { - joinObservers.Add(joinObserver, joinObserver); + _joinObservers.Add(joinObserver, joinObserver); } protected void Dequeue() { - foreach (var observer in joinObservers.Values) + foreach (var observer in _joinObservers.Values) { observer.Dequeue(); } diff --git a/AsyncRx.NET/System.Reactive.Async/Joins/AsyncJoinObserver.cs b/AsyncRx.NET/System.Reactive.Async/Joins/AsyncJoinObserver.cs index 3b44bc038f..e1e8df311e 100644 --- a/AsyncRx.NET/System.Reactive.Async/Joins/AsyncJoinObserver.cs +++ b/AsyncRx.NET/System.Reactive.Async/Joins/AsyncJoinObserver.cs @@ -15,8 +15,8 @@ internal sealed class AsyncJoinObserver : AsyncObserverBase>, private readonly IAsyncObservable _source; private readonly Func _onError; - private readonly List _activePlans = new List(); - private readonly SingleAssignmentAsyncDisposable _subscription = new SingleAssignmentAsyncDisposable(); + private readonly List _activePlans = new(); + private readonly SingleAssignmentAsyncDisposable _subscription = new(); private AsyncGate _gate; private bool _isDisposed; diff --git a/AsyncRx.NET/System.Reactive.Async/Runtime/CompilerServices/AsyncMethodBuilderAttribute.cs b/AsyncRx.NET/System.Reactive.Async/Runtime/CompilerServices/AsyncMethodBuilderAttribute.cs index cc84a9f084..bdc6892cc4 100644 --- a/AsyncRx.NET/System.Reactive.Async/Runtime/CompilerServices/AsyncMethodBuilderAttribute.cs +++ b/AsyncRx.NET/System.Reactive.Async/Runtime/CompilerServices/AsyncMethodBuilderAttribute.cs @@ -13,7 +13,9 @@ internal sealed class AsyncMethodBuilderAttribute : Attribute /// Creates a new instance of the attribute using the specified . /// /// The type implementing the asynchronous method builder. +#pragma warning disable IDE0060 // Remove unused parameter - this attribute is understood intrinsically by the compiler, so it doesn't need to use its argument. public AsyncMethodBuilderAttribute(Type type) +#pragma warning restore IDE0060 // Remove unused parameter { } } diff --git a/AsyncRx.NET/System.Reactive.Async/Runtime/CompilerServices/AsyncObservableMethodBuilder.cs b/AsyncRx.NET/System.Reactive.Async/Runtime/CompilerServices/AsyncObservableMethodBuilder.cs index d0c85f9799..42ddb58eac 100644 --- a/AsyncRx.NET/System.Reactive.Async/Runtime/CompilerServices/AsyncObservableMethodBuilder.cs +++ b/AsyncRx.NET/System.Reactive.Async/Runtime/CompilerServices/AsyncObservableMethodBuilder.cs @@ -205,7 +205,6 @@ private static void Rethrow(Exception exception) /// } /// /// - /// The type of the elements in the sequence. internal sealed class TaskObservable : IAsyncObservable, INotifyCompletion { /// diff --git a/AsyncRx.NET/System.Reactive.Async/Subjects/AsyncAsyncSubject.cs b/AsyncRx.NET/System.Reactive.Async/Subjects/AsyncAsyncSubject.cs index 93cd054c34..c842a72684 100644 --- a/AsyncRx.NET/System.Reactive.Async/Subjects/AsyncAsyncSubject.cs +++ b/AsyncRx.NET/System.Reactive.Async/Subjects/AsyncAsyncSubject.cs @@ -13,8 +13,8 @@ namespace System.Reactive.Subjects { public abstract class AsyncAsyncSubject : IAsyncSubject, INotifyCompletion { - private readonly object _gate = new object(); - private readonly List> _observers = new List>(); + private readonly object _gate = new(); + private readonly List> _observers = new(); private bool _hasValue; private T _value; private bool _done; diff --git a/AsyncRx.NET/System.Reactive.Async/Subjects/BehaviorAsyncSubject.cs b/AsyncRx.NET/System.Reactive.Async/Subjects/BehaviorAsyncSubject.cs index 16b4ecc51b..5f12f775c3 100644 --- a/AsyncRx.NET/System.Reactive.Async/Subjects/BehaviorAsyncSubject.cs +++ b/AsyncRx.NET/System.Reactive.Async/Subjects/BehaviorAsyncSubject.cs @@ -11,8 +11,8 @@ namespace System.Reactive.Subjects { public abstract class BehaviorAsyncSubject : IAsyncSubject { - private readonly AsyncGate _gate = new AsyncGate(); - private readonly List> _observers = new List>(); + private readonly AsyncGate _gate = new(); + private readonly List> _observers = new(); private T _value; private bool _done; private Exception _error; diff --git a/AsyncRx.NET/System.Reactive.Async/Subjects/ConcurrentAsyncAsyncSubject.cs b/AsyncRx.NET/System.Reactive.Async/Subjects/ConcurrentAsyncAsyncSubject.cs index 6e373a12d8..2ce5018a0b 100644 --- a/AsyncRx.NET/System.Reactive.Async/Subjects/ConcurrentAsyncAsyncSubject.cs +++ b/AsyncRx.NET/System.Reactive.Async/Subjects/ConcurrentAsyncAsyncSubject.cs @@ -10,10 +10,10 @@ namespace System.Reactive.Subjects { public sealed class ConcurrentAsyncAsyncSubject : AsyncAsyncSubject { - protected override ValueTask OnCompletedAsyncCore(IEnumerable> observers) => new ValueTask(Task.WhenAll(observers.Select(observer => observer.OnCompletedAsync().AsTask()))); + protected override ValueTask OnCompletedAsyncCore(IEnumerable> observers) => new(Task.WhenAll(observers.Select(observer => observer.OnCompletedAsync().AsTask()))); - protected override ValueTask OnErrorAsyncCore(IEnumerable> observers, Exception error) => new ValueTask(Task.WhenAll(observers.Select(observer => observer.OnErrorAsync(error).AsTask()))); + protected override ValueTask OnErrorAsyncCore(IEnumerable> observers, Exception error) => new(Task.WhenAll(observers.Select(observer => observer.OnErrorAsync(error).AsTask()))); - protected override ValueTask OnNextAsyncCore(IEnumerable> observers, T value) => new ValueTask(Task.WhenAll(observers.Select(observer => observer.OnNextAsync(value).AsTask()))); + protected override ValueTask OnNextAsyncCore(IEnumerable> observers, T value) => new(Task.WhenAll(observers.Select(observer => observer.OnNextAsync(value).AsTask()))); } } diff --git a/AsyncRx.NET/System.Reactive.Async/Subjects/ConcurrentBehaviorAsyncSubject.cs b/AsyncRx.NET/System.Reactive.Async/Subjects/ConcurrentBehaviorAsyncSubject.cs index 3047ec9d76..86192d2002 100644 --- a/AsyncRx.NET/System.Reactive.Async/Subjects/ConcurrentBehaviorAsyncSubject.cs +++ b/AsyncRx.NET/System.Reactive.Async/Subjects/ConcurrentBehaviorAsyncSubject.cs @@ -15,10 +15,10 @@ public ConcurrentBehaviorAsyncSubject(T value) { } - protected override ValueTask OnCompletedAsyncCore(IEnumerable> observers) => new ValueTask(Task.WhenAll(observers.Select(observer => observer.OnCompletedAsync().AsTask()))); + protected override ValueTask OnCompletedAsyncCore(IEnumerable> observers) => new(Task.WhenAll(observers.Select(observer => observer.OnCompletedAsync().AsTask()))); - protected override ValueTask OnErrorAsyncCore(IEnumerable> observers, Exception error) => new ValueTask(Task.WhenAll(observers.Select(observer => observer.OnErrorAsync(error).AsTask()))); + protected override ValueTask OnErrorAsyncCore(IEnumerable> observers, Exception error) => new(Task.WhenAll(observers.Select(observer => observer.OnErrorAsync(error).AsTask()))); - protected override ValueTask OnNextAsyncCore(IEnumerable> observers, T value) => new ValueTask(Task.WhenAll(observers.Select(observer => observer.OnNextAsync(value).AsTask()))); + protected override ValueTask OnNextAsyncCore(IEnumerable> observers, T value) => new(Task.WhenAll(observers.Select(observer => observer.OnNextAsync(value).AsTask()))); } } diff --git a/AsyncRx.NET/System.Reactive.Async/Subjects/ConcurrentSimpleAsyncSubject.cs b/AsyncRx.NET/System.Reactive.Async/Subjects/ConcurrentSimpleAsyncSubject.cs index b1c554f310..7428fad5f1 100644 --- a/AsyncRx.NET/System.Reactive.Async/Subjects/ConcurrentSimpleAsyncSubject.cs +++ b/AsyncRx.NET/System.Reactive.Async/Subjects/ConcurrentSimpleAsyncSubject.cs @@ -10,10 +10,10 @@ namespace System.Reactive.Subjects { public sealed class ConcurrentSimpleAsyncSubject : SimpleAsyncSubject { - protected override ValueTask OnCompletedAsyncCore(IEnumerable> observers) => new ValueTask(Task.WhenAll(observers.Select(observer => observer.OnCompletedAsync().AsTask()))); + protected override ValueTask OnCompletedAsyncCore(IEnumerable> observers) => new(Task.WhenAll(observers.Select(observer => observer.OnCompletedAsync().AsTask()))); - protected override ValueTask OnErrorAsyncCore(IEnumerable> observers, Exception error) => new ValueTask(Task.WhenAll(observers.Select(observer => observer.OnErrorAsync(error).AsTask()))); + protected override ValueTask OnErrorAsyncCore(IEnumerable> observers, Exception error) => new(Task.WhenAll(observers.Select(observer => observer.OnErrorAsync(error).AsTask()))); - protected override ValueTask OnNextAsyncCore(IEnumerable> observers, T value) => new ValueTask(Task.WhenAll(observers.Select(observer => observer.OnNextAsync(value).AsTask()))); + protected override ValueTask OnNextAsyncCore(IEnumerable> observers, T value) => new(Task.WhenAll(observers.Select(observer => observer.OnNextAsync(value).AsTask()))); } } diff --git a/AsyncRx.NET/System.Reactive.Async/Subjects/ConnectableAsyncObservable.cs b/AsyncRx.NET/System.Reactive.Async/Subjects/ConnectableAsyncObservable.cs index 95e94dee67..3723ce014f 100644 --- a/AsyncRx.NET/System.Reactive.Async/Subjects/ConnectableAsyncObservable.cs +++ b/AsyncRx.NET/System.Reactive.Async/Subjects/ConnectableAsyncObservable.cs @@ -10,53 +10,53 @@ namespace System.Reactive.Subjects { internal sealed class ConnectableAsyncObservable : IConnectableAsyncObservable { - private readonly IAsyncSubject subject; - private readonly IAsyncObservable source; - private readonly AsyncGate gate = new AsyncGate(); + private readonly IAsyncSubject _subject; + private readonly IAsyncObservable _source; + private readonly AsyncGate _gate = new(); - private Connection connection; + private Connection _connection; public ConnectableAsyncObservable(IAsyncObservable source, IAsyncSubject subject) { - this.subject = subject; - this.source = source.AsAsyncObservable(); + _subject = subject; + _source = source.AsAsyncObservable(); } public async ValueTask ConnectAsync() { - using (await gate.LockAsync().ConfigureAwait(false)) + using (await _gate.LockAsync().ConfigureAwait(false)) { - if (connection == null) + if (_connection == null) { - var subscription = await source.SubscribeAsync(subject).ConfigureAwait(false); - connection = new Connection(this, subscription); + var subscription = await _source.SubscribeAsync(_subject).ConfigureAwait(false); + _connection = new Connection(this, subscription); } - return connection; + return _connection; } } private sealed class Connection : IAsyncDisposable { - private readonly ConnectableAsyncObservable parent; - private IAsyncDisposable subscription; + private readonly ConnectableAsyncObservable _parent; + private IAsyncDisposable _subscription; public Connection(ConnectableAsyncObservable parent, IAsyncDisposable subscription) { - this.parent = parent; - this.subscription = subscription; + _parent = parent; + _subscription = subscription; } public async ValueTask DisposeAsync() { - using (await parent.gate.LockAsync().ConfigureAwait(false)) + using (await _parent._gate.LockAsync().ConfigureAwait(false)) { - if (subscription != null) + if (_subscription != null) { - await subscription.DisposeAsync().ConfigureAwait(false); - subscription = null; + await _subscription.DisposeAsync().ConfigureAwait(false); + _subscription = null; - parent.connection = null; + _parent._connection = null; } } } @@ -67,7 +67,7 @@ public ValueTask SubscribeAsync(IAsyncObserver observ if (observer == null) throw new ArgumentNullException(nameof(observer)); - return subject.SubscribeAsync(observer); + return _subject.SubscribeAsync(observer); } } } diff --git a/AsyncRx.NET/System.Reactive.Async/Subjects/ReplayAsyncSubject.cs b/AsyncRx.NET/System.Reactive.Async/Subjects/ReplayAsyncSubject.cs index 90e29eb18a..5928dc9957 100644 --- a/AsyncRx.NET/System.Reactive.Async/Subjects/ReplayAsyncSubject.cs +++ b/AsyncRx.NET/System.Reactive.Async/Subjects/ReplayAsyncSubject.cs @@ -120,8 +120,8 @@ public ReplayAsyncSubject(bool concurrent, int bufferSize, TimeSpan window, IAsy private abstract class ReplayBase : IAsyncSubject { private readonly bool _concurrent; - private readonly AsyncGate _lock = new AsyncGate(); - private readonly List> _observers = new List>(); // TODO: immutable array + private readonly AsyncGate _lock = new(); + private readonly List> _observers = new(); // TODO: immutable array private bool _done; private Exception _error; @@ -361,7 +361,7 @@ protected override void Trim() { } private abstract class ReplayManyBase : ReplayBufferBase { - protected readonly Queue Values = new Queue(); + protected readonly Queue Values = new(); public ReplayManyBase(bool concurrent, Func, IScheduledAsyncObserver> createObserver) : base(concurrent, createObserver) @@ -422,7 +422,7 @@ private sealed class ReplayTime : ReplayBufferBase private readonly IAsyncScheduler _scheduler; private readonly int _bufferSize; private readonly TimeSpan _window; - private readonly Queue> _values = new Queue>(); + private readonly Queue> _values = new(); public ReplayTime(bool concurrent, IAsyncScheduler scheduler, int bufferSize, TimeSpan window, Func, IScheduledAsyncObserver> createObserver) : base(concurrent, createObserver) diff --git a/AsyncRx.NET/System.Reactive.Async/Subjects/SimpleAsyncSubject.cs b/AsyncRx.NET/System.Reactive.Async/Subjects/SimpleAsyncSubject.cs index 366a34953d..7b8f45c786 100644 --- a/AsyncRx.NET/System.Reactive.Async/Subjects/SimpleAsyncSubject.cs +++ b/AsyncRx.NET/System.Reactive.Async/Subjects/SimpleAsyncSubject.cs @@ -10,8 +10,8 @@ namespace System.Reactive.Subjects { public abstract class SimpleAsyncSubject : IAsyncSubject { - private readonly object _gate = new object(); - private readonly List> _observers = new List>(); + private readonly object _gate = new(); + private readonly List> _observers = new(); private bool _done; private Exception _error; diff --git a/AsyncRx.NET/System.Reactive.Async/Threading/AsyncGate.cs b/AsyncRx.NET/System.Reactive.Async/Threading/AsyncGate.cs index d1b9ebbced..507aa676f7 100644 --- a/AsyncRx.NET/System.Reactive.Async/Threading/AsyncGate.cs +++ b/AsyncRx.NET/System.Reactive.Async/Threading/AsyncGate.cs @@ -9,9 +9,9 @@ namespace System.Threading { public sealed class AsyncGate { - private readonly object _gate = new object(); - private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1); - private readonly AsyncLocal _recursionCount = new AsyncLocal(); + private readonly object _gate = new(); + private readonly SemaphoreSlim _semaphore = new(1, 1); + private readonly AsyncLocal _recursionCount = new(); public ValueTask LockAsync() { @@ -51,7 +51,7 @@ private void Release() } } - public struct Releaser : IDisposable + public readonly struct Releaser : IDisposable { private readonly AsyncGate _parent; diff --git a/AsyncRx.NET/System.Reactive.Async/Threading/AsyncQueueLock.cs b/AsyncRx.NET/System.Reactive.Async/Threading/AsyncQueueLock.cs index 56e0af029c..407924a459 100644 --- a/AsyncRx.NET/System.Reactive.Async/Threading/AsyncQueueLock.cs +++ b/AsyncRx.NET/System.Reactive.Async/Threading/AsyncQueueLock.cs @@ -9,8 +9,8 @@ namespace System.Threading { public sealed class AsyncQueueLock : IAsyncDisposable { - private readonly Queue> _queue = new Queue>(); - private readonly AsyncGate _gate = new AsyncGate(); + private readonly Queue> _queue = new(); + private readonly AsyncGate _gate = new(); private bool _isAcquired; private bool _hasFaulted; diff --git a/AsyncRx.NET/System.Reactive.Async/Threading/Tasks/TaskAwaitable.cs b/AsyncRx.NET/System.Reactive.Async/Threading/Tasks/TaskAwaitable.cs index af3e5116c6..6b170a1732 100644 --- a/AsyncRx.NET/System.Reactive.Async/Threading/Tasks/TaskAwaitable.cs +++ b/AsyncRx.NET/System.Reactive.Async/Threading/Tasks/TaskAwaitable.cs @@ -22,7 +22,7 @@ public TaskAwaitable(Task task, bool continueOnCapturedContext, IAsyncScheduler _token = token; } - public TaskAwaiter GetAwaiter() => new TaskAwaiter(_task.ConfigureAwait(_continueOnCapturedContext).GetAwaiter(), _scheduler, _token); + public TaskAwaiter GetAwaiter() => new(_task.ConfigureAwait(_continueOnCapturedContext).GetAwaiter(), _scheduler, _token); public readonly struct TaskAwaiter : INotifyCompletion { @@ -111,7 +111,7 @@ public TaskAwaitable(Task task, bool continueOnCapturedContext, IAsyncSchedul _token = token; } - public TaskAwaiter GetAwaiter() => new TaskAwaiter(_task.ConfigureAwait(_continueOnCapturedContext).GetAwaiter(), _scheduler, _token); + public TaskAwaiter GetAwaiter() => new(_task.ConfigureAwait(_continueOnCapturedContext).GetAwaiter(), _scheduler, _token); public readonly struct TaskAwaiter : INotifyCompletion { diff --git a/AsyncRx.NET/System.Reactive.Async/Threading/Tasks/ValueTaskAwaitable.cs b/AsyncRx.NET/System.Reactive.Async/Threading/Tasks/ValueTaskAwaitable.cs index f49e7a511f..47ba3ef577 100644 --- a/AsyncRx.NET/System.Reactive.Async/Threading/Tasks/ValueTaskAwaitable.cs +++ b/AsyncRx.NET/System.Reactive.Async/Threading/Tasks/ValueTaskAwaitable.cs @@ -22,7 +22,7 @@ public ValueTaskAwaitable(ValueTask task, bool continueOnCapturedContext, IAsync _token = token; } - public ValueTaskAwaiter GetAwaiter() => new ValueTaskAwaiter(_task.ConfigureAwait(_continueOnCapturedContext).GetAwaiter(), _scheduler, _token); + public ValueTaskAwaiter GetAwaiter() => new(_task.ConfigureAwait(_continueOnCapturedContext).GetAwaiter(), _scheduler, _token); public readonly struct ValueTaskAwaiter : INotifyCompletion { @@ -111,7 +111,7 @@ public ValueTaskAwaitable(ValueTask task, bool continueOnCapturedContext, IAs _token = token; } - public ValueTaskAwaiter GetAwaiter() => new ValueTaskAwaiter(_task.ConfigureAwait(_continueOnCapturedContext).GetAwaiter(), _scheduler, _token); + public ValueTaskAwaiter GetAwaiter() => new(_task.ConfigureAwait(_continueOnCapturedContext).GetAwaiter(), _scheduler, _token); public readonly struct ValueTaskAwaiter : INotifyCompletion {