From a985305d56bc15b7ee7ad67b9605034d8db315d6 Mon Sep 17 00:00:00 2001 From: ZLoo Date: Mon, 22 Jul 2024 20:25:20 +0300 Subject: [PATCH] Fix warning CA1062#IPolicyWrapExtension and fix S4456 Parameter validation in yielding methods should be wrapped https://rules.sonarsource.com/csharp/RSPEC-4456 --- src/Polly/Wrap/IPolicyWrapExtension.cs | 37 ++++++++++++------- .../Wrap/IPolicyWrapExtensionSpecs.cs | 9 +++++ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/Polly/Wrap/IPolicyWrapExtension.cs b/src/Polly/Wrap/IPolicyWrapExtension.cs index b34ae3303ae..e97a606f21d 100644 --- a/src/Polly/Wrap/IPolicyWrapExtension.cs +++ b/src/Polly/Wrap/IPolicyWrapExtension.cs @@ -3,7 +3,6 @@ /// /// Extension methods for IPolicyWrap. /// -#pragma warning disable CA1062 // Validate arguments of public methods public static class IPolicyWrapExtension { /// @@ -13,21 +12,12 @@ public static class IPolicyWrapExtension /// An of all the policies in the wrap. public static IEnumerable GetPolicies(this IPolicyWrap policyWrap) { - var childPolicies = new[] { policyWrap.Outer, policyWrap.Inner }; - foreach (var childPolicy in childPolicies) + if (policyWrap is null) { - if (childPolicy is IPolicyWrap anotherWrap) - { - foreach (var policy in anotherWrap.GetPolicies()) - { - yield return policy; - } - } - else if (childPolicy != null) - { - yield return childPolicy; - } + throw new ArgumentNullException(nameof(policyWrap)); } + + return GetPoliciesIterator(policyWrap); } /// @@ -83,4 +73,23 @@ public static TPolicy GetPolicy(this IPolicyWrap policyWrap, Func().SingleOrDefault(filter); } + + private static IEnumerable GetPoliciesIterator(IPolicyWrap policyWrap) + { + var childPolicies = new[] { policyWrap.Outer, policyWrap.Inner }; + foreach (var childPolicy in childPolicies) + { + if (childPolicy is IPolicyWrap anotherWrap) + { + foreach (var policy in anotherWrap.GetPolicies()) + { + yield return policy; + } + } + else if (childPolicy != null) + { + yield return childPolicy; + } + } + } } diff --git a/test/Polly.Specs/Wrap/IPolicyWrapExtensionSpecs.cs b/test/Polly.Specs/Wrap/IPolicyWrapExtensionSpecs.cs index 47aaaacfb51..e3ced53bb36 100644 --- a/test/Polly.Specs/Wrap/IPolicyWrapExtensionSpecs.cs +++ b/test/Polly.Specs/Wrap/IPolicyWrapExtensionSpecs.cs @@ -2,6 +2,15 @@ public class IPolicyWrapExtensionSpecs { + [Fact] + public void Should_throw_when_policy_wrap_is_null() + { + IPolicyWrap policyWrap = null!; + + var action = () => policyWrap.GetPolicies(); + action.Should().Throw().And.ParamName.Should().Be("policyWrap"); + } + [Fact] public void Should_pass_all_nested_policies_from_PolicyWrap_in_same_order_they_were_added() {