Skip to content

Commit cc9023e

Browse files
committed
Code style: use c# 12 collection literals
1 parent 4a5e6b6 commit cc9023e

33 files changed

+113
-115
lines changed

.editorconfig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ dotnet_style_qualification_for_field = false:suggestion
1717
dotnet_style_qualification_for_property = false:suggestion
1818
dotnet_style_qualification_for_method = false:suggestion
1919
dotnet_style_qualification_for_event = false:suggestion
20-
csharp_style_namespace_declarations=file_scoped:warning
20+
csharp_style_namespace_declarations = file_scoped:warning
21+
dotnet_style_prefer_collection_expression = true:warning
22+
dotnet_style_collection_initializer = true:warning
2123

2224
# ReSharper properties
2325
resharper_int_align_switch_expressions = true

src/NSubstitute/Compatibility/DiagnosticsNullabilityAttributes.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ internal sealed class MemberNotNullAttribute : Attribute
9090
/// <param name="member">
9191
/// The field or property member that is promised to be not-null.
9292
/// </param>
93-
public MemberNotNullAttribute(string member) => Members = new[] { member };
93+
public MemberNotNullAttribute(string member) => Members = [member];
9494

9595
/// <summary>Initializes the attribute with the list of field and property members.</summary>
9696
/// <param name="members">
@@ -116,7 +116,7 @@ internal sealed class MemberNotNullWhenAttribute : Attribute
116116
public MemberNotNullWhenAttribute(bool returnValue, string member)
117117
{
118118
ReturnValue = returnValue;
119-
Members = new[] { member };
119+
Members = [member];
120120
}
121121

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

src/NSubstitute/Core/ArgumentSpecificationDequeue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace NSubstitute.Core;
55

66
public class ArgumentSpecificationDequeue : IArgumentSpecificationDequeue
77
{
8-
private static readonly IArgumentSpecification[] EmptySpecifications = new IArgumentSpecification[0];
8+
private static readonly IArgumentSpecification[] EmptySpecifications = [];
99

1010
private readonly Func<IList<IArgumentSpecification>> _dequeueAllQueuedArgSpecs;
1111

src/NSubstitute/Core/Arguments/ArrayContentsArgumentMatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public bool IsSatisfiedBy(object? argument)
3131

3232
public string Format(object? argument, bool highlight)
3333
{
34-
var argArray = argument is IEnumerable enumerableArgs ? enumerableArgs.Cast<object>().ToArray() : new object[0];
34+
var argArray = argument is IEnumerable enumerableArgs ? enumerableArgs.Cast<object>().ToArray() : [];
3535
return Format(argArray, _argumentSpecifications).Join(", ");
3636
}
3737

src/NSubstitute/Core/Call.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void AssignSequenceNumber(long number)
8181
object?[] originalArray = _originalArguments;
8282
if (originalArray == _arguments && originalArray.Length > 0)
8383
{
84-
object?[] copy = originalArray.ToArray();
84+
object?[] copy = [.. originalArray];
8585
// If it happens that _originalArguments doesn't point to the `_arguments` anymore -
8686
// it might happen that other thread already created a copy and mutated the original `_arguments` array.
8787
// In this case it's unsafe to replace it with a copy.

src/NSubstitute/Core/CustomHandlers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class CustomHandlers : ICustomHandlers
44
{
5-
private readonly List<ICallHandler> _handlers = new();
5+
private readonly List<ICallHandler> _handlers = [];
66
private readonly ISubstituteState _substituteState;
77

88
public IReadOnlyCollection<ICallHandler> Handlers => _handlers;

src/NSubstitute/Core/DependencyInjection/NSubContainer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class NSubContainer : IConfigurableNSubContainer
1717
{
1818
private readonly NSubContainer? _parentContainer;
1919
private readonly object _syncRoot;
20-
private readonly Dictionary<Type, Registration> _registrations = new();
20+
private readonly Dictionary<Type, Registration> _registrations = [];
2121

2222
public NSubContainer()
2323
{
@@ -177,7 +177,7 @@ public object Resolve(Scope scope)
177177

178178
private class Scope : INSubResolver
179179
{
180-
private readonly Dictionary<Registration, object> _cache = new Dictionary<Registration, object>();
180+
private readonly Dictionary<Registration, object> _cache = [];
181181
private readonly NSubContainer _mostNestedContainer;
182182

183183
public Scope(NSubContainer mostNestedContainer)

src/NSubstitute/Core/EventHandlerRegistry.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class EventHandlerRegistry : IEventHandlerRegistry
66
// Events are not expected to be configured/raised concurrently, so simple locking should be sufficient.
77
// List lookup is O(n), but for really small size performance is comparable to dictionary.
88
// Given that normally a few events are configured only, it should be totally fine.
9-
private readonly List<Tuple<string, List<object>>> _handlersForEvent = new();
9+
private readonly List<Tuple<string, List<object>>> _handlersForEvent = [];
1010

1111
public void Add(string eventName, object handler)
1212
{

src/NSubstitute/Core/Events/DelegateEventWrapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private bool LooksLikeAnEventStyleCall(ParameterInfo[] parameters)
6464
sender = _providedArguments[0];
6565
eventArgs = GetDefaultForEventArgType(eventArgsType);
6666
}
67-
return new[] { sender, eventArgs };
67+
return [sender, eventArgs];
6868
}
6969

7070
private static bool RequiredArgsHaveBeenProvided(object?[] providedArgs, ParameterInfo[] requiredArgs)

src/NSubstitute/Core/Events/EventHandlerWrapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ protected override object[] WorkOutRequiredArguments(ICall call)
3535
{
3636
var sender = _sender ?? call.Target();
3737
var eventArgs = _eventArgs ?? GetDefaultForEventArgType(typeof(TEventArgs));
38-
return new[] { sender, eventArgs };
38+
return [sender, eventArgs];
3939
}
4040
}

0 commit comments

Comments
 (0)