Skip to content

Commit

Permalink
Support null! suppression of elements in a params argument
Browse files Browse the repository at this point in the history
  • Loading branch information
manfred-brands committed Nov 15, 2023
1 parent 30e6172 commit b48dee4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,32 @@ public void Test(object a) { }
RoslynAssert.Valid(this.analyzer, testCode);
}

[Test]
public void AnalyzeWhenArgumentPassesNullToNonNullableParamsReferenceType()
{
var testCode = TestUtility.WrapClassInNamespaceAndAddUsing(@"
public sealed class AnalyzeWhenArgumentPassesSuppressedNullToNonNullableType
{
[TestCase(""Hello"", null)]
public void Test(params string[] a) { }
}");
RoslynAssert.Diagnostics(this.analyzer,
ExpectedDiagnostic.Create(AnalyzerIdentifiers.TestCaseParameterTypeMismatchUsage),
testCode);
}

[Test]
public void AnalyzeWhenArgumentPassesSuppressedNullToNonNullableParamsReferenceType()
{
var testCode = TestUtility.WrapClassInNamespaceAndAddUsing(@"
public sealed class AnalyzeWhenArgumentPassesSuppressedNullToNonNullableType
{
[TestCase(""Hello"", null!)]
public void Test(params string[] a) { }
}");
RoslynAssert.Valid(this.analyzer, testCode);
}

[Test]
public void AnalyzeWhenArgumentPassesValueToNullableType()
{
Expand Down
29 changes: 17 additions & 12 deletions src/nunit.analyzers/TestCaseUsage/TestCaseUsageAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,27 +180,32 @@ private static void AnalyzePositionalArgumentsAndParameters(

ITypeSymbol? argumentType = attributeArgument.Type;

var argumentTypeMatchesParameterType = attributeArgument.CanAssignTo(
methodParameterType,
context.Compilation,
allowImplicitConversion: true,
allowEnumToUnderlyingTypeConversion: true,
suppressNullableWarning: argumentSyntax.IsSuppressNullableWarning());

if (methodParameterParamsType is null && argumentTypeMatchesParameterType)
continue;
if (i < methodParameters.Length)
{
// Test against the actual parameter definition.
// In case an array is passed to a params parameter.
var argumentTypeMatchesParameterType = attributeArgument.CanAssignTo(
methodParameterType,
context.Compilation,
allowImplicitConversion: true,
allowEnumToUnderlyingTypeConversion: true,
suppressNullableWarning: argumentSyntax.IsSuppressNullableWarning());

if (argumentTypeMatchesParameterType)
continue;
}

if (methodParameterParamsType is not null)
{
// Test against the element type of the params parameter.
var argumentTypeMatchesElementType = attributeArgument.CanAssignTo(
methodParameterParamsType,
context.Compilation,
allowImplicitConversion: true,
allowEnumToUnderlyingTypeConversion: true,
suppressNullableWarning: false);
suppressNullableWarning: argumentSyntax.IsSuppressNullableWarning());

if (argumentTypeMatchesElementType ||
(argumentTypeMatchesParameterType && (argumentType is not null || !methodParameterParamsType.IsValueType)))
if (argumentTypeMatchesElementType)
{
continue;
}
Expand Down

0 comments on commit b48dee4

Please sign in to comment.