diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpers.cs b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpers.cs index cad24dfbb2..c789078f2a 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpers.cs +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpers.cs @@ -180,7 +180,8 @@ aooreThrowIfGreaterThan is not null || aooreThrowIfGreaterThanOrEqual is not nul if (SymbolEqualityComparer.Default.Equals(objectCreationOperation.Type, ane)) { if (aneThrowIfNull is not null && - IsParameterNullCheck(condition.Condition, out IParameterReferenceOperation? nullCheckParameter)) + IsParameterNullCheck(condition.Condition, out IParameterReferenceOperation? nullCheckParameter) && + nullCheckParameter.Type.IsReferenceType) { context.ReportDiagnostic(condition.CreateDiagnostic( UseArgumentNullExceptionThrowIfNullRule, diff --git a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpersTests.cs b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpersTests.cs index ee37f3d144..321565cd5f 100644 --- a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpersTests.cs +++ b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpersTests.cs @@ -177,6 +177,39 @@ string this[string name] return name; } } + + void NullableArg(int? arg) + { + if (arg is null) throw new ArgumentNullException(nameof(arg)); + } + + void GenericMethod(T arg) + { + if (arg is null) throw new ArgumentNullException(nameof(arg)); + } + + void GenericMethodWithClassConstraint(T arg) where T : class + { + {|CA1510:if (arg is null) throw new ArgumentNullException(nameof(arg));|} + } + + void GenericMethodWithTypeConstraint(T arg) where T : C + { + {|CA1510:if (arg is null) throw new ArgumentNullException(nameof(arg));|} + } + + void GenericMethodWithInterfaceConstraint(T arg) where T : IDisposable + { + if (arg is null) throw new ArgumentNullException(nameof(arg)); + } +} + +class GenericType +{ + void M(T arg) + { + if (arg is null) throw new ArgumentNullException(nameof(arg)); + } } ", FixedCode = @@ -260,6 +293,39 @@ string this[string name] return name; } } + + void NullableArg(int? arg) + { + if (arg is null) throw new ArgumentNullException(nameof(arg)); + } + + void GenericMethod(T arg) + { + if (arg is null) throw new ArgumentNullException(nameof(arg)); + } + + void GenericMethodWithClassConstraint(T arg) where T : class + { + ArgumentNullException.ThrowIfNull(arg); + } + + void GenericMethodWithTypeConstraint(T arg) where T : C + { + ArgumentNullException.ThrowIfNull(arg); + } + + void GenericMethodWithInterfaceConstraint(T arg) where T : IDisposable + { + if (arg is null) throw new ArgumentNullException(nameof(arg)); + } +} + +class GenericType +{ + void M(T arg) + { + if (arg is null) throw new ArgumentNullException(nameof(arg)); + } } " }.RunAsync();