Skip to content

Commit d236e7f

Browse files
authored
Only report CA1512 if the specific helper required exists (#6549)
We were raising the diagnostic if any of the helpers on the type existed even if the specific one required didn't.
1 parent 597c280 commit d236e7f

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public override void Initialize(AnalysisContext context)
122122
ISymbol? aooreThrowIfNegative = aoore.GetMembers("ThrowIfNegative").FirstOrDefault();
123123
ISymbol? aooreThrowIfNegativeOrZero = aoore.GetMembers("ThrowIfNegativeOrZero").FirstOrDefault();
124124
ISymbol? aooreThrowIfGreaterThan = aoore.GetMembers("ThrowIfGreaterThan").FirstOrDefault();
125-
ISymbol? aooreThrowIfGreaterThanOrEqual = aoore.GetMembers("aooreThrowIfGreaterThanOrEqual").FirstOrDefault();
125+
ISymbol? aooreThrowIfGreaterThanOrEqual = aoore.GetMembers("ThrowIfGreaterThanOrEqual").FirstOrDefault();
126126
ISymbol? aooreThrowIfLessThan = aoore.GetMembers("ThrowIfLessThan").FirstOrDefault();
127127
ISymbol? aooreThrowIfLessThanOrEqual = aoore.GetMembers("ThrowIfLessThanOrEqual").FirstOrDefault();
128128
ISymbol? aooreThrowIfEqual = aoore.GetMembers("ThrowIfEqual").FirstOrDefault();
@@ -244,11 +244,24 @@ aooreThrowIfLessThan is not null || aooreThrowIfLessThanOrEqual is not null ||
244244

245245
if (additionalLocations.Length != 0 && !AvoidComparing(aooreParameter!))
246246
{
247-
context.ReportDiagnostic(condition.CreateDiagnostic(
248-
UseArgumentOutOfRangeExceptionThrowIfRule,
249-
additionalLocations,
250-
properties: ImmutableDictionary<string, string?>.Empty.Add(MethodNamePropertyKey, methodName),
251-
args: new object[] { nameof(ArgumentOutOfRangeException), methodName! }));
247+
switch (methodName)
248+
{
249+
case "ThrowIfZero" when aooreThrowIfZero is not null:
250+
case "ThrowIfNegative" when aooreThrowIfNegative is not null:
251+
case "ThrowIfNegativeOrZero" when aooreThrowIfNegativeOrZero is not null:
252+
case "ThrowIfGreaterThan" when aooreThrowIfGreaterThan is not null:
253+
case "ThrowIfGreaterThanOrEqual" when aooreThrowIfGreaterThanOrEqual is not null:
254+
case "ThrowIfLessThan" when aooreThrowIfLessThan is not null:
255+
case "ThrowIfLessThanOrEqual" when aooreThrowIfLessThanOrEqual is not null:
256+
case "ThrowIfEqual" when aooreThrowIfEqual is not null:
257+
case "ThrowIfNotEqual" when aooreThrowIfNotEqual is not null:
258+
context.ReportDiagnostic(condition.CreateDiagnostic(
259+
UseArgumentOutOfRangeExceptionThrowIfRule,
260+
additionalLocations,
261+
properties: ImmutableDictionary<string, string?>.Empty.Add(MethodNamePropertyKey, methodName),
262+
args: new object[] { nameof(ArgumentOutOfRangeException), methodName! }));
263+
break;
264+
}
252265
}
253266

254267
static bool AvoidComparing(IParameterReferenceOperation p) =>

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,50 @@ void Nullables(int? arg)
912912
}.RunAsync();
913913
}
914914

915+
[Fact]
916+
public async Task ArgumentOutOfRangeExceptionThrowIf_NoDiagnosticForMissingHelper()
917+
{
918+
await new VerifyCS.Test()
919+
{
920+
LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp9,
921+
TestCode =
922+
@"
923+
using System;
924+
925+
namespace System
926+
{
927+
public class ArgumentOutOfRangeException : Exception
928+
{
929+
public ArgumentOutOfRangeException(string paramName) { }
930+
public ArgumentOutOfRangeException(string paramName, string message) { }
931+
public static void ThrowIfZero<T>(T arg) { }
932+
public static void ThrowIfNegative<T>(T arg) { }
933+
public static void ThrowIfNegativeOrZero<T>(T arg) { }
934+
public static void ThrowIfGreaterThan<T>(T arg, T other) { }
935+
public static void ThrowIfGreaterThanOrEqual<T>(T arg, T other) { }
936+
public static void ThrowIfLessThan<T>(T arg, T other) { }
937+
public static void ThrowIfLessThanOrEqual<T>(T arg, T other) { }
938+
}
939+
}
940+
941+
class C
942+
{
943+
void M(int arg)
944+
{
945+
if (arg != 42)
946+
{
947+
throw new ArgumentOutOfRangeException(nameof(arg));
948+
}
949+
if (42 != arg)
950+
{
951+
throw new ArgumentOutOfRangeException(nameof(arg));
952+
}
953+
}
954+
}
955+
"
956+
}.RunAsync();
957+
}
958+
915959
[Fact]
916960
public async Task ObjectDisposedExceptionThrowIf_DoesntExist_NoDiagnostics()
917961
{

0 commit comments

Comments
 (0)