From 76a8fc6ff03c147e4362383102ef43e98a5fa992 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baranyai=20M=C3=A1t=C3=A9?= Date: Fri, 26 Jan 2024 17:14:12 +0100 Subject: [PATCH 1/2] Got rid of IDE0066, IDE0250, IDE0063 warnings --- src/Polly/Polly.csproj | 6 ++-- src/Polly/Utilities/EmptyStruct.cs | 2 +- src/Polly/Wrap/AsyncPolicyWrapSyntax.cs | 38 ++++++++----------------- src/Polly/Wrap/PolicyWrapSyntax.cs | 38 ++++++++----------------- 4 files changed, 28 insertions(+), 56 deletions(-) diff --git a/src/Polly/Polly.csproj b/src/Polly/Polly.csproj index 817ad4d631c..e4a0c4388cf 100644 --- a/src/Polly/Polly.csproj +++ b/src/Polly/Polly.csproj @@ -6,10 +6,10 @@ Library 70 true - $(NoWarn);IDE0011;SA1501;S103;IDE0055;SA1111;IDE0066;S3872;SA1127;SA1128;SA1402;SA1513;SA1414;S3215;S2955 + $(NoWarn);IDE0011;SA1501;S103;IDE0055;SA1111;S3872;SA1127;SA1128;SA1402;SA1513;SA1414;S3215;S2955 $(NoWarn);IDE1006;CA1062;S107;CA1068;S4039;SA1121;CA1000;CA1063;SA1113;CA1031;CA1051;CA1200 - $(NoWarn);SA1629;SA1612;CA2211;S2223;CA1032;CA1815;CA1816;S4457;SA1615;IDE0250;S109;SA1618;SA1407;CA1033 - $(NoWarn);SA1515;S4023;CA1010;IDE0063;S2681;S3442;S3880;CA1064;SA1110;SA1203;SA1649;SA1625;SA1623;SA1118 + $(NoWarn);SA1629;SA1612;CA2211;S2223;CA1032;CA1815;CA1816;S4457;SA1615;S109;SA1618;SA1407;CA1033 + $(NoWarn);SA1515;S4023;CA1010;S2681;S3442;S3880;CA1064;SA1110;SA1203;SA1649;SA1625;SA1623;SA1118 $(NoWarn);S3253;S3971;S6605;CA1724;CA1716;SA1108;CA1710;S4049;S3246 $(NoWarn);SA1805;SA1805;CA1805;CA1821 diff --git a/src/Polly/Utilities/EmptyStruct.cs b/src/Polly/Utilities/EmptyStruct.cs index 92980e9c5da..5b7f1a58406 100644 --- a/src/Polly/Utilities/EmptyStruct.cs +++ b/src/Polly/Utilities/EmptyStruct.cs @@ -3,7 +3,7 @@ /// /// A null struct for policies and actions which do not return a TResult. /// -internal struct EmptyStruct +internal readonly struct EmptyStruct { internal static readonly EmptyStruct Instance; } diff --git a/src/Polly/Wrap/AsyncPolicyWrapSyntax.cs b/src/Polly/Wrap/AsyncPolicyWrapSyntax.cs index b2b262e9260..73c7b723791 100644 --- a/src/Polly/Wrap/AsyncPolicyWrapSyntax.cs +++ b/src/Polly/Wrap/AsyncPolicyWrapSyntax.cs @@ -129,20 +129,13 @@ public partial class Policy /// The policies to place in the wrap, outermost (at left) to innermost (at right). /// The PolicyWrap. /// The enumerable of policies to form the wrap must contain at least two policies. - public static AsyncPolicyWrap WrapAsync(params IAsyncPolicy[] policies) - { - switch (policies.Length) + public static AsyncPolicyWrap WrapAsync(params IAsyncPolicy[] policies) => + policies.Length switch { - case 0: - case 1: - throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)); - case 2: - return new AsyncPolicyWrap((AsyncPolicy)policies[0], policies[1]); - - default: - return WrapAsync(policies[0], WrapAsync(policies.Skip(1).ToArray())); - } - } + 0 or 1 => throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)), + 2 => new AsyncPolicyWrap((AsyncPolicy)policies[0], policies[1]), + _ => WrapAsync(policies[0], WrapAsync(policies.Skip(1).ToArray())), + }; /// /// Creates a of the given policies governing delegates returning values of type . @@ -151,18 +144,11 @@ public static AsyncPolicyWrap WrapAsync(params IAsyncPolicy[] policies) /// The return type of delegates which may be executed through the policy. /// The PolicyWrap. /// The enumerable of policies to form the wrap must contain at least two policies. - public static AsyncPolicyWrap WrapAsync(params IAsyncPolicy[] policies) - { - switch (policies.Length) + public static AsyncPolicyWrap WrapAsync(params IAsyncPolicy[] policies) => + policies.Length switch { - case 0: - case 1: - throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)); - case 2: - return new AsyncPolicyWrap((AsyncPolicy)policies[0], policies[1]); - - default: - return WrapAsync(policies[0], WrapAsync(policies.Skip(1).ToArray())); - } - } + 0 or 1 => throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)), + 2 => new AsyncPolicyWrap((AsyncPolicy)policies[0], policies[1]), + _ => WrapAsync(policies[0], WrapAsync(policies.Skip(1).ToArray())), + }; } diff --git a/src/Polly/Wrap/PolicyWrapSyntax.cs b/src/Polly/Wrap/PolicyWrapSyntax.cs index d4b67db64f2..9dedad33f17 100644 --- a/src/Polly/Wrap/PolicyWrapSyntax.cs +++ b/src/Polly/Wrap/PolicyWrapSyntax.cs @@ -129,20 +129,13 @@ public partial class Policy /// The policies to place in the wrap, outermost (at left) to innermost (at right). /// The PolicyWrap. /// The enumerable of policies to form the wrap must contain at least two policies. - public static PolicyWrap Wrap(params ISyncPolicy[] policies) - { - switch (policies.Length) + public static PolicyWrap Wrap(params ISyncPolicy[] policies) => + policies.Length switch { - case 0: - case 1: - throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)); - case 2: - return new PolicyWrap((Policy)policies[0], policies[1]); - - default: - return Wrap(policies[0], Wrap(policies.Skip(1).ToArray())); - } - } + 0 or 1 => throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)), + 2 => new PolicyWrap((Policy)policies[0], policies[1]), + _ => Wrap(policies[0], Wrap(policies.Skip(1).ToArray())), + }; /// /// Creates a of the given policies governing delegates returning values of type . @@ -151,18 +144,11 @@ public static PolicyWrap Wrap(params ISyncPolicy[] policies) /// The return type of delegates which may be executed through the policy. /// The PolicyWrap. /// The enumerable of policies to form the wrap must contain at least two policies. - public static PolicyWrap Wrap(params ISyncPolicy[] policies) - { - switch (policies.Length) + public static PolicyWrap Wrap(params ISyncPolicy[] policies) => + policies.Length switch { - case 0: - case 1: - throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)); - case 2: - return new PolicyWrap((Policy)policies[0], policies[1]); - - default: - return Wrap(policies[0], Wrap(policies.Skip(1).ToArray())); - } - } + 0 or 1 => throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)), + 2 => new PolicyWrap((Policy)policies[0], policies[1]), + _ => Wrap(policies[0], Wrap(policies.Skip(1).ToArray())), + }; } From fec589e6c753b9562943a5caba5811117daa1443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baranyai=20M=C3=A1t=C3=A9?= Date: Fri, 26 Jan 2024 17:30:41 +0100 Subject: [PATCH 2/2] Make TimedLock struct readonly --- src/Polly/Utilities/TimedLock.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Polly/Utilities/TimedLock.cs b/src/Polly/Utilities/TimedLock.cs index 23628c60ea8..d42f5aa5113 100644 --- a/src/Polly/Utilities/TimedLock.cs +++ b/src/Polly/Utilities/TimedLock.cs @@ -12,7 +12,7 @@ namespace Polly.Utilities; // Thanks to John Sands for providing the necessary incentive to make // me invent a way of using a struct in both release and debug builds // without losing the debug leak tracking. -internal struct TimedLock : IDisposable +internal readonly struct TimedLock : IDisposable { // The TimedLock class throws a LockTimeoutException if a lock cannot be obtained within the LockTimeout. This allows the easier discovery and debugging of deadlocks during Polly development, than if using a pure lock. // We do not however ever want to throw a LockTimeoutException in production - hence the forked LockTimeout value below for DEBUG versus RELEASE builds.