Skip to content

Commit

Permalink
use target typed new where type is evident
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Jan 15, 2023
1 parent fea0007 commit 4575972
Show file tree
Hide file tree
Showing 23 changed files with 45 additions and 55 deletions.
11 changes: 5 additions & 6 deletions src/Polly.Benchmarks/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ namespace Polly.Benchmarks;
[Config(typeof(PollyConfig))]
public class Cache
{
private static readonly MemoryCache MemoryCache = new MemoryCache(new MemoryCacheOptions());
private static readonly MemoryCacheProvider CacheProvider = new MemoryCacheProvider(MemoryCache);
private static readonly MemoryCache MemoryCache = new(new MemoryCacheOptions());
private static readonly MemoryCacheProvider CacheProvider = new(MemoryCache);

private static readonly Policy SyncPolicyMiss = Policy.Cache(CacheProvider, TimeSpan.Zero);
private static readonly AsyncPolicy AsyncPolicyMiss = Policy.CacheAsync(CacheProvider, TimeSpan.Zero);

private static readonly Policy SyncPolicyHit = Policy.Cache(CacheProvider, TimeSpan.MaxValue);
private static readonly AsyncPolicy AsyncPolicyHit = Policy.CacheAsync(CacheProvider, TimeSpan.MaxValue);

private static readonly Context HitContext = new Context(nameof(HitContext));
private static readonly Context MissContext = new Context(nameof(MissContext));
private static readonly Context HitContext = new(nameof(HitContext));
private static readonly Context MissContext = new(nameof(MissContext));

[GlobalSetup]
public Task GlobalSetup()
Expand All @@ -40,8 +40,7 @@ public object Cache_Synchronous_Miss() =>
public Task<object> Cache_Asynchronous_Miss() =>
AsyncPolicyMiss.ExecuteAsync((_, token) => GetObjectAsync(token), MissContext, CancellationToken.None);

private static object GetObject() =>
new object();
private static object GetObject() => new();

private static Task<object> GetObjectAsync(CancellationToken cancellationToken) =>
Task.FromResult(new object());
Expand Down
2 changes: 1 addition & 1 deletion src/Polly.Specs/Bulkhead/BulkheadSpecsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public BulkheadSpecsBase(ITestOutputHelper testOutputHelper)

protected Task[] Tasks { get; set; } = { };

protected readonly AutoResetEvent StatusChangedEvent = new AutoResetEvent(false);
protected readonly AutoResetEvent StatusChangedEvent = new(false);

#endregion

Expand Down
2 changes: 1 addition & 1 deletion src/Polly.Specs/Helpers/Bulkhead/AnnotatedOutputHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Item(string format, object[] args)
public object[] Args { get; }
}

private readonly ConcurrentDictionary<Guid, Item> items = new ConcurrentDictionary<Guid, Item>();
private readonly ConcurrentDictionary<Guid, Item> items = new();

private readonly object[] noArgs = Array.Empty<object>();

Expand Down
4 changes: 2 additions & 2 deletions src/Polly.Specs/Helpers/Bulkhead/TraceableAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class TraceableAction : IDisposable
private readonly string _id;
private readonly ITestOutputHelper _testOutputHelper;

private readonly TaskCompletionSource<object?> _tcsProxyForRealWork = new TaskCompletionSource<object?>();
private readonly CancellationTokenSource CancellationSource = new CancellationTokenSource();
private readonly TaskCompletionSource<object?> _tcsProxyForRealWork = new();
private readonly CancellationTokenSource CancellationSource = new();

private TraceableActionStatus _status;
private readonly AutoResetEvent _statusChanged;
Expand Down
2 changes: 1 addition & 1 deletion src/Polly.Specs/Helpers/Caching/StubCacheProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public CacheItem(object? value, Ttl ttl)
public readonly object? Value;
}

private readonly Dictionary<string, CacheItem> cachedValues = new Dictionary<string, CacheItem>();
private readonly Dictionary<string, CacheItem> cachedValues = new();

