Skip to content

Commit 3753a6d

Browse files
committed
Prevent diagnostics on .NET SDK 8.0.100 (#2049)
Address collection expression diagnostics In most cases, we now just use the new (C# 12.0) collection expression syntax. However, we've disabled IDE0305 because it makes suggestions that aren't an obvious improvement. It wants to rewrite use of ToArray() to be a collection expression. E.g. something.ToArray() would become [.. something]. Perhaps that will seem natural once we've all got used to spreads in collection expressions, but to me (idg10) today that looks odd. Add comment explaining why we've disable IDE0290 Make UWP test runner use the same warning settings as everything else, except for CS0618, which a large number of tests appear to depend on.
1 parent 72a4a2c commit 3753a6d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+143
-148
lines changed

Rx.NET/Source/Directory.build.props

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<EnableNETAnalyzers>true</EnableNETAnalyzers>
6262

6363
<!--
64-
Disable diagnostics:
64+
Disabled diagnostics:
6565
CA1001 - types holding disposable fields should implement IDisposable. See next item.
6666
CA2213 - IDisposable types should Dispose any IDisposable fields. This rule finds over 600
6767
examples! These are all in subtle multithreaded or async code. Some of them appear
@@ -74,9 +74,21 @@
7474
7575
IDE0056 - Use of index/range syntax - relevant types not available on all targets, so we can't
7676
IDE0057 do this.
77+
78+
IDE0290 - Primary ctors. This diagnostic suggests them in a lot of places where we don't want
79+
them. E.g., in most types with multiple constructors I find I prefer not to have
80+
a primary ctor. Since this is all or nothing, we turn it off.
81+
IDE0305 - Suggests Collection expressions in place of .ToArray. E.g, wants to change this:
82+
_readyList.ToArray()
83+
to this:
84+
[.. readyList]
85+
This won't improve performance as far as we know (sometimes a reason for using that
86+
syntax), and it's not obviously an improvement in readability.
87+
88+
CA1510 - use ArgumentNullException.ThrowIf (not available on all targets)
89+
CA1513 - use ObjectDisposedException.ThrowIf (not available on all targets)
7790
-->
78-
<!-- Get diagnostics back to the level that the .NET 5.0 SDK had for now. Fix these properly before release. -->
79-
<NoWarn>$(NoWarn);CA1001;CA2213;IDE0056;IDE0057</NoWarn>
91+
<NoWarn>$(NoWarn);CA1001;CA2213;CA1510;CA1513;IDE0056;IDE0057;IDE0290;IDE0305</NoWarn>
8092
</PropertyGroup>
8193

8294
<ItemGroup>

Rx.NET/Source/src/Microsoft.Reactive.Testing/ColdObservable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal class ColdObservable<T> : ITestableObservable<T>
1313
{
1414
private readonly TestScheduler _scheduler;
1515
private readonly Recorded<Notification<T>>[] _messages;
16-
private readonly List<Subscription> _subscriptions = new();
16+
private readonly List<Subscription> _subscriptions = [];
1717

1818
public ColdObservable(TestScheduler scheduler, params Recorded<Notification<T>>[] messages)
1919
{

Rx.NET/Source/src/Microsoft.Reactive.Testing/HotObservable.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ namespace Microsoft.Reactive.Testing
1212
internal class HotObservable<T> : ITestableObservable<T>
1313
{
1414
private readonly TestScheduler _scheduler;
15-
private readonly List<IObserver<T>> _observers = new();
16-
private readonly List<Subscription> _subscriptions = new();
15+
private readonly List<IObserver<T>> _observers = [];
16+
private readonly List<Subscription> _subscriptions = [];
1717
private readonly Recorded<Notification<T>>[] _messages;
1818

1919
public HotObservable(TestScheduler scheduler, params Recorded<Notification<T>>[] messages)

Rx.NET/Source/src/Microsoft.Reactive.Testing/MockObserver.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ namespace Microsoft.Reactive.Testing
1111
internal class MockObserver<T> : ITestableObserver<T>
1212
{
1313
private readonly TestScheduler _scheduler;
14-
private readonly List<Recorded<Notification<T>>> _messages;
14+
private readonly List<Recorded<Notification<T>>> _messages = [];
1515

1616
public MockObserver(TestScheduler scheduler)
1717
{
1818
_scheduler = scheduler ?? throw new ArgumentNullException(nameof(scheduler));
19-
_messages = new List<Recorded<Notification<T>>>();
2019
}
2120

2221
public void OnNext(T value)

Rx.NET/Source/src/System.Reactive/Concurrency/LocalScheduler.TimerQueue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public partial class LocalScheduler
5555
/// Set of disposable handles to all of the current short term work Schedule calls,
5656
/// allowing those to be cancelled upon a system clock change.
5757
/// </summary>
58-
private readonly HashSet<IDisposable> _shortTermWork = new();
58+
private readonly HashSet<IDisposable> _shortTermWork = [];
5959

6060
/// <summary>
6161
/// Threshold where an item is considered to be short term work or gets moved from

Rx.NET/Source/src/System.Reactive/Concurrency/Scheduler.Recursive.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,11 @@ private abstract class InvokeRecBaseState : IDisposable
175175
{
176176
protected readonly IScheduler Scheduler;
177177

178-
protected readonly CompositeDisposable Group;
178+
protected readonly CompositeDisposable Group = [];
179179

180180
protected InvokeRecBaseState(IScheduler scheduler)
181181
{
182182
Scheduler = scheduler;
183-
Group = new CompositeDisposable();
184183
}
185184

186185
public void Dispose()

Rx.NET/Source/src/System.Reactive/Concurrency/Scheduler.Services.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ namespace System.Reactive.Concurrency
1010
//
1111
public static partial class Scheduler
1212
{
13-
internal static Type[] Optimizations = {
13+
internal static Type[] Optimizations =
14+
[
1415
typeof(ISchedulerLongRunning),
1516
typeof(IStopwatchProvider),
1617
typeof(ISchedulerPeriodic)
1718
/* update this list if new interface-based optimizations are added */
18-
};
19+
];
1920

2021
/// <summary>
2122
/// Returns the <see cref="ISchedulerLongRunning"/> implementation of the specified scheduler, or <c>null</c> if no such implementation is available.

Rx.NET/Source/src/System.Reactive/Concurrency/SchedulerWrapper.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ namespace System.Reactive.Concurrency
99
internal abstract class SchedulerWrapper : IScheduler, IServiceProvider
1010
{
1111
protected readonly IScheduler _scheduler;
12-
private readonly ConditionalWeakTable<IScheduler, IScheduler> _cache;
12+
private readonly ConditionalWeakTable<IScheduler, IScheduler> _cache =
13+
#if NET472_OR_GREATER || NETSTANDARD2_0_OR_GREATER
14+
new();
15+
#else
16+
[];
17+
#endif
1318

1419
protected SchedulerWrapper(IScheduler scheduler)
1520
{
1621
_scheduler = scheduler;
17-
_cache = new ConditionalWeakTable<IScheduler, IScheduler>();
1822
}
1923

2024
protected SchedulerWrapper(IScheduler scheduler, ConditionalWeakTable<IScheduler, IScheduler> cache)

Rx.NET/Source/src/System.Reactive/Diagnostics/CodeAnalysis/NullableAttributes.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ internal sealed class MemberNotNullAttribute : Attribute
9696
/// The field or property member that is promised to be not-null.
9797
/// </param>
9898
#pragma warning disable CA1019 // Define accessors for attribute arguments - this needs to be identical to the real type
99-
public MemberNotNullAttribute(string member) => Members = new[] { member };
99+
public MemberNotNullAttribute(string member) => Members = [member];
100100
#pragma warning restore CA1019
101101

102102
/// <summary>Initializes the attribute with the list of field and property members.</summary>
@@ -125,7 +125,7 @@ public MemberNotNullWhenAttribute(bool returnValue, string member)
125125
#pragma warning restore CA1019
126126
{
127127
ReturnValue = returnValue;
128-
Members = new[] { member };
128+
Members = [member];
129129
}
130130

131131
/// <summary>Initializes the attribute with the specified return value condition and list of field and property members.</summary>

Rx.NET/Source/src/System.Reactive/Disposables/CompositeDisposable.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public sealed class CompositeDisposable : ICollection<IDisposable>, ICancelable
2929
/// </summary>
3030
public CompositeDisposable()
3131
{
32-
_disposables = new List<IDisposable?>();
32+
_disposables = [];
3333
}
3434

3535
/// <summary>
@@ -387,7 +387,7 @@ public IEnumerator<IDisposable> GetEnumerator()
387387
/// method to avoid allocation on disposed or empty composites.
388388
/// </summary>
389389
private static readonly CompositeEnumerator EmptyEnumerator =
390-
new(Array.Empty<IDisposable?>());
390+
new([]);
391391

392392
/// <summary>
393393
/// An enumerator for an array of disposables.

Rx.NET/Source/src/System.Reactive/Disposables/SingleAssignmentDisposableValue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ public void Dispose()
5050
}
5151

5252
/// <inheritdoc/>
53-
public override bool Equals(object? obj) => false;
53+
public override readonly bool Equals(object? obj) => false;
5454

5555
/// <inheritdoc/>
56-
public override int GetHashCode() => 0;
56+
public override readonly int GetHashCode() => 0;
5757

5858
#pragma warning disable IDE0060 // (Remove unused parameter.) Required part of public API
5959
public static bool operator ==(SingleAssignmentDisposableValue left, SingleAssignmentDisposableValue right) => false;

Rx.NET/Source/src/System.Reactive/EventPattern.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ public class EventPattern<TEventArgs> : EventPattern<object, TEventArgs>
1717
/// </summary>
1818
/// <param name="sender">The sender object that raised the event.</param>
1919
/// <param name="e">The event data that was generated by the event.</param>
20-
#pragma warning disable CA2109 // (Consider making non-public.) This has long been part of the public API so we couldn't change this even if we wanted to.
2120
public EventPattern(object? sender, TEventArgs e)
22-
#pragma warning restore CA2109
2321
: base(sender, e)
2422
{
2523
}

Rx.NET/Source/src/System.Reactive/EventPatternSourceBase.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void SetResource(IDisposable resource)
7676
}
7777

7878
private readonly IObservable<EventPattern<TSender, TEventArgs>> _source;
79-
private readonly Dictionary<Delegate, Stack<IDisposable>> _subscriptions;
79+
private readonly Dictionary<Delegate, Stack<IDisposable>> _subscriptions = [];
8080
private readonly Action<Action<TSender?, TEventArgs>, /*object,*/ EventPattern<TSender, TEventArgs>> _invokeHandler;
8181

8282
/// <summary>
@@ -89,7 +89,6 @@ protected EventPatternSourceBase(IObservable<EventPattern<TSender, TEventArgs>>
8989
{
9090
_source = source ?? throw new ArgumentNullException(nameof(source));
9191
_invokeHandler = invokeHandler ?? throw new ArgumentNullException(nameof(invokeHandler));
92-
_subscriptions = new Dictionary<Delegate, Stack<IDisposable>>();
9392
}
9493

9594
/// <summary>

Rx.NET/Source/src/System.Reactive/EventSource.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ namespace System.Reactive
99
internal sealed class EventSource<T> : IEventSource<T>
1010
{
1111
private readonly IObservable<T> _source;
12-
private readonly Dictionary<Delegate, Stack<IDisposable>> _subscriptions;
12+
private readonly Dictionary<Delegate, Stack<IDisposable>> _subscriptions = [];
1313
private readonly Action<Action<T>, /*object,*/ T> _invokeHandler;
1414

1515
public EventSource(IObservable<T> source, Action<Action<T>, /*object,*/ T> invokeHandler)
1616
{
1717
_source = source;
1818
_invokeHandler = invokeHandler;
19-
_subscriptions = new Dictionary<Delegate, Stack<IDisposable>>();
2019
}
2120

2221
public event Action<T> OnNext

Rx.NET/Source/src/System.Reactive/Internal/ImmutableList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ internal sealed class ImmutableList<T>
1010

1111
private readonly T[] _data;
1212

13-
private ImmutableList() => _data = Array.Empty<T>();
13+
private ImmutableList() => _data = [];
1414

1515
public ImmutableList(T[] data) => _data = data;
1616

Rx.NET/Source/src/System.Reactive/Internal/Lookup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void Add(K key, E element)
2323
{
2424
if (!_dictionary.TryGetValue(key, out var list))
2525
{
26-
_dictionary[key] = list = new List<E>();
26+
_dictionary[key] = list = [];
2727
}
2828

2929
list.Add(element);

Rx.NET/Source/src/System.Reactive/Internal/SystemClock.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static class SystemClock
2121
{
2222
private static readonly Lazy<ISystemClock> ServiceSystemClock = new(InitializeSystemClock);
2323
private static readonly Lazy<INotifySystemClockChanged> ServiceSystemClockChanged = new(InitializeSystemClockChanged);
24-
internal static readonly HashSet<WeakReference<LocalScheduler>> SystemClockChanged = new();
24+
internal static readonly HashSet<WeakReference<LocalScheduler>> SystemClockChanged = [];
2525
private static IDisposable? _systemClockChangedHandlerCollector;
2626

2727
private static int _refCount;
@@ -123,7 +123,7 @@ private static void CollectHandlers()
123123
{
124124
if (!handler.TryGetTarget(out _))
125125
{
126-
remove ??= new HashSet<WeakReference<LocalScheduler>>();
126+
remove ??= [];
127127

128128
remove.Add(handler);
129129
}

Rx.NET/Source/src/System.Reactive/Joins/ActivePlan.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace System.Reactive.Joins
88
{
99
internal abstract class ActivePlan
1010
{
11-
private readonly Dictionary<IJoinObserver, IJoinObserver> _joinObservers = new();
11+
private readonly Dictionary<IJoinObserver, IJoinObserver> _joinObservers = [];
1212

1313
protected readonly Action _onCompleted;
1414

Rx.NET/Source/src/System.Reactive/Joins/JoinObserver.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal sealed class JoinObserver<T> : ObserverBase<Notification<T>>, IJoinObse
1919
private object? _gate;
2020
private readonly IObservable<T> _source;
2121
private readonly Action<Exception> _onError;
22-
private readonly List<ActivePlan> _activePlans;
22+
private readonly List<ActivePlan> _activePlans = [];
2323
private SingleAssignmentDisposableValue _subscription;
2424
private bool _isDisposed;
2525

@@ -28,7 +28,6 @@ public JoinObserver(IObservable<T> source, Action<Exception> onError)
2828
_source = source;
2929
_onError = onError;
3030
Queue = new Queue<Notification<T>>();
31-
_activePlans = new List<ActivePlan>();
3231
}
3332

3433
public Queue<Notification<T>> Queue { get; }

Rx.NET/Source/src/System.Reactive/Linq/Observable/Buffer.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ public TimeHopping(IObservable<TSource> source, TimeSpan timeSpan, IScheduler sc
436436
internal sealed class _ : Sink<TSource, IList<TSource>>
437437
{
438438
private readonly object _gate = new();
439-
private List<TSource> _list = new();
439+
private List<TSource> _list = [];
440440

441441
public _(IObserver<IList<TSource>> observer)
442442
: base(observer)
@@ -465,7 +465,7 @@ private void Tick()
465465
lock (_gate)
466466
{
467467
ForwardOnNext(_list);
468-
_list = new List<TSource>();
468+
_list = [];
469469
}
470470
}
471471

@@ -521,7 +521,7 @@ internal sealed class _ : Sink<TSource, IList<TSource>>
521521
{
522522
private readonly Ferry _parent;
523523
private readonly object _gate = new();
524-
private List<TSource> _s = new();
524+
private List<TSource> _s = [];
525525

526526
public _(Ferry parent, IObserver<IList<TSource>> observer)
527527
: base(observer)
@@ -574,7 +574,7 @@ private void Tick(int id)
574574
var newId = ++_windowId;
575575

576576
var res = _s;
577-
_s = new List<TSource>();
577+
_s = [];
578578
ForwardOnNext(res);
579579

580580
CreateTimer(newId);
@@ -598,7 +598,7 @@ public override void OnNext(TSource value)
598598
newId = ++_windowId;
599599

600600
var res = _s;
601-
_s = new List<TSource>();
601+
_s = [];
602602
ForwardOnNext(res);
603603
}
604604

@@ -653,7 +653,7 @@ internal sealed class _ : Sink<TSource, IList<TSource>>
653653
private readonly AsyncLock _bufferGate = new();
654654
private readonly Func<IObservable<TBufferClosing>> _bufferClosingSelector;
655655

656-
private List<TSource> _buffer = new();
656+
private List<TSource> _buffer = [];
657657
private SerialDisposableValue _bufferClosingSerialDisposable;
658658

659659
public _(Selector parent, IObserver<IList<TSource>> observer)
@@ -706,7 +706,7 @@ private void CloseBuffer(IDisposable closingSubscription)
706706
lock (_gate)
707707
{
708708
var res = _buffer;
709-
_buffer = new List<TSource>();
709+
_buffer = [];
710710
ForwardOnNext(res);
711711
}
712712

@@ -785,7 +785,7 @@ internal sealed class _ : Sink<TSource, IList<TSource>>
785785
{
786786
private readonly object _gate = new();
787787

788-
private List<TSource> _buffer = new();
788+
private List<TSource> _buffer = [];
789789
private SingleAssignmentDisposableValue _boundariesDisposable;
790790

791791
public _(IObserver<IList<TSource>> observer)
@@ -823,7 +823,7 @@ public void OnNext(TBufferClosing value)
823823
lock (_parent._gate)
824824
{
825825
var res = _parent._buffer;
826-
_parent._buffer = new List<TSource>();
826+
_parent._buffer = [];
827827
_parent.ForwardOnNext(res);
828828
}
829829
}

Rx.NET/Source/src/System.Reactive/Linq/Observable/Delay.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ protected Base(IObservable<TSource> source)
597597

598598
internal abstract class _ : IdentitySink<TSource>
599599
{
600-
private readonly CompositeDisposable _delays = new();
600+
private readonly CompositeDisposable _delays = [];
601601
private readonly object _gate = new();
602602

603603
private readonly Func<TSource, IObservable<TDelay>> _delaySelector;

Rx.NET/Source/src/System.Reactive/Linq/Observable/FromEventPattern.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private Action AddHandlerCore(Delegate handler)
142142
private Action AddHandlerCoreWinRT(Delegate handler)
143143
{
144144
var token = _addMethod.Invoke(_target, new object[] { handler });
145-
return () => _removeMethod.Invoke(_target, new[] { token });
145+
return () => _removeMethod.Invoke(_target, [token]);
146146
}
147147
}
148148
}

Rx.NET/Source/src/System.Reactive/Linq/Observable/GroupByUntil.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ internal sealed class _ : Sink<TSource, IGroupedObservable<TKey, TElement>>
3535
{
3636
private readonly object _gate = new();
3737
private readonly object _nullGate = new();
38-
private readonly CompositeDisposable _groupDisposable = new();
38+
private readonly CompositeDisposable _groupDisposable = [];
3939
private readonly RefCountDisposable _refCountDisposable;
4040
private readonly Map<TKey, ISubject<TElement>> _map;
4141

0 commit comments

Comments
 (0)