Skip to content

Commit

Permalink
#2973: False warning from xUnit1012 when using params string[]?
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Jul 10, 2024
1 parent 798f46c commit 7f99c6a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,34 @@ public void TestMethod(params string[] args) { }
await Verify.VerifyAnalyzer(source);
}

[Fact]
public async Task MethodUsingNullParamsArgument_NonNullable()
{
var source = @"
public class TestClass {
[Xunit.Theory]
[Xunit.InlineData(null)]
public void TestMethod(params string[] args) { }
}";

await Verify.VerifyAnalyzer(source);
}

[Fact]
public async Task MethodUsingNullParamsArgument_Nullable()
{
var source = @"
#nullable enable
public class TestClass {
[Xunit.Theory]
[Xunit.InlineData(null)]
public void TestMethod(params string[]? args) { }
}";

await Verify.VerifyAnalyzer(LanguageVersion.CSharp9, source);
}

[Fact]
public async Task MethodUsingNormalAndParamsArgument()
{
Expand All @@ -40,6 +68,34 @@ public void TestMethod(string first, params string[] args) { }
await Verify.VerifyAnalyzer(source);
}

[Fact]
public async Task MethodUsingNormalAndNullParamsArgument_NonNullable()
{
var source = @"
public class TestClass {
[Xunit.Theory]
[Xunit.InlineData(""abc"", null)]
public void TestMethod(string first, params string[] args) { }
}";

await Verify.VerifyAnalyzer(source);
}

[Fact]
public async Task MethodUsingNormalAndNullParamsArgument_Nullable()
{
var source = @"
#nullable enable
public class TestClass {
[Xunit.Theory]
[Xunit.InlineData(""abc"", null)]
public void TestMethod(string first, params string[]? args) { }
}";

await Verify.VerifyAnalyzer(LanguageVersion.CSharp9, source);
}

[Fact]
public async Task MethodUsingNormalAndUnusedParamsArgument()
{
Expand Down
11 changes: 11 additions & 0 deletions src/xunit.analyzers/X1000/InlineDataMustMatchTheoryParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ public override void AnalyzeCompilation(

if (value.IsNull)
{
// Special case: if this is the only value of the params array, and it's null,
// and the params array itself is nullable, then this is allowable, since we'll
// end up passing null for the array itself.
if (paramsElementType is not null &&
valueIdx == values.Length - 1 &&
parameter.Type.NullableAnnotation == NullableAnnotation.Annotated)
{
valueIdx = values.Length;
break;
}

var isValueTypeParam =
paramsElementType is not null
? paramsElementType.IsValueType && paramsElementType.OriginalDefinition.SpecialType != SpecialType.System_Nullable_T
Expand Down

0 comments on commit 7f99c6a

Please sign in to comment.