public (bool, object?) TryGet(string key)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ internal class StubErroringCacheProvider : ISyncCacheProvider, IAsyncCacheProvid
private Exception? _getException;
private Exception? _putException;

private StubCacheProvider innerProvider = new StubCacheProvider();
private StubCacheProvider innerProvider = new();

public StubErroringCacheProvider(Exception? getException, Exception? putException)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ internal class AsyncPreExecutePolicy : AsyncPolicy
{
private Func<Task> _preExecute;

public static AsyncPreExecutePolicy CreateAsync(Func<Task> preExecute) =>
new AsyncPreExecutePolicy(preExecute);
public static AsyncPreExecutePolicy CreateAsync(Func<Task> preExecute) => new(preExecute);

internal AsyncPreExecutePolicy(Func<Task> preExecute) =>
_preExecute = preExecute ?? throw new ArgumentNullException(nameof(preExecute));
Expand Down
6 changes: 2 additions & 4 deletions src/Polly.Specs/Helpers/Custom/PreExecute/PreExecutePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ internal class PreExecutePolicy : Policy
{
private Action _preExecute;

public static PreExecutePolicy Create(Action preExecute) =>
new PreExecutePolicy(preExecute);
public static PreExecutePolicy Create(Action preExecute) => new(preExecute);

internal PreExecutePolicy(Action preExecute) =>
_preExecute = preExecute ?? throw new ArgumentNullException(nameof(preExecute));
Expand All @@ -18,8 +17,7 @@ internal class PreExecutePolicy<TResult> : Policy<TResult>
{
private Action _preExecute;

public static PreExecutePolicy<TResult> Create(Action preExecute) =>
new PreExecutePolicy<TResult>(preExecute);
public static PreExecutePolicy<TResult> Create(Action preExecute) => new(preExecute);

internal PreExecutePolicy(Action preExecute) =>
_preExecute = preExecute ?? throw new ArgumentNullException(nameof(preExecute));
Expand Down
8 changes: 4 additions & 4 deletions src/Polly/Caching/CacheProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static IAsyncCacheProvider<TCacheFormat> AsyncFor<TCacheFormat>(this IAsy
/// <returns>SerializingCacheProvider&lt;TResult, TSerialized&gt;.</returns>
public static SerializingCacheProvider<TSerialized> WithSerializer<TSerialized>(
this ISyncCacheProvider<TSerialized> cacheProvider, ICacheItemSerializer<object, TSerialized> serializer) =>
new SerializingCacheProvider<TSerialized>(cacheProvider, serializer);
new(cacheProvider, serializer);

/// <summary>
/// Wraps the <paramref name="serializer"/> around the <paramref name="cacheProvider"/> so that delegate return values of type <typeparamref name="TResult"/> can be stored in the cache as type <typeparamref name="TSerialized"/>.
Expand All @@ -45,7 +45,7 @@ public static SerializingCacheProvider<TSerialized> WithSerializer<TSerialized>(
/// <returns>SerializingCacheProvider&lt;TResult, TSerialized&gt;.</returns>
public static SerializingCacheProvider<TResult, TSerialized> WithSerializer<TResult, TSerialized>(
this ISyncCacheProvider<TSerialized> cacheProvider, ICacheItemSerializer<TResult, TSerialized> serializer) =>
new SerializingCacheProvider<TResult, TSerialized>(cacheProvider, serializer);
new(cacheProvider, serializer);

/// <summary>
/// Wraps the <paramref name="serializer"/> around the asynchronous <paramref name="cacheProvider"/> so that delegate return values of any type can be stored in the cache as type <typeparamref name="TSerialized"/>.
Expand All @@ -56,7 +56,7 @@ public static SerializingCacheProvider<TResult, TSerialized> WithSerializer<TRes
/// <returns>SerializingCacheProvider&lt;TResult, TSerialized&gt;.</returns>
public static AsyncSerializingCacheProvider<TSerialized> WithSerializer<TSerialized>(
this IAsyncCacheProvider<TSerialized> cacheProvider, ICacheItemSerializer<object, TSerialized> serializer) =>
new AsyncSerializingCacheProvider<TSerialized>(cacheProvider, serializer);
new(cacheProvider, serializer);

/// <summary>
/// Wraps the <paramref name="serializer"/> around the asynchronous <paramref name="cacheProvider"/> so that delegate return values of type <typeparamref name="TResult"/> can be stored in the cache as type <typeparamref name="TSerialized"/>.
Expand All @@ -68,5 +68,5 @@ public static AsyncSerializingCacheProvider<TSerialized> WithSerializer<TSeriali
/// <returns>SerializingCacheProvider&lt;TResult, TSerialized&gt;.</returns>
public static AsyncSerializingCacheProvider<TResult, TSerialized> WithSerializer<TResult, TSerialized>(
this IAsyncCacheProvider<TSerialized> cacheProvider, ICacheItemSerializer<TResult, TSerialized> serializer) =>
new AsyncSerializingCacheProvider<TResult, TSerialized>(cacheProvider, serializer);
new(cacheProvider, serializer);
}
2 changes: 1 addition & 1 deletion src/Polly/Caching/ContextualTtl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ContextualTtl : ITtlStrategy
/// </summary>
public static readonly string SlidingExpirationKey = "ContextualTtlSliding";

private static readonly Ttl _noTtl = new Ttl(TimeSpan.Zero, false);
private static readonly Ttl _noTtl = new(TimeSpan.Zero, false);

/// <summary>
/// Gets the TimeSpan for which to keep an item about to be cached, which may be influenced by data in the execution context.
Expand Down
3 changes: 1 addition & 2 deletions src/Polly/Caching/RelativeTtl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ public RelativeTtl(TimeSpan ttl)
/// <param name="context">The execution context.</param>
/// <param name="result">The execution result.</param>
/// <returns>A <see cref="Ttl"/> representing the remaining Ttl of the cached item.</returns>
public Ttl GetTtl(Context context, object? result) =>
new Ttl(ttl);
public Ttl GetTtl(Context context, object? result) => new(ttl);
}
2 changes: 1 addition & 1 deletion src/Polly/CircuitBreaker/CircuitStateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal abstract class CircuitStateController<TResult> : ICircuitController<TRe
protected readonly Action<Context> _onReset;
protected readonly Action _onHalfOpen;

protected readonly object _lock = new object();
protected readonly object _lock = new();

protected CircuitStateController(
TimeSpan durationOfBreak,
Expand Down
3 changes: 1 addition & 2 deletions src/Polly/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
/// </summary>
public partial class Context
{
internal static Context None() =>
new Context();
internal static Context None() => new();

private Guid? _correlationId;

Expand Down
2 changes: 1 addition & 1 deletion src/Polly/ExceptionPredicates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public Exception FirstMatchOrDefault(Exception ex) =>
/// <summary>
/// Specifies that no Exception-handling filters are applied or are required.
/// </summary>
public static readonly ExceptionPredicates None = new ExceptionPredicates();
public static readonly ExceptionPredicates None = new();
}

3 changes: 1 addition & 2 deletions src/Polly/NoOp/AsyncNoOpSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ public partial class Policy
/// Builds a NoOp <see cref="AsyncPolicy"/> that will execute without any custom behavior.
/// </summary>
/// <returns>The policy instance.</returns>
public static AsyncNoOpPolicy NoOpAsync() =>
new AsyncNoOpPolicy();
public static AsyncNoOpPolicy NoOpAsync() => new();
}
3 changes: 1 addition & 2 deletions src/Polly/NoOp/AsyncNoOpTResultSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ public partial class Policy
/// </summary>
/// <typeparam name="TResult">The type of return values this policy will handle.</typeparam>
/// <returns>The policy instance.</returns>
public static AsyncNoOpPolicy<TResult> NoOpAsync<TResult>() =>
new AsyncNoOpPolicy<TResult>();
public static AsyncNoOpPolicy<TResult> NoOpAsync<TResult>() => new();
}
3 changes: 1 addition & 2 deletions src/Polly/NoOp/NoOpSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ public partial class Policy
/// Builds a NoOp <see cref="Policy"/> that will execute without any custom behavior.
/// </summary>
/// <returns>The policy instance.</returns>
public static NoOpPolicy NoOp() =>
new NoOpPolicy();
public static NoOpPolicy NoOp() => new();
}
3 changes: 1 addition & 2 deletions src/Polly/NoOp/NoOpTResultSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ public partial class Policy
/// </summary>
/// <typeparam name="TResult">The type of return values this policy will handle.</typeparam>
/// <returns>The policy instance.</returns>
public static NoOpPolicy<TResult> NoOp<TResult>() =>
new NoOpPolicy<TResult>();
public static NoOpPolicy<TResult> NoOp<TResult>() => new();
}
20 changes: 10 additions & 10 deletions src/Polly/Policy.HandleSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public partial class Policy
/// <typeparam name="TException">The type of the exception to handle.</typeparam>
/// <returns>The PolicyBuilder instance, for fluent chaining.</returns>
public static PolicyBuilder Handle<TException>() where TException : Exception =>
new PolicyBuilder(exception => exception is TException ? exception : null);
new(exception => exception is TException ? exception : null);

/// <summary>
/// Specifies the type of exception that this policy can handle with additional filters on this exception type.
Expand All @@ -17,23 +17,23 @@ public static PolicyBuilder Handle<TException>() where TException : Exception =>
/// <param name="exceptionPredicate">The exception predicate to filter the type of exception this policy can handle.</param>
/// <returns>The PolicyBuilder instance, for fluent chaining.</returns>
public static PolicyBuilder Handle<TException>(Func<TException, bool> exceptionPredicate) where TException : Exception =>
new PolicyBuilder(exception => exception is TException texception && exceptionPredicate(texception) ? exception : null);
new(exception => exception is TException texception && exceptionPredicate(texception) ? exception : null);

/// <summary>
/// Specifies the type of exception that this policy can handle if found as an InnerException of a regular <see cref="Exception"/>, or at any level of nesting within an <see cref="AggregateException"/>.
/// </summary>
/// <typeparam name="TException">The type of the exception to handle.</typeparam>
/// <returns>The PolicyBuilder instance, for fluent chaining.</returns>
public static PolicyBuilder HandleInner<TException>() where TException : Exception =>
new PolicyBuilder(PolicyBuilder.HandleInner(ex => ex is TException));
new(PolicyBuilder.HandleInner(ex => ex is TException));

/// <summary>
/// Specifies the type of exception that this policy can handle, with additional filters on this exception type, if found as an InnerException of a regular <see cref="Exception"/>, or at any level of nesting within an <see cref="AggregateException"/>.
/// </summary>
/// <typeparam name="TException">The type of the exception to handle.</typeparam>
/// <returns>The PolicyBuilder instance, for fluent chaining.</returns>
public static PolicyBuilder HandleInner<TException>(Func<TException, bool> exceptionPredicate) where TException : Exception =>
new PolicyBuilder(PolicyBuilder.HandleInner(ex => ex is TException texception && exceptionPredicate(texception)));
new(PolicyBuilder.HandleInner(ex => ex is TException texception && exceptionPredicate(texception)));

/// <summary>
/// Specifies the type of return result that this policy can handle with additional filters on the result.
Expand All @@ -42,7 +42,7 @@ public static PolicyBuilder HandleInner<TException>(Func<TException, bool> excep
/// <param name="resultPredicate">The predicate to filter results this policy will handle.</param>
/// <returns>The PolicyBuilder instance.</returns>
public static PolicyBuilder<TResult> HandleResult<TResult>(Func<TResult, bool> resultPredicate) =>
new PolicyBuilder<TResult>(resultPredicate);
new(resultPredicate);

/// <summary>
/// Specifies the type of return result that this policy can handle, and a result value which the policy will handle.
Expand All @@ -63,7 +63,7 @@ public partial class Policy<TResult>
/// <typeparam name="TException">The type of the exception to handle.</typeparam>
/// <returns>The PolicyBuilder instance.</returns>
public static PolicyBuilder<TResult> Handle<TException>() where TException : Exception =>
new PolicyBuilder<TResult>(exception => exception is TException ? exception : null);
new(exception => exception is TException ? exception : null);

/// <summary>
/// Specifies the type of exception that this policy can handle with additional filters on this exception type.
Expand All @@ -72,31 +72,31 @@ public static PolicyBuilder<TResult> Handle<TException>() where TException : Exc
/// <param name="exceptionPredicate">The exception predicate to filter the type of exception this policy can handle.</param>
/// <returns>The PolicyBuilder instance.</returns>
public static PolicyBuilder<TResult> Handle<TException>(Func<TException, bool> exceptionPredicate) where TException : Exception =>
new PolicyBuilder<TResult>(exception => exception is TException texception && exceptionPredicate(texception) ? exception : null);
new(exception => exception is TException texception && exceptionPredicate(texception) ? exception : null);

/// <summary>
/// Specifies the type of exception that this policy can handle if found as an InnerException of a regular <see cref="Exception"/>, or at any level of nesting within an <see cref="AggregateException"/>.
/// </summary>
/// <typeparam name="TException">The type of the exception to handle.</typeparam>
/// <returns>The PolicyBuilder instance, for fluent chaining.</returns>
public static PolicyBuilder<TResult> HandleInner<TException>() where TException : Exception =>
new PolicyBuilder<TResult>(PolicyBuilder.HandleInner(ex => ex is TException));
new(PolicyBuilder.HandleInner(ex => ex is TException));

/// <summary>
/// Specifies the type of exception that this policy can handle, with additional filters on this exception type, if found as an InnerException of a regular <see cref="Exception"/>, or at any level of nesting within an <see cref="AggregateException"/>.
/// </summary>
/// <typeparam name="TException">The type of the exception to handle.</typeparam>
/// <returns>The PolicyBuilder instance, for fluent chaining.</returns>
public static PolicyBuilder<TResult> HandleInner<TException>(Func<TException, bool> exceptionPredicate) where TException : Exception =>
new PolicyBuilder<TResult>(PolicyBuilder.HandleInner(ex => ex is TException texception && exceptionPredicate(texception)));
new(PolicyBuilder.HandleInner(ex => ex is TException texception && exceptionPredicate(texception)));

/// <summary>
/// Specifies a filter on the return result values that this strongly-typed generic policy will handle.
/// </summary>
/// <param name="resultPredicate">The predicate to filter the results this policy will handle.</param>
/// <returns>The PolicyBuilder instance.</returns>
public static PolicyBuilder<TResult> HandleResult(Func<TResult, bool> resultPredicate) =>
new PolicyBuilder<TResult>(resultPredicate);
new(resultPredicate);

/// <summary>
/// Specifies a return result value which the strongly-typed generic policy will handle.
Expand Down
2 changes: 1 addition & 1 deletion src/Polly/PolicyBase.ContextAndKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public abstract partial class PolicyBase
/// </summary>
public string PolicyKey => policyKeyInternal ?? (policyKeyInternal = GetType().Name + "-" + KeyHelper.GuidPart());

internal static ArgumentException PolicyKeyMustBeImmutableException => new ArgumentException("PolicyKey cannot be changed once set; or (when using the default value after the PolicyKey property has been accessed.", "policyKey");
internal static ArgumentException PolicyKeyMustBeImmutableException => new("PolicyKey cannot be changed once set; or (when using the default value after the PolicyKey property has been accessed.", "policyKey");

/// <summary>
/// Updates the execution <see cref="Context"/> with context from the executing policy.
Expand Down
Loading

0 comments on commit 4575972

Please sign in to comment.