Skip to content

Commit 2a8c658

Browse files
authored
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.
1 parent 1eda704 commit 2a8c658

35 files changed

+128
-140
lines changed

AsyncRx.NET/ApiCompare/Program.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
namespace ApiCompare
1111
{
12-
class Program
12+
internal class Program
1313
{
14-
static void Main()
14+
private static void Main()
1515
{
1616
var observable = typeof(Observable).GetMethods(BindingFlags.Public | BindingFlags.Static).Select(m => m.Name).Distinct();
1717
var asyncObservable = typeof(AsyncObservable).GetMethods(BindingFlags.Public | BindingFlags.Static).Select(m => m.Name).Distinct();

AsyncRx.NET/Playground/Program.cs

+33-33
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
// The .NET Foundation licenses this file to you under the MIT License.
33
// See the LICENSE file in the project root for more information.
44

5+
// The intention is that people will uncomment whichever method call in Main they want to try.
6+
// The following suppressions prevent warnings due to 'unused' members, and the fact that all of
7+
// the await statements in Main are commented out to start with
8+
#pragma warning disable IDE0051, CS1998
9+
510
using System;
611
using System.Linq;
712
using System.Reactive;
@@ -12,16 +17,9 @@
1217

1318
namespace Playground
1419
{
15-
static class Program
20+
internal static class Program
1621
{
17-
static void Main()
18-
{
19-
MainAsync().GetAwaiter().GetResult();
20-
21-
Console.ReadLine();
22-
}
23-
24-
static async Task MainAsync()
22+
private static async Task Main()
2523
{
2624
//await AggregateAsync();
2725
//await AllAsync();
@@ -45,34 +43,36 @@ static async Task MainAsync()
4543
//await TakeUntilAsync();
4644
//await TimerAsync();
4745
//await WhileAsync();
46+
47+
Console.ReadLine();
4848
}
4949

50-
static async Task AggregateAsync()
50+
private static async Task AggregateAsync()
5151
{
5252
await AsyncObservable.Range(0, 10).Aggregate(0, (sum, x) => sum + x).SubscribeAsync(Print<int>());
5353
}
5454

55-
static async Task AllAsync()
55+
private static async Task AllAsync()
5656
{
5757
await AsyncObservable.Range(0, 10).All(x => x < 10).SubscribeAsync(Print<bool>());
5858
}
5959

60-
static async Task AnyAsync()
60+
private static async Task AnyAsync()
6161
{
6262
await AsyncObservable.Range(0, 10).Any(x => x == 5).SubscribeAsync(Print<bool>());
6363
}
6464

65-
static async Task AppendAsync()
65+
private static async Task AppendAsync()
6666
{
6767
await AsyncObservable.Range(0, 10).Append(42).SubscribeAsync(Print<int>());
6868
}
6969

70-
static async Task AwaitAsync()
70+
private static async Task AwaitAsync()
7171
{
7272
Console.WriteLine(await AsyncObservable.Range(0, 10));
7373
}
7474

75-
static async Task BufferTimeHoppingAsync()
75+
private static async Task BufferTimeHoppingAsync()
7676
{
7777
await
7878
AsyncObservable
@@ -82,7 +82,7 @@ static async Task BufferTimeHoppingAsync()
8282
.SubscribeAsync(Print<string>()); // TODO: Use ForEachAsync.
8383
}
8484

85-
static async Task BufferTimeSlidingAsync()
85+
private static async Task BufferTimeSlidingAsync()
8686
{
8787
await
8888
AsyncObservable
@@ -93,7 +93,7 @@ static async Task BufferTimeSlidingAsync()
9393
.SubscribeAsync(Print<string>()); // TODO: Use ForEachAsync.
9494
}
9595

96-
static async Task CombineLatestAsync()
96+
private static async Task CombineLatestAsync()
9797
{
9898
await
9999
AsyncObservable.CombineLatest(
@@ -104,7 +104,7 @@ static async Task CombineLatestAsync()
104104
.SubscribeAsync(Print<string>()); // TODO: Use ForEachAsync.
105105
}
106106

107-
static async Task ConcatAsync()
107+
private static async Task ConcatAsync()
108108
{
109109
await
110110
AsyncObservable.Concat(
@@ -116,7 +116,7 @@ static async Task ConcatAsync()
116116
.SubscribeAsync(Print<int>()); // TODO: Use ForEachAsync.
117117
}
118118

119-
static async Task DelayAsync()
119+
private static async Task DelayAsync()
120120
{
121121
await
122122
AsyncObservable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1))
@@ -127,7 +127,7 @@ static async Task DelayAsync()
127127
.SubscribeAsync(Print<string>()); // TODO: Use ForEachAsync.
128128
}
129129

130-
static async Task GroupByAsync()
130+
private static async Task GroupByAsync()
131131
{
132132
await
133133
AsyncObservable.Interval(TimeSpan.FromMilliseconds(250))
@@ -140,7 +140,7 @@ static async Task GroupByAsync()
140140
});
141141
}
142142

143-
static async Task GroupBySelectManyAsync()
143+
private static async Task GroupBySelectManyAsync()
144144
{
145145
await
146146
AsyncObservable.Interval(TimeSpan.FromMilliseconds(250))
@@ -151,7 +151,7 @@ static async Task GroupBySelectManyAsync()
151151
.SubscribeAsync(Print<string>());
152152
}
153153

154-
static async Task MergeAsync()
154+
private static async Task MergeAsync()
155155
{
156156
var subject = new SequentialSimpleAsyncSubject<IAsyncObservable<int>>();
157157

@@ -167,17 +167,17 @@ static async Task MergeAsync()
167167
await subject.OnCompletedAsync();
168168
}
169169

170-
static async Task PrependAsync()
170+
private static async Task PrependAsync()
171171
{
172172
await AsyncObservable.Range(0, 10).Prepend(42).SubscribeAsync(Print<int>());
173173
}
174174

175-
static async Task RangeAsync()
175+
private static async Task RangeAsync()
176176
{
177177
await AsyncObservable.Range(0, 10).SubscribeAsync(PrintAsync<int>()); // TODO: Use ForEachAsync.
178178
}
179179

180-
static async Task ReplaySubjectAsync()
180+
private static async Task ReplaySubjectAsync()
181181
{
182182
var sub = new SequentialReplayAsyncSubject<int>(5);
183183

@@ -208,12 +208,12 @@ static async Task ReplaySubjectAsync()
208208
await sub.OnNextAsync(47);
209209
}
210210

211-
static async Task ReturnAsync()
211+
private static async Task ReturnAsync()
212212
{
213213
await AsyncObservable.Return(42).SubscribeAsync(Print<int>());
214214
}
215215

216-
static async Task SelectManyAsync()
216+
private static async Task SelectManyAsync()
217217
{
218218
var res = from i in AsyncObservable.Range(0, 10)
219219
from j in AsyncObservable.Range(i * 10, 10)
@@ -222,7 +222,7 @@ from j in AsyncObservable.Range(i * 10, 10)
222222
await res.SubscribeAsync(Print<string>());
223223
}
224224

225-
static async Task SubjectAsync()
225+
private static async Task SubjectAsync()
226226
{
227227
var subject = new SequentialSimpleAsyncSubject<int>();
228228

@@ -238,24 +238,24 @@ static async Task SubjectAsync()
238238
await subject.OnCompletedAsync();
239239
}
240240

241-
static async Task TakeUntilAsync()
241+
private static async Task TakeUntilAsync()
242242
{
243243
await AsyncObservable.Range(0, int.MaxValue).TakeUntil(DateTimeOffset.Now.AddSeconds(5)).SubscribeAsync(Print<int>()); // TODO: Use ForEachAsync.
244244
}
245245

246-
static async Task TimerAsync()
246+
private static async Task TimerAsync()
247247
{
248248
await AsyncObservable.Timer(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2)).Take(5).Select(_ => DateTimeOffset.Now).SubscribeAsync(Print<DateTimeOffset>()); // TODO: Use ForEachAsync.
249249
}
250250

251-
static async Task WhileAsync()
251+
private static async Task WhileAsync()
252252
{
253253
var i = 0;
254254

255255
await AsyncObservable.While(() => ++i < 5, AsyncObservable.Range(0, 5)).SubscribeAsync(Print<int>()); // TODO: Use ForEachAsync.
256256
}
257257

258-
static IAsyncObserver<T> Print<T>()
258+
private static IAsyncObserver<T> Print<T>()
259259
{
260260
return AsyncObserver.Create<T>(
261261
x =>
@@ -276,7 +276,7 @@ static IAsyncObserver<T> Print<T>()
276276
);
277277
}
278278

279-
static IAsyncObserver<T> PrintAsync<T>()
279+
private static IAsyncObserver<T> PrintAsync<T>()
280280
{
281281
return AsyncObserver.Create<T>(
282282
async x =>

AsyncRx.NET/System.Reactive.Async/AsyncObservableBase.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public async ValueTask<IAsyncDisposable> SubscribeAsync(IAsyncObserver<T> observ
2727
private sealed class AutoDetachAsyncObserver : AsyncObserverBase<T>, IAsyncDisposable
2828
{
2929
private readonly IAsyncObserver<T> _observer;
30-
private readonly object _gate = new object();
30+
private readonly object _gate = new();
3131

3232
private IAsyncDisposable _subscription;
3333
private ValueTask _task;
@@ -62,7 +62,7 @@ public async ValueTask AssignAsync(IAsyncDisposable subscription)
6262

6363
public async ValueTask DisposeAsync()
6464
{
65-
var task = default(ValueTask);
65+
ValueTask task;
6666
var subscription = default(IAsyncDisposable);
6767

6868
lock (_gate)

AsyncRx.NET/System.Reactive.Async/Concurrency/AsyncScheduler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public RendezVousAwaitable(IAsyncScheduler scheduler, CancellationToken token)
208208
_token = token;
209209
}
210210

211-
public RendezVousAwaiter GetAwaiter() => new RendezVousAwaiter(_scheduler, _token);
211+
public RendezVousAwaiter GetAwaiter() => new(_scheduler, _token);
212212

213213
public sealed class RendezVousAwaiter : INotifyCompletion
214214
{

AsyncRx.NET/System.Reactive.Async/Concurrency/AsyncSchedulerBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected virtual async ValueTask<IAsyncDisposable> ScheduleAsyncCore(Func<Cance
6666

6767
private sealed class CancellationAsyncDisposable : IAsyncDisposable
6868
{
69-
private readonly CancellationTokenSource _cts = new CancellationTokenSource();
69+
private readonly CancellationTokenSource _cts = new();
7070

7171
public CancellationToken Token => _cts.Token;
7272

AsyncRx.NET/System.Reactive.Async/Concurrency/ImmediateAsyncScheduler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public sealed class ImmediateAsyncScheduler : AsyncSchedulerBase
1313

1414
private ImmediateAsyncScheduler() { }
1515

16-
protected override ValueTask Delay(TimeSpan dueTime, CancellationToken token) => new ValueTask(Task.Delay(dueTime));
16+
protected override ValueTask Delay(TimeSpan dueTime, CancellationToken token) => new(Task.Delay(dueTime));
1717

1818
protected override ValueTask ScheduleAsyncCore(Func<CancellationToken, ValueTask> action, CancellationToken token) => action(token);
1919
}

AsyncRx.NET/System.Reactive.Async/Concurrency/SynchronizationContextAsyncScheduler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public SynchronizationContextAsyncScheduler(SynchronizationContext context)
1616
_context = context ?? throw new ArgumentNullException(nameof(context));
1717
}
1818

19-
protected override ValueTask Delay(TimeSpan dueTime, CancellationToken token) => new ValueTask(Task.Delay(dueTime, token));
19+
protected override ValueTask Delay(TimeSpan dueTime, CancellationToken token) => new(Task.Delay(dueTime, token));
2020

2121
protected override ValueTask ScheduleAsyncCore(Func<CancellationToken, ValueTask> action, CancellationToken token)
2222
{

AsyncRx.NET/System.Reactive.Async/Concurrency/TaskPoolAsyncScheduler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public TaskPoolAsyncScheduler(TaskFactory factory)
2727
_factory = factory ?? throw new ArgumentNullException(nameof(factory));
2828
}
2929

30-
protected override ValueTask Delay(TimeSpan dueTime, CancellationToken token) => new ValueTask(Task.Delay(dueTime, token));
30+
protected override ValueTask Delay(TimeSpan dueTime, CancellationToken token) => new(Task.Delay(dueTime, token));
3131

3232
protected override ValueTask ScheduleAsyncCore(Func<CancellationToken, ValueTask> action, CancellationToken token)
3333
{

AsyncRx.NET/System.Reactive.Async/Disposables/CompositeAsyncDisposable.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace System.Reactive.Disposables
1111
{
1212
public sealed class CompositeAsyncDisposable : IAsyncDisposable
1313
{
14-
private readonly AsyncGate _gate = new AsyncGate();
14+
private readonly AsyncGate _gate = new();
1515
private readonly List<IAsyncDisposable> _disposables;
1616
private bool _disposed;
1717

AsyncRx.NET/System.Reactive.Async/Disposables/RefCountAsyncDisposable.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace System.Reactive.Disposables
99
{
1010
public sealed class RefCountAsyncDisposable : IAsyncDisposable
1111
{
12-
private readonly AsyncGate _gate = new AsyncGate();
12+
private readonly AsyncGate _gate = new();
1313
private IAsyncDisposable _disposable;
1414
private bool _primaryDisposed;
1515
private int _count;

AsyncRx.NET/System.Reactive.Async/Disposables/SerialAsyncDisposable.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace System.Reactive.Disposables
99
{
1010
public sealed class SerialAsyncDisposable : IAsyncDisposable
1111
{
12-
private readonly AsyncGate _gate = new AsyncGate();
12+
private readonly AsyncGate _gate = new();
1313

1414
private IAsyncDisposable _disposable;
1515
private bool _disposed;

AsyncRx.NET/System.Reactive.Async/Internal/AsyncQueueLockAsyncObserver.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace System.Reactive
99
{
1010
internal sealed class AsyncQueueLockAsyncObserver<T> : AsyncObserverBase<T>
1111
{
12-
private readonly AsyncQueueLock _gate = new AsyncQueueLock();
12+
private readonly AsyncQueueLock _gate = new();
1313
private readonly IAsyncObserver<T> _observer;
1414

1515
public AsyncQueueLockAsyncObserver(IAsyncObserver<T> observer)

AsyncRx.NET/System.Reactive.Async/Internal/EventPatternSourceBaseInternal.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,7 @@ protected void Remove(Delegate handler)
120120
}
121121
}
122122

123-
if (d != null)
124-
{
125-
d.DisposeAsync().GetAwaiter().GetResult();
126-
}
123+
d?.DisposeAsync().GetAwaiter().GetResult();
127124
}
128125
}
129126
}

AsyncRx.NET/System.Reactive.Async/Internal/EventSource.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,7 @@ private void Remove(Delegate handler)
9191
}
9292
}
9393

94-
if (d != null)
95-
{
96-
d.DisposeAsync().GetAwaiter().GetResult();
97-
}
94+
d?.DisposeAsync().GetAwaiter().GetResult();
9895
}
9996
}
10097
}

AsyncRx.NET/System.Reactive.Async/Internal/FastImmediateAsyncObserver.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace System.Reactive
1010
{
1111
internal sealed class FastImmediateAsyncObserver<T> : ScheduledAsyncObserverBase<T>
1212
{
13-
private readonly CancellationAsyncDisposable _disposable = new CancellationAsyncDisposable();
13+
private readonly CancellationAsyncDisposable _disposable = new();
1414

1515
public FastImmediateAsyncObserver(IAsyncObserver<T> observer)
1616
: base(observer)
@@ -19,9 +19,9 @@ public FastImmediateAsyncObserver(IAsyncObserver<T> observer)
1919

2020
public override ValueTask DisposeAsync() => _disposable.DisposeAsync();
2121

22-
protected override ValueTaskAwaitable RendezVous(ValueTask task) => new ValueTaskAwaitable(task, continueOnCapturedContext: false, scheduler: null, CancellationToken.None);
22+
protected override ValueTaskAwaitable RendezVous(ValueTask task) => new(task, continueOnCapturedContext: false, scheduler: null, CancellationToken.None);
2323

24-
protected override ValueTaskAwaitable<R> RendezVous<R>(ValueTask<R> task) => new ValueTaskAwaitable<R>(task, continueOnCapturedContext: false, scheduler: null, CancellationToken.None);
24+
protected override ValueTaskAwaitable<R> RendezVous<R>(ValueTask<R> task) => new(task, continueOnCapturedContext: false, scheduler: null, CancellationToken.None);
2525

2626
protected override ValueTask ScheduleAsync() => RunAsync(_disposable.Token);
2727
}

0 commit comments

Comments
 (0)