Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public override void Initialize(AnalysisContext context)
ISymbol? aooreThrowIfNegative = aoore.GetMembers("ThrowIfNegative").FirstOrDefault();
ISymbol? aooreThrowIfNegativeOrZero = aoore.GetMembers("ThrowIfNegativeOrZero").FirstOrDefault();
ISymbol? aooreThrowIfGreaterThan = aoore.GetMembers("ThrowIfGreaterThan").FirstOrDefault();
ISymbol? aooreThrowIfGreaterThanOrEqual = aoore.GetMembers("aooreThrowIfGreaterThanOrEqual").FirstOrDefault();
ISymbol? aooreThrowIfGreaterThanOrEqual = aoore.GetMembers("ThrowIfGreaterThanOrEqual").FirstOrDefault();
ISymbol? aooreThrowIfLessThan = aoore.GetMembers("ThrowIfLessThan").FirstOrDefault();
ISymbol? aooreThrowIfLessThanOrEqual = aoore.GetMembers("ThrowIfLessThanOrEqual").FirstOrDefault();
ISymbol? aooreThrowIfEqual = aoore.GetMembers("ThrowIfEqual").FirstOrDefault();
Expand Down Expand Up @@ -244,11 +244,24 @@ aooreThrowIfLessThan is not null || aooreThrowIfLessThanOrEqual is not null ||

if (additionalLocations.Length != 0 && !AvoidComparing(aooreParameter!))
{
context.ReportDiagnostic(condition.CreateDiagnostic(
UseArgumentOutOfRangeExceptionThrowIfRule,
additionalLocations,
properties: ImmutableDictionary<string, string?>.Empty.Add(MethodNamePropertyKey, methodName),
args: new object[] { nameof(ArgumentOutOfRangeException), methodName! }));
switch (methodName)
{
case "ThrowIfZero" when aooreThrowIfZero is not null:
case "ThrowIfNegative" when aooreThrowIfNegative is not null:
case "ThrowIfNegativeOrZero" when aooreThrowIfNegativeOrZero is not null:
case "ThrowIfGreaterThan" when aooreThrowIfGreaterThan is not null:
case "ThrowIfGreaterThanOrEqual" when aooreThrowIfGreaterThanOrEqual is not null:
case "ThrowIfLessThan" when aooreThrowIfLessThan is not null:
case "ThrowIfLessThanOrEqual" when aooreThrowIfLessThanOrEqual is not null:
case "ThrowIfEqual" when aooreThrowIfEqual is not null:
case "ThrowIfNotEqual" when aooreThrowIfNotEqual is not null:
context.ReportDiagnostic(condition.CreateDiagnostic(
UseArgumentOutOfRangeExceptionThrowIfRule,
additionalLocations,
properties: ImmutableDictionary<string, string?>.Empty.Add(MethodNamePropertyKey, methodName),
args: new object[] { nameof(ArgumentOutOfRangeException), methodName! }));
break;
}
}

static bool AvoidComparing(IParameterReferenceOperation p) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,50 @@ void Nullables(int? arg)
}.RunAsync();
}

[Fact]
public async Task ArgumentOutOfRangeExceptionThrowIf_NoDiagnosticForMissingHelper()
{
await new VerifyCS.Test()
{
LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp9,
TestCode =
@"
using System;

namespace System
{
public class ArgumentOutOfRangeException : Exception
{
public ArgumentOutOfRangeException(string paramName) { }
public ArgumentOutOfRangeException(string paramName, string message) { }
public static void ThrowIfZero<T>(T arg) { }
public static void ThrowIfNegative<T>(T arg) { }
public static void ThrowIfNegativeOrZero<T>(T arg) { }
public static void ThrowIfGreaterThan<T>(T arg, T other) { }
public static void ThrowIfGreaterThanOrEqual<T>(T arg, T other) { }
public static void ThrowIfLessThan<T>(T arg, T other) { }
public static void ThrowIfLessThanOrEqual<T>(T arg, T other) { }
}
}

class C
{
void M(int arg)
{
if (arg != 42)
{
throw new ArgumentOutOfRangeException(nameof(arg));
}
if (42 != arg)
{
throw new ArgumentOutOfRangeException(nameof(arg));
}
}
}
"
}.RunAsync();
}

[Fact]
public async Task ObjectDisposedExceptionThrowIf_DoesntExist_NoDiagnostics()
{
Expand Down