diff --git a/src/xunit.analyzers.tests/Analyzers/X1000/InlineDataMustMatchTheoryParametersTests.cs b/src/xunit.analyzers.tests/Analyzers/X1000/InlineDataMustMatchTheoryParametersTests.cs index 52d2f803..ce73a8a5 100644 --- a/src/xunit.analyzers.tests/Analyzers/X1000/InlineDataMustMatchTheoryParametersTests.cs +++ b/src/xunit.analyzers.tests/Analyzers/X1000/InlineDataMustMatchTheoryParametersTests.cs @@ -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() { @@ -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() { diff --git a/src/xunit.analyzers/X1000/InlineDataMustMatchTheoryParameters.cs b/src/xunit.analyzers/X1000/InlineDataMustMatchTheoryParameters.cs index 883ab8ce..562a359b 100644 --- a/src/xunit.analyzers/X1000/InlineDataMustMatchTheoryParameters.cs +++ b/src/xunit.analyzers/X1000/InlineDataMustMatchTheoryParameters.cs @@ -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