From 5d9f063d08c0d18777aff2059716b68f1afe394b Mon Sep 17 00:00:00 2001 From: Brad Wilson Date: Sun, 25 Aug 2024 13:46:34 -0700 Subject: [PATCH] xunit/xunit#3000: xUnit1012 when using [InlineData()] --- ...InlineDataMustMatchTheoryParametersTests.cs | 18 ++++++++++++++++++ .../InlineDataMustMatchTheoryParameters.cs | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/src/xunit.analyzers.tests/Analyzers/X1000/InlineDataMustMatchTheoryParametersTests.cs b/src/xunit.analyzers.tests/Analyzers/X1000/InlineDataMustMatchTheoryParametersTests.cs index fd8cfbc4..57a6157f 100644 --- a/src/xunit.analyzers.tests/Analyzers/X1000/InlineDataMustMatchTheoryParametersTests.cs +++ b/src/xunit.analyzers.tests/Analyzers/X1000/InlineDataMustMatchTheoryParametersTests.cs @@ -313,6 +313,24 @@ public void TestMethod(byte[] input) { } await Verify.VerifyAnalyzer(source); } + + // https://github.com/xunit/xunit/issues/3000 + [Fact] + public async Task DecimalValue() + { + var source = /* lang=c#-test */ """ + using Xunit; + + public sealed class ReproClass { + [Theory] + [InlineData({|CS0182:0.1m|})] + public void ReproMethod(decimal m) + { } + } + """; + + await Verify.VerifyAnalyzer(source); + } } public class X1009_TooFewValues diff --git a/src/xunit.analyzers/X1000/InlineDataMustMatchTheoryParameters.cs b/src/xunit.analyzers/X1000/InlineDataMustMatchTheoryParameters.cs index 562a359b..ba1e656b 100644 --- a/src/xunit.analyzers/X1000/InlineDataMustMatchTheoryParameters.cs +++ b/src/xunit.analyzers/X1000/InlineDataMustMatchTheoryParameters.cs @@ -97,6 +97,11 @@ public override void AnalyzeCompilation( var parameter = method.Parameters[paramIdx]; var value = values[valueIdx]; + // If the value isn't legal (malformed or illegal type), then just skip validation and let + // the compiler report the problem. + if (value.Kind == TypedConstantKind.Error) + continue; + // If the parameter type is object, everything is compatible, though we still need to check for nullability if (SymbolEqualityComparer.Default.Equals(parameter.Type, compilation.ObjectType) && (!value.IsNull || parameter.Type.NullableAnnotation != NullableAnnotation.NotAnnotated))