Skip to content

Commit f019aa3

Browse files
committed
Align ThrowHelper test stub with real signatures
Remove 0-arg overloads from ThrowHelperStub that don't exist in the real ThrowHelper. Convert ShouldFixZeroArgConstructor to a negative test since there's no matching overload. 🤖 Co-Authored-By: Claude Code <noreply@anthropic.com>
1 parent d852eb3 commit f019aa3

File tree

2 files changed

+20
-42
lines changed

2 files changed

+20
-42
lines changed

tracer/src/Datadog.Trace.Tools.Analyzers.CodeFixes/ThrowInInlinedMethodAnalyzer/ThrowInInlinedMethodCodeFixProvider.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ public class ThrowInInlinedMethodCodeFixProvider : CodeFixProvider
2929
{
3030
private const string ThrowHelperUsingNamespace = "Datadog.Trace.Util";
3131

32-
private static readonly Dictionary<string, (string MethodName, int MaxArgs)> SupportedExceptions
33-
= new Dictionary<string, (string, int)>
32+
private static readonly Dictionary<string, (string MethodName, int MinArgs, int MaxArgs)> SupportedExceptions
33+
= new Dictionary<string, (string, int, int)>
3434
{
35-
["System.ArgumentNullException"] = ("ThrowArgumentNullException", 1),
36-
["System.ArgumentOutOfRangeException"] = ("ThrowArgumentOutOfRangeException", 3),
37-
["System.ArgumentException"] = ("ThrowArgumentException", 2),
38-
["System.InvalidOperationException"] = ("ThrowInvalidOperationException", 1),
39-
["System.Exception"] = ("ThrowException", 1),
40-
["System.InvalidCastException"] = ("ThrowInvalidCastException", 1),
41-
["System.IndexOutOfRangeException"] = ("ThrowIndexOutOfRangeException", 1),
42-
["System.NotSupportedException"] = ("ThrowNotSupportedException", 1),
43-
["System.Collections.Generic.KeyNotFoundException"] = ("ThrowKeyNotFoundException", 1),
44-
["System.NullReferenceException"] = ("ThrowNullReferenceException", 1),
45-
["System.ObjectDisposedException"] = ("ThrowObjectDisposedException", 1),
35+
["System.ArgumentNullException"] = ("ThrowArgumentNullException", 1, 1),
36+
["System.ArgumentOutOfRangeException"] = ("ThrowArgumentOutOfRangeException", 1, 3),
37+
["System.ArgumentException"] = ("ThrowArgumentException", 1, 2),
38+
["System.InvalidOperationException"] = ("ThrowInvalidOperationException", 1, 1),
39+
["System.Exception"] = ("ThrowException", 1, 1),
40+
["System.InvalidCastException"] = ("ThrowInvalidCastException", 1, 1),
41+
["System.IndexOutOfRangeException"] = ("ThrowIndexOutOfRangeException", 1, 1),
42+
["System.NotSupportedException"] = ("ThrowNotSupportedException", 1, 1),
43+
["System.Collections.Generic.KeyNotFoundException"] = ("ThrowKeyNotFoundException", 1, 1),
44+
["System.NullReferenceException"] = ("ThrowNullReferenceException", 1, 1),
45+
["System.ObjectDisposedException"] = ("ThrowObjectDisposedException", 1, 1),
4646
};
4747

4848
/// <inheritdoc />
@@ -112,7 +112,7 @@ private static bool TryGetThrowHelperMethod(
112112
}
113113

114114
var argCount = creation.ArgumentList?.Arguments.Count ?? 0;
115-
if (argCount > entry.MaxArgs)
115+
if (argCount < entry.MinArgs || argCount > entry.MaxArgs)
116116
{
117117
return false;
118118
}

tracer/test/Datadog.Trace.Tools.Analyzers.Tests/ThrowInInlinedMethodAnalyzer/ThrowInInlinedMethodCodeFixProviderTests.cs

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,25 @@ public class ThrowInInlinedMethodCodeFixProviderTests
2222
{
2323
private const string DiagnosticId = Analyzers.ThrowInInlinedMethodAnalyzer.Diagnostics.DiagnosticId;
2424

25-
// Minimal ThrowHelper stub so the fixed code compiles in the test harness
25+
// Minimal ThrowHelper stub matching real signatures so the fixed code compiles in the test harness
2626
private const string ThrowHelperStub = """
2727
2828
namespace Datadog.Trace.Util
2929
{
3030
internal static class ThrowHelper
3131
{
32-
internal static void ThrowArgumentNullException() => throw new System.ArgumentNullException();
3332
internal static void ThrowArgumentNullException(string paramName) => throw new System.ArgumentNullException(paramName);
34-
internal static void ThrowArgumentOutOfRangeException() => throw new System.ArgumentOutOfRangeException();
3533
internal static void ThrowArgumentOutOfRangeException(string paramName) => throw new System.ArgumentOutOfRangeException(paramName);
3634
internal static void ThrowArgumentOutOfRangeException(string paramName, string message) => throw new System.ArgumentOutOfRangeException(paramName, message);
3735
internal static void ThrowArgumentOutOfRangeException(string paramName, object actualValue, string message) => throw new System.ArgumentOutOfRangeException(paramName, actualValue, message);
38-
internal static void ThrowArgumentException() => throw new System.ArgumentException();
3936
internal static void ThrowArgumentException(string message) => throw new System.ArgumentException(message);
4037
internal static void ThrowArgumentException(string message, string paramName) => throw new System.ArgumentException(message, paramName);
41-
internal static void ThrowInvalidOperationException() => throw new System.InvalidOperationException();
4238
internal static void ThrowInvalidOperationException(string message) => throw new System.InvalidOperationException(message);
43-
internal static void ThrowException() => throw new System.Exception();
4439
internal static void ThrowException(string message) => throw new System.Exception(message);
45-
internal static void ThrowInvalidCastException() => throw new System.InvalidCastException();
4640
internal static void ThrowInvalidCastException(string message) => throw new System.InvalidCastException(message);
47-
internal static void ThrowIndexOutOfRangeException() => throw new System.IndexOutOfRangeException();
4841
internal static void ThrowIndexOutOfRangeException(string message) => throw new System.IndexOutOfRangeException(message);
49-
internal static void ThrowNotSupportedException() => throw new System.NotSupportedException();
5042
internal static void ThrowNotSupportedException(string message) => throw new System.NotSupportedException(message);
51-
internal static void ThrowKeyNotFoundException() => throw new System.Collections.Generic.KeyNotFoundException();
5243
internal static void ThrowKeyNotFoundException(string message) => throw new System.Collections.Generic.KeyNotFoundException(message);
53-
internal static void ThrowNullReferenceException() => throw new System.NullReferenceException();
5444
internal static void ThrowNullReferenceException(string message) => throw new System.NullReferenceException(message);
5545
}
5646
}
@@ -271,8 +261,10 @@ void TestMethod()
271261
}
272262

273263
[Fact]
274-
public async Task ShouldFixZeroArgConstructor()
264+
public async Task ShouldNotFixZeroArgConstructor()
275265
{
266+
// Zero-arg constructors have no matching ThrowHelper overload, so no fix should be offered.
267+
// The diagnostic is still reported, but the source is unchanged.
276268
const string source = """
277269
using System;
278270
using System.Runtime.CompilerServices;
@@ -287,25 +279,11 @@ void TestMethod()
287279
}
288280
""";
289281

290-
const string fix = """
291-
using System;
292-
using System.Runtime.CompilerServices;
293-
using Datadog.Trace.Util;
294-
295-
class TestClass
296-
{
297-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
298-
void TestMethod()
299-
{
300-
ThrowHelper.ThrowInvalidOperationException();
301-
}
302-
}
303-
""";
304-
305282
var expected = new DiagnosticResult(DiagnosticId, DiagnosticSeverity.Warning)
306283
.WithLocation(0)
307284
.WithArguments("TestMethod");
308-
await Verifier.VerifyCodeFixAsync(source + ThrowHelperStub, expected, fix + ThrowHelperStub);
285+
286+
await Verifier.VerifyCodeFixAsync(source, expected, source);
309287
}
310288

311289
[Fact]

0 commit comments

Comments
 (0)