Skip to content

Commit

Permalink
Resolve SDK 8.0.2xx diagnostics (#2100)
Browse files Browse the repository at this point in the history
It looks like the 8.0.200 .NET SDK added a lot of new diagnostics suggesting the use of collection expressions.

For the most part we have taken these suggestions, but in some cases we've retained the existing code so you can see what types are being used. I find that in methods with lots of overloads (e.g., Assert.AreEqual) it becomes quite difficult to work out what will actually happen if you replace an explicitly typed list initializer with just `[...]`.
  • Loading branch information
idg10 authored May 1, 2024
1 parent 8da262d commit 044ff7c
Show file tree
Hide file tree
Showing 56 changed files with 466 additions and 458 deletions.
2 changes: 1 addition & 1 deletion Ix.NET/Integration/Android/Resources/Resource.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Description>Reactive Extensions (Rx) for .NET - Testing Library</Description>
<PackageReadmeFile>readme.md</PackageReadmeFile>
<!-- NB: A lot of CA and IDE warnings are disabled because of the .cs files included from xunit.assert.source. -->
<NoWarn>$(NoWarn);IDE0054;IDE0066;CA1305;CA1307;CA1032;CA1064;CA1704;CA1822;CA1812;CA1820;CA1823;CA1825;CA1845;CA2249;IDE0016;IDE0018;IDE0019;IDE0020;IDE0031;IDE0039;IDE0044;IDE0059;IDE0074;IDE0270</NoWarn>
<NoWarn>$(NoWarn);IDE0054;IDE0066;CA1305;CA1307;CA1032;CA1064;CA1704;CA1822;CA1812;CA1820;CA1823;CA1825;CA1845;CA2249;IDE0016;IDE0018;IDE0019;IDE0020;IDE0028;IDE0031;IDE0039;IDE0044;IDE0059;IDE0074;IDE0090;IDE0270;IDE0300</NoWarn>
</PropertyGroup>

<PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion Rx.NET/Source/src/System.Reactive/Internal/Lookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public IEnumerable<E> this[K key]
{
if (!_dictionary.TryGetValue(key, out var list))
{
return Enumerable.Empty<E>();
return [];
}

return Hide(list);
Expand Down
8 changes: 4 additions & 4 deletions Rx.NET/Source/src/System.Reactive/Linq/Observable/Buffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal sealed class ExactSink : Sink<TSource, IList<TSource>>
{
private readonly int _count;
private int _index;
private IList<TSource>? _buffer;
private List<TSource>? _buffer;

internal ExactSink(IObserver<IList<TSource>> observer, int count) : base(observer)
{
Expand All @@ -41,7 +41,7 @@ public override void OnNext(TSource value)
var buffer = _buffer;
if (buffer == null)
{
buffer = new List<TSource>();
buffer = [];
_buffer = buffer;
}

Expand Down Expand Up @@ -102,7 +102,7 @@ internal sealed class SkipSink : Sink<TSource, IList<TSource>>
private readonly int _count;
private readonly int _skip;
private int _index;
private IList<TSource>? _buffer;
private List<TSource>? _buffer;

internal SkipSink(IObserver<IList<TSource>> observer, int count, int skip) : base(observer)
{
Expand All @@ -116,7 +116,7 @@ public override void OnNext(TSource value)
var buffer = _buffer;
if (idx == 0)
{
buffer = new List<TSource>();
buffer = [];
_buffer = buffer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ public void Dispose()

private Action AddHandlerCore(Delegate handler)
{
_addMethod.Invoke(_target, new object[] { handler });
return () => _removeMethod.Invoke(_target, new object[] { handler });
_addMethod.Invoke(_target, [handler]);
return () => _removeMethod.Invoke(_target, [handler]);
}

private Action AddHandlerCoreWinRT(Delegate handler)
{
var token = _addMethod.Invoke(_target, new object[] { handler });
var token = _addMethod.Invoke(_target, [handler]);
return () => _removeMethod.Invoke(_target, [token]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal partial class QueryLanguage

public virtual IEnumerable<IList<TSource>> Chunkify<TSource>(IObservable<TSource> source)
{
return source.Collect<TSource, IList<TSource>>(() => new List<TSource>(), (lst, x) => { lst.Add(x); return lst; }, _ => new List<TSource>());
return source.Collect<TSource, IList<TSource>>(() => [], (lst, x) => { lst.Add(x); return lst; }, _ => []);
}

#endregion
Expand Down
6 changes: 3 additions & 3 deletions Rx.NET/Source/src/System.Reactive/ObservableQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,13 @@ private IList<Expression> VisitQbservableOperatorArguments(MethodInfo method, IE
if (lastArgument.NodeType == ExpressionType.NewArrayInit)
{
var paramsArray = (NewArrayExpression)lastArgument;
return new List<Expression>
{
return
[
Expression.NewArrayInit(
typeof(Plan<>).MakeGenericType(method.GetGenericArguments()[0]),
paramsArray.Expressions.Select(param => Visit(param))
)
};
];
}
}

Expand Down
4 changes: 2 additions & 2 deletions Rx.NET/Source/src/System.Reactive/Subjects/AsyncSubject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public sealed class AsyncSubject<T> : SubjectBase<T>, INotifyCompletion
private bool _hasValue;
private Exception? _exception;

#pragma warning disable CA1825 // (Avoid zero-length array allocations.) The identity of these arrays matters, so we can't use the shared Array.Empty<T>() instance
#pragma warning disable CA1825,IDE0300 // (Avoid zero-length array allocations. Use collection expressions) The identity of these arrays matters, so we can't use the shared Array.Empty<T>() instance either explicitly, or indirectly via a collection expression
/// <summary>
/// A pre-allocated empty array indicating the AsyncSubject has terminated.
/// </summary>
Expand All @@ -31,7 +31,7 @@ public sealed class AsyncSubject<T> : SubjectBase<T>, INotifyCompletion
/// A pre-allocated empty array indicating the AsyncSubject has been disposed.
/// </summary>
private static readonly AsyncSubjectDisposable[] Disposed = new AsyncSubjectDisposable[0];
#pragma warning restore CA1825
#pragma warning restore CA1825,IDE0300

#endregion

Expand Down
4 changes: 2 additions & 2 deletions Rx.NET/Source/src/System.Reactive/Subjects/Subject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public sealed class Subject<T> : SubjectBase<T>

private SubjectDisposable[] _observers;
private Exception? _exception;
#pragma warning disable CA1825 // (Avoid zero-length array allocations.) The identity of these arrays matters, so we can't use the shared Array.Empty<T>() instance
#pragma warning disable CA1825,IDE0300 // (Avoid zero-length array allocations. Use collection expressions) The identity of these arrays matters, so we can't use the shared Array.Empty<T>() instance either explicitly, or indirectly via a collection expression
private static readonly SubjectDisposable[] Terminated = new SubjectDisposable[0];
private static readonly SubjectDisposable[] Disposed = new SubjectDisposable[0];
#pragma warning restore CA1825
#pragma warning restore CA1825,IDE0300

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private string GeneratePublicApi(Assembly assembly)
{
ApiGeneratorOptions options = new()
{
AllowNamespacePrefixes = new[] { "System", "Microsoft" }
AllowNamespacePrefixes = ["System", "Microsoft"]
};
return Filter(ApiGenerator.GeneratePublicApi(assembly, options));
}
Expand Down
8 changes: 4 additions & 4 deletions Rx.NET/Source/tests/Tests.System.Reactive/Tests/Aliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public void Qbservable_Aliases()
{
var xs = Observable.Return(1).AsQbservable();

Assert.True(xs.Filter(x => true).ToEnumerable().SequenceEqual(new[] { 1 }), "Filter");
Assert.True(xs.Filter(x => true).Concat(xs.Filter(x => false)).ToEnumerable().SequenceEqual(new[] { 1 }), "Concat/Filter");
Assert.True(xs.Map(x => x.ToString()).ToEnumerable().SequenceEqual(new[] { "1" }), "Map");
Assert.True(xs.FlatMap(x => xs).ToEnumerable().SequenceEqual(new[] { 1 }), "FlatMap");
Assert.True(xs.Filter(x => true).ToEnumerable().SequenceEqual([1]), "Filter");
Assert.True(xs.Filter(x => true).Concat(xs.Filter(x => false)).ToEnumerable().SequenceEqual([1]), "Concat/Filter");
Assert.True(xs.Map(x => x.ToString()).ToEnumerable().SequenceEqual(["1"]), "Map");
Assert.True(xs.FlatMap(x => xs).ToEnumerable().SequenceEqual([1]), "FlatMap");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class ArgumentValidationTest
/// </summary>
static ArgumentValidationTest()
{
#pragma warning disable IDE0300 // Simplify collection initialization. We want to be clear about what kinds of collections are in use in these tests.
_defaultValues = new Dictionary<string, object>
{
{ "IObservable`1[Object]", Observable.Return(new object()) },
Expand Down Expand Up @@ -269,6 +270,7 @@ static ArgumentValidationTest()

{ "Func`17[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, AsyncCallback, Object, IAsyncResult]", new Func<int, int, int, int, int, int, int, int, int, int, int, int, int, int, AsyncCallback, object, IAsyncResult>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16) => null) }
};
#pragma warning restore IDE0300 // Simplify collection initialization
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ public void OnResuming()
public void DisableOptimizations_ArgumentChecking()
{
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.DisableOptimizations(default));
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.DisableOptimizations(default, new Type[0]));
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.DisableOptimizations(default, []));
#if !WINDOWS
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.DisableOptimizations(ThreadPoolScheduler.Instance, default));
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public void CompositeDisposable_ToArray()
var g = new CompositeDisposable(d1, d2);
Assert.Equal(2, g.Count);
var x = Enumerable.ToArray(g);
Assert.True(g.ToArray().SequenceEqual(new[] { d1, d2 }));
Assert.True(g.ToArray().SequenceEqual([d1, d2]));
}

[TestMethod]
Expand All @@ -211,7 +211,7 @@ public void CompositeDisposable_GetEnumerator()
lst.Add(x);
}

Assert.True(lst.SequenceEqual(new[] { d1, d2 }));
Assert.True(lst.SequenceEqual([d1, d2]));
}

[TestMethod]
Expand All @@ -226,7 +226,7 @@ public void CompositeDisposable_GetEnumeratorNonGeneric()
lst.Add(x);
}

Assert.True(lst.SequenceEqual(new[] { d1, d2 }));
Assert.True(lst.SequenceEqual([d1, d2]));
}

[TestMethod]
Expand All @@ -252,7 +252,7 @@ public void CompositeDisposable_AddNull_via_params_ctor()
[TestMethod]
public void CompositeDisposable_AddNull_via_IEnum_ctor()
{
IEnumerable<IDisposable> values = new IDisposable[] { null };
IEnumerable<IDisposable> values = [null];
#pragma warning disable CA1806 // (Unused new instance.) We expect the constructor to throw.
ReactiveAssert.Throws<ArgumentException>(() => new CompositeDisposable(values));
#pragma warning restore CA1806
Expand Down Expand Up @@ -882,7 +882,7 @@ public void StableCompositeDisposable_Nary2()
var disp3 = false;
var d3 = Disposable.Create(() => { Assert.False(disp3); disp3 = true; });

var d = StableCompositeDisposable.Create(new List<IDisposable>(new[] { d1, d2, d3 }));
var d = StableCompositeDisposable.Create(new List<IDisposable>([d1, d2, d3]));

Assert.False(disp1);
Assert.False(disp2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,53 +21,53 @@ public void ImmutableList_Basics()
{
var list = ImmutableList<int>.Empty;

Assert.True(list.Data.SequenceEqual(new int[] { }));
Assert.True(list.Data.SequenceEqual([]));

list = list.Add(42);

Assert.True(list.Data.SequenceEqual(new int[] { 42 }));
Assert.True(list.Data.SequenceEqual([42]));

list = list.Remove(42);

Assert.True(list.Data.SequenceEqual(new int[] { }));
Assert.True(list.Data.SequenceEqual([]));

list = list.Remove(42);

Assert.True(list.Data.SequenceEqual(new int[] { }));
Assert.True(list.Data.SequenceEqual([]));

list = list.Add(43);
list = list.Add(44);
list = list.Add(43);

Assert.True(list.Data.SequenceEqual(new int[] { 43, 44, 43 }));
Assert.True(list.Data.SequenceEqual([43, 44, 43]));

list = list.Remove(43);

Assert.True(list.Data.SequenceEqual(new int[] { 44, 43 }));
Assert.True(list.Data.SequenceEqual([44, 43]));

list = list.Remove(43);

Assert.True(list.Data.SequenceEqual(new int[] { 44 }));
Assert.True(list.Data.SequenceEqual([44]));

list = list.Remove(44);

Assert.True(list.Data.SequenceEqual(new int[] { }));
Assert.True(list.Data.SequenceEqual([]));
}

[TestMethod]
public void ImmutableList_Nulls()
{
var list = ImmutableList<string>.Empty;

Assert.True(list.Data.SequenceEqual(new string[] { }));
Assert.True(list.Data.SequenceEqual([]));

list = list.Add(null);

Assert.True(list.Data.SequenceEqual(new string[] { null }));
Assert.True(list.Data.SequenceEqual([null]));

list = list.Remove(null);

Assert.True(list.Data.SequenceEqual(new string[] { }));
Assert.True(list.Data.SequenceEqual([]));
}
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

using Assert = Xunit.Assert;

#pragma warning disable IDE0028 // Simplify collection initialization. Using this in Assert.Equals makes it unclear what types are actually in use.

namespace ReactiveTests.Tests
{
[TestClass]
Expand Down
Loading

0 comments on commit 044ff7c

Please sign in to comment.