Skip to content

Commit 87cfed7

Browse files
authored
Fix CA1062 warnings (#2219)
Fix CA1062 warnings for `NoOpPolicy`.
1 parent 994193e commit 87cfed7

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

src/Polly/NoOp/NoOpPolicy.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ namespace Polly.NoOp;
44
/// <summary>
55
/// A no op policy that can be applied to delegates.
66
/// </summary>
7-
#pragma warning disable CA1062 // Validate arguments of public methods
87
public class NoOpPolicy : Policy, INoOpPolicy
98
{
109
internal NoOpPolicy()
@@ -13,8 +12,15 @@ internal NoOpPolicy()
1312

1413
/// <inheritdoc/>
1514
[DebuggerStepThrough]
16-
protected override TResult Implementation<TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) =>
17-
NoOpEngine.Implementation(action, context, cancellationToken);
15+
protected override TResult Implementation<TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
16+
{
17+
if (action is null)
18+
{
19+
throw new ArgumentNullException(nameof(action));
20+
}
21+
22+
return NoOpEngine.Implementation(action, context, cancellationToken);
23+
}
1824
}
1925

2026
/// <summary>
@@ -29,6 +35,13 @@ internal NoOpPolicy()
2935

3036
/// <inheritdoc/>
3137
[DebuggerStepThrough]
32-
protected override TResult Implementation(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) =>
33-
NoOpEngine.Implementation(action, context, cancellationToken);
38+
protected override TResult Implementation(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
39+
{
40+
if (action is null)
41+
{
42+
throw new ArgumentNullException(nameof(action));
43+
}
44+
45+
return NoOpEngine.Implementation(action, context, cancellationToken);
46+
}
3447
}

test/Polly.Specs/NoOp/NoOpSpecs.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@
22

33
public class NoOpSpecs
44
{
5+
[Fact]
6+
public void Should_throw_when_action_is_null()
7+
{
8+
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
9+
Func<Context, CancellationToken, EmptyStruct> action = null!;
10+
11+
var instance = Activator.CreateInstance(typeof(NoOpPolicy), true)!;
12+
var instanceType = instance.GetType();
13+
var methods = instanceType.GetMethods(flags);
14+
15+
var methodInfo = methods.First(method => method is { Name: "Implementation", ReturnType.Name: "TResult" });
16+
var generic = methodInfo.MakeGenericMethod(typeof(EmptyStruct));
17+
var func = () => generic.Invoke(instance, [action, new Context(), CancellationToken.None]);
18+
19+
var exceptionAssertions = func.Should().Throw<TargetInvocationException>();
20+
exceptionAssertions.And.Message.Should().Be("Exception has been thrown by the target of an invocation.");
21+
exceptionAssertions.WithInnerException<ArgumentNullException>("action");
22+
}
23+
524
[Fact]
625
public void Should_execute_user_delegate()
726
{

test/Polly.Specs/NoOp/NoOpTResultSpecs.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@
22

33
public class NoOpTResultSpecs
44
{
5+
[Fact]
6+
public void Should_throw_when_action_is_null()
7+
{
8+
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
9+
Func<Context, CancellationToken, EmptyStruct> action = null!;
10+
11+
var instance = Activator.CreateInstance(typeof(NoOpPolicy<EmptyStruct>), true)!;
12+
var instanceType = instance.GetType();
13+
var methods = instanceType.GetMethods(flags);
14+
var methodInfo = methods.First(method => method is { Name: "Implementation", ReturnType.Name: "EmptyStruct" });
15+
var func = () => methodInfo.Invoke(instance, [action, new Context(), CancellationToken.None]);
16+
17+
var exceptionAssertions = func.Should().Throw<TargetInvocationException>();
18+
exceptionAssertions.And.Message.Should().Be("Exception has been thrown by the target of an invocation.");
19+
exceptionAssertions.WithInnerException<ArgumentNullException>("action");
20+
}
21+
522
[Fact]
623
public void Should_execute_user_delegate()
724
{

0 commit comments

Comments
 (0)