Skip to content

Commit 0e1ba77

Browse files
committed
Update UseExceptionThrowHelpers to only recommend ANE for classes
1 parent 22361ae commit 0e1ba77

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpers.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ aooreThrowIfGreaterThan is not null || aooreThrowIfGreaterThanOrEqual is not nul
180180
if (SymbolEqualityComparer.Default.Equals(objectCreationOperation.Type, ane))
181181
{
182182
if (aneThrowIfNull is not null &&
183-
IsParameterNullCheck(condition.Condition, out IParameterReferenceOperation? nullCheckParameter))
183+
IsParameterNullCheck(condition.Condition, out IParameterReferenceOperation? nullCheckParameter) &&
184+
nullCheckParameter.Type.TypeKind == TypeKind.Class)
184185
{
185186
context.ReportDiagnostic(condition.CreateDiagnostic(
186187
UseArgumentNullExceptionThrowIfNullRule,

src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpersTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,24 @@ string this[string name]
177177
return name;
178178
}
179179
}
180+
181+
void NullableArg(int? arg)
182+
{
183+
if (arg is null) throw new ArgumentNullException(nameof(arg));
184+
}
185+
186+
void GenericMethod<T>(T arg)
187+
{
188+
if (arg is null) throw new ArgumentNullException(nameof(arg));
189+
}
190+
}
191+
192+
class GenericType<T>
193+
{
194+
void M(T arg)
195+
{
196+
if (arg is null) throw new ArgumentNullException(nameof(arg));
197+
}
180198
}
181199
",
182200
FixedCode =
@@ -260,6 +278,24 @@ string this[string name]
260278
return name;
261279
}
262280
}
281+
282+
void NullableArg(int? arg)
283+
{
284+
if (arg is null) throw new ArgumentNullException(nameof(arg));
285+
}
286+
287+
void GenericMethod<T>(T arg)
288+
{
289+
if (arg is null) throw new ArgumentNullException(nameof(arg));
290+
}
291+
}
292+
293+
class GenericType<T>
294+
{
295+
void M(T arg)
296+
{
297+
if (arg is null) throw new ArgumentNullException(nameof(arg));
298+
}
263299
}
264300
"
265301
}.RunAsync();

0 commit comments

Comments
 (0)