Skip to content

Commit

Permalink
Use collection initializers
Browse files Browse the repository at this point in the history
- Use C#12 collection initializers where relevant.
- Suppress false positives when using `TheoryData`.
- Remove some redundant code analysis suppressions.
  • Loading branch information
martincostello committed Oct 30, 2023
1 parent 0cc2ff9 commit d92e8e8
Show file tree
Hide file tree
Showing 46 changed files with 112 additions and 88 deletions.
4 changes: 2 additions & 2 deletions src/Polly.Core/CircuitBreaker/CircuitBreakerManualControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace Polly.CircuitBreaker;
public sealed class CircuitBreakerManualControl
{
private readonly object _lock = new();
private readonly HashSet<Func<ResilienceContext, Task>> _onIsolate = new();
private readonly HashSet<Func<ResilienceContext, Task>> _onReset = new();
private readonly HashSet<Func<ResilienceContext, Task>> _onIsolate = [];
private readonly HashSet<Func<ResilienceContext, Task>> _onReset = [];
private bool _isolated;

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Polly.Core/Hedging/Controller/HedgingExecutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ internal sealed class HedgingExecutionContext<T> : IAsyncDisposable
{
public readonly record struct ExecutionInfo<TResult>(TaskExecution<T>? Execution, bool Loaded, Outcome<TResult>? Outcome);

private readonly List<TaskExecution<T>> _tasks = new();
private readonly List<TaskExecution<T>> _executingTasks = new();
private readonly List<TaskExecution<T>> _tasks = [];
private readonly List<TaskExecution<T>> _executingTasks = [];
private readonly ObjectPool<TaskExecution<T>> _executionPool;
private readonly TimeProvider _timeProvider;
private readonly int _maxAttempts;
Expand Down
2 changes: 1 addition & 1 deletion src/Polly.Core/PredicateBuilder.TResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Polly;
/// <typeparam name="TResult">The type of the result.</typeparam>
public partial class PredicateBuilder<TResult>
{
private readonly List<Predicate<Outcome<TResult>>> _predicates = new();
private readonly List<Predicate<Outcome<TResult>>> _predicates = [];

/// <summary>
/// Adds a predicate for handling exceptions of the specified type.
Expand Down
4 changes: 2 additions & 2 deletions src/Polly.Core/Registry/ConfigureBuilderContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ internal ConfigureBuilderContext(TKey strategyKey, string builderName, string? b
/// </summary>
internal string? BuilderInstanceName { get; }

internal List<CancellationToken> ReloadTokens { get; } = new();
internal List<CancellationToken> ReloadTokens { get; } = [];

internal List<Action> DisposeCallbacks { get; } = new();
internal List<Action> DisposeCallbacks { get; } = [];

/// <summary>
/// Reloads the pipeline when <paramref name="cancellationToken"/> is canceled.
Expand Down
3 changes: 1 addition & 2 deletions src/Polly.Core/ResiliencePipelineBuilderBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.ComponentModel;

using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using Polly.Telemetry;
Expand All @@ -19,7 +18,7 @@ namespace Polly;
public abstract class ResiliencePipelineBuilderBase
#pragma warning restore S1694 // An abstract class should have both abstract and concrete methods
{
private readonly List<Entry> _entries = new();
private readonly List<Entry> _entries = [];
private bool _used;

private protected ResiliencePipelineBuilderBase()
Expand Down
4 changes: 2 additions & 2 deletions src/Polly.Core/Utils/Pipeline/ReloadableComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private void TryRegisterOnReload()
return;
}

_tokenSource = CancellationTokenSource.CreateLinkedTokenSource(_reloadTokens.ToArray());
_tokenSource = CancellationTokenSource.CreateLinkedTokenSource([.. _reloadTokens]);
_registration = _tokenSource.Token.Register(() =>
{
var context = ResilienceContextPool.Shared.Get().Initialize<VoidResult>(isSynchronous: true);
Expand All @@ -65,7 +65,7 @@ private void TryRegisterOnReload()
}
catch (Exception e)
{
_reloadTokens = new List<CancellationToken>();
_reloadTokens = [];
_telemetry.Report(new(ResilienceEventSeverity.Error, ReloadFailedEvent), context, Outcome.FromException(e), new ReloadFailedArguments(e));
ResilienceContextPool.Shared.Return(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace Polly.DependencyInjection;
internal sealed class ConfigureResiliencePipelineRegistryOptions<TKey>
where TKey : notnull
{
public List<Action<ResiliencePipelineRegistry<TKey>>> Actions { get; } = new();
public List<Action<ResiliencePipelineRegistry<TKey>>> Actions { get; } = [];
}
24 changes: 12 additions & 12 deletions src/Polly/AsyncPolicy.ExecuteOverloads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public abstract partial class AsyncPolicy : PolicyBase, IAsyncPolicy
/// <param name="action">The action to perform.</param>
[DebuggerStepThrough]
public Task ExecuteAsync(Func<Task> action) =>
ExecuteAsync((_, _) => action(), new Context(), DefaultCancellationToken, DefaultContinueOnCapturedContext);
ExecuteAsync((_, _) => action(), [], DefaultCancellationToken, DefaultContinueOnCapturedContext);

/// <summary>
/// Executes the specified asynchronous action within the policy.
Expand All @@ -37,7 +37,7 @@ public Task ExecuteAsync(Func<Context, Task> action, Context context) =>
/// <param name="cancellationToken">A cancellation token which can be used to cancel the action. When a retry policy in use, also cancels any further retries.</param>
[DebuggerStepThrough]
public Task ExecuteAsync(Func<CancellationToken, Task> action, CancellationToken cancellationToken) =>
ExecuteAsync((_, ct) => action(ct), new Context(), cancellationToken, DefaultContinueOnCapturedContext);
ExecuteAsync((_, ct) => action(ct), [], cancellationToken, DefaultContinueOnCapturedContext);

/// <summary>
/// Executes the specified asynchronous action within the policy.
Expand Down Expand Up @@ -68,7 +68,7 @@ public Task ExecuteAsync(Func<Context, CancellationToken, Task> action, Context
/// <exception cref="InvalidOperationException">Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods.</exception>
[DebuggerStepThrough]
public Task ExecuteAsync(Func<CancellationToken, Task> action, CancellationToken cancellationToken, bool continueOnCapturedContext) =>
ExecuteAsync((_, ct) => action(ct), new Context(), cancellationToken, continueOnCapturedContext);
ExecuteAsync((_, ct) => action(ct), [], cancellationToken, continueOnCapturedContext);

/// <summary>
/// Executes the specified asynchronous action within the policy.
Expand Down Expand Up @@ -117,7 +117,7 @@ public async Task ExecuteAsync(Func<Context, CancellationToken, Task> action, Co
/// <returns>The value returned by the action</returns>
[DebuggerStepThrough]
public Task<TResult> ExecuteAsync<TResult>(Func<Task<TResult>> action) =>
ExecuteAsync((_, _) => action(), new Context(), DefaultCancellationToken, DefaultContinueOnCapturedContext);
ExecuteAsync((_, _) => action(), [], DefaultCancellationToken, DefaultContinueOnCapturedContext);

/// <summary>
/// Executes the specified asynchronous action within the policy and returns the result.
Expand Down Expand Up @@ -149,7 +149,7 @@ public Task<TResult> ExecuteAsync<TResult>(Func<Context, Task<TResult>> action,
/// <returns>The value returned by the action</returns>
[DebuggerStepThrough]
public Task<TResult> ExecuteAsync<TResult>(Func<CancellationToken, Task<TResult>> action, CancellationToken cancellationToken) =>
ExecuteAsync((_, ct) => action(ct), new Context(), cancellationToken, DefaultContinueOnCapturedContext);
ExecuteAsync((_, ct) => action(ct), [], cancellationToken, DefaultContinueOnCapturedContext);

/// <summary>
/// Executes the specified asynchronous action within the policy and returns the result.
Expand Down Expand Up @@ -185,7 +185,7 @@ public Task<TResult> ExecuteAsync<TResult>(Func<Context, CancellationToken, Task
/// <exception cref="InvalidOperationException">Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods.</exception>
[DebuggerStepThrough]
public Task<TResult> ExecuteAsync<TResult>(Func<CancellationToken, Task<TResult>> action, CancellationToken cancellationToken, bool continueOnCapturedContext) =>
ExecuteAsync((_, ct) => action(ct), new Context(), cancellationToken, continueOnCapturedContext);
ExecuteAsync((_, ct) => action(ct), [], cancellationToken, continueOnCapturedContext);

/// <summary>
/// Executes the specified asynchronous action within the policy and returns the result.
Expand Down Expand Up @@ -240,7 +240,7 @@ public async Task<TResult> ExecuteAsync<TResult>(Func<Context, CancellationToken
/// <returns>The captured result</returns>
[DebuggerStepThrough]
public Task<PolicyResult> ExecuteAndCaptureAsync(Func<Task> action) =>
ExecuteAndCaptureAsync((_, _) => action(), new Context(), DefaultCancellationToken, DefaultContinueOnCapturedContext);
ExecuteAndCaptureAsync((_, _) => action(), [], DefaultCancellationToken, DefaultContinueOnCapturedContext);

/// <summary>
/// Executes the specified asynchronous action within the policy and returns the captured result.
Expand Down Expand Up @@ -270,7 +270,7 @@ public Task<PolicyResult> ExecuteAndCaptureAsync(Func<Context, Task> action, Con
/// <param name="cancellationToken">A cancellation token which can be used to cancel the action. When a retry policy in use, also cancels any further retries.</param>
[DebuggerStepThrough]
public Task<PolicyResult> ExecuteAndCaptureAsync(Func<CancellationToken, Task> action, CancellationToken cancellationToken) =>
ExecuteAndCaptureAsync((_, ct) => action(ct), new Context(), cancellationToken, DefaultContinueOnCapturedContext);
ExecuteAndCaptureAsync((_, ct) => action(ct), [], cancellationToken, DefaultContinueOnCapturedContext);

/// <summary>
/// Executes the specified asynchronous action within the policy and returns the captured result.
Expand Down Expand Up @@ -303,7 +303,7 @@ public Task<PolicyResult> ExecuteAndCaptureAsync(Func<Context, CancellationToken
/// <exception cref="InvalidOperationException">Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods.</exception>
[DebuggerStepThrough]
public Task<PolicyResult> ExecuteAndCaptureAsync(Func<CancellationToken, Task> action, CancellationToken cancellationToken, bool continueOnCapturedContext) =>
ExecuteAndCaptureAsync((_, ct) => action(ct), new Context(), cancellationToken, continueOnCapturedContext);
ExecuteAndCaptureAsync((_, ct) => action(ct), [], cancellationToken, continueOnCapturedContext);

/// <summary>
/// Executes the specified asynchronous action within the policy and returns the captured result.
Expand Down Expand Up @@ -352,7 +352,7 @@ public async Task<PolicyResult> ExecuteAndCaptureAsync(Func<Context, Cancellatio
/// <returns>The captured result</returns>
[DebuggerStepThrough]
public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<Task<TResult>> action) =>
ExecuteAndCaptureAsync((_, _) => action(), new Context(), DefaultCancellationToken, DefaultContinueOnCapturedContext);
ExecuteAndCaptureAsync((_, _) => action(), [], DefaultCancellationToken, DefaultContinueOnCapturedContext);

/// <summary>
/// Executes the specified asynchronous action within the policy and returns the result.
Expand Down Expand Up @@ -386,7 +386,7 @@ public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<Context,
/// <returns>The captured result</returns>
[DebuggerStepThrough]
public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<CancellationToken, Task<TResult>> action, CancellationToken cancellationToken) =>
ExecuteAndCaptureAsync((_, ct) => action(ct), new Context(), cancellationToken, DefaultContinueOnCapturedContext);
ExecuteAndCaptureAsync((_, ct) => action(ct), [], cancellationToken, DefaultContinueOnCapturedContext);

/// <summary>
/// Executes the specified asynchronous action within the policy and returns the result.
Expand Down Expand Up @@ -424,7 +424,7 @@ public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<Context,
/// <exception cref="InvalidOperationException">Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods.</exception>
[DebuggerStepThrough]
public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<CancellationToken, Task<TResult>> action, CancellationToken cancellationToken, bool continueOnCapturedContext) =>
ExecuteAndCaptureAsync((_, ct) => action(ct), new Context(), cancellationToken, continueOnCapturedContext);
ExecuteAndCaptureAsync((_, ct) => action(ct), [], cancellationToken, continueOnCapturedContext);

/// <summary>
/// Executes the specified asynchronous action within the policy and returns the result.
Expand Down
12 changes: 6 additions & 6 deletions src/Polly/AsyncPolicy.TResult.ExecuteOverloads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract partial class AsyncPolicy<TResult> : IAsyncPolicy<TResult>
/// <returns>The value returned by the action</returns>
[DebuggerStepThrough]
public Task<TResult> ExecuteAsync(Func<Task<TResult>> action) =>
ExecuteAsync((_, _) => action(), new Context(), CancellationToken.None, DefaultContinueOnCapturedContext);
ExecuteAsync((_, _) => action(), [], CancellationToken.None, DefaultContinueOnCapturedContext);

/// <summary>
/// Executes the specified asynchronous action within the policy and returns the result.
Expand Down Expand Up @@ -41,7 +41,7 @@ public Task<TResult> ExecuteAsync(Func<Context, Task<TResult>> action, Context c
/// <returns>The value returned by the action</returns>
[DebuggerStepThrough]
public Task<TResult> ExecuteAsync(Func<CancellationToken, Task<TResult>> action, CancellationToken cancellationToken) =>
ExecuteAsync((_, ct) => action(ct), new Context(), cancellationToken, DefaultContinueOnCapturedContext);
ExecuteAsync((_, ct) => action(ct), [], cancellationToken, DefaultContinueOnCapturedContext);

/// <summary>
/// Executes the specified asynchronous action within the policy and returns the result.
Expand All @@ -53,7 +53,7 @@ public Task<TResult> ExecuteAsync(Func<CancellationToken, Task<TResult>> action,
/// <exception cref="InvalidOperationException">Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods.</exception>
[DebuggerStepThrough]
public Task<TResult> ExecuteAsync(Func<CancellationToken, Task<TResult>> action, CancellationToken cancellationToken, bool continueOnCapturedContext) =>
ExecuteAsync((_, ct) => action(ct), new Context(), cancellationToken, continueOnCapturedContext);
ExecuteAsync((_, ct) => action(ct), [], cancellationToken, continueOnCapturedContext);

/// <summary>
/// Executes the specified asynchronous action within the policy and returns the result.
Expand Down Expand Up @@ -127,7 +127,7 @@ public async Task<TResult> ExecuteAsync(Func<Context, CancellationToken, Task<TR
/// <returns>The captured result</returns>
[DebuggerStepThrough]
public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync(Func<Task<TResult>> action) =>
ExecuteAndCaptureAsync((_, _) => action(), new Context(), CancellationToken.None, DefaultContinueOnCapturedContext);
ExecuteAndCaptureAsync((_, _) => action(), [], CancellationToken.None, DefaultContinueOnCapturedContext);

/// <summary>
/// Executes the specified asynchronous action within the policy and returns the result.
Expand Down Expand Up @@ -158,7 +158,7 @@ public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync(Func<Context, Task<TRe
/// <returns>The captured result</returns>
[DebuggerStepThrough]
public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync(Func<CancellationToken, Task<TResult>> action, CancellationToken cancellationToken) =>
ExecuteAndCaptureAsync((_, ct) => action(ct), new Context(), cancellationToken, DefaultContinueOnCapturedContext);
ExecuteAndCaptureAsync((_, ct) => action(ct), [], cancellationToken, DefaultContinueOnCapturedContext);

/// <summary>
/// Executes the specified asynchronous action within the policy and returns the result.
Expand All @@ -170,7 +170,7 @@ public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync(Func<CancellationToken
/// <exception cref="InvalidOperationException">Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods.</exception>
[DebuggerStepThrough]
public Task<PolicyResult<TResult>> ExecuteAndCaptureAsync(Func<CancellationToken, Task<TResult>> action, CancellationToken cancellationToken, bool continueOnCapturedContext) =>
ExecuteAndCaptureAsync((_, ct) => action(ct), new Context(), cancellationToken, continueOnCapturedContext);
ExecuteAndCaptureAsync((_, ct) => action(ct), [], cancellationToken, continueOnCapturedContext);

/// <summary>
/// Executes the specified asynchronous action within the policy and returns the result.
Expand Down
2 changes: 1 addition & 1 deletion src/Polly/Context.Dictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public partial class Context : IDictionary<string, object>, IDictionary, IReadOn

private Dictionary<string, object> wrappedDictionary;

private Dictionary<string, object> WrappedDictionary => wrappedDictionary ?? (wrappedDictionary = new Dictionary<string, object>());
private Dictionary<string, object> WrappedDictionary => wrappedDictionary ?? (wrappedDictionary = []);

/// <summary>
/// Initializes a new instance of the <see cref="Context"/> class, with the specified <paramref name="operationKey" /> and the supplied <paramref name="contextData"/>.
Expand Down
2 changes: 1 addition & 1 deletion src/Polly/ExceptionPredicates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ExceptionPredicates

internal void Add(ExceptionPredicate predicate)
{
_predicates ??= new List<ExceptionPredicate>(); // The ?? pattern here is sufficient; only a deliberately contrived example would lead to the same PolicyBuilder instance being used in a multi-threaded way to define policies simultaneously on multiple threads.
_predicates ??= []; // The ?? pattern here is sufficient; only a deliberately contrived example would lead to the same PolicyBuilder instance being used in a multi-threaded way to define policies simultaneously on multiple threads.

_predicates.Add(predicate);
}
Expand Down
Loading

0 comments on commit d92e8e8

Please sign in to comment.