diff --git a/src/xunit.analyzers.tests/Analyzers/X2000/AssertEqualShouldNotBeUsedForCollectionSizeCheckTests.cs b/src/xunit.analyzers.tests/Analyzers/X2000/AssertEqualShouldNotBeUsedForCollectionSizeCheckTests.cs index 7c95123b..00452e77 100644 --- a/src/xunit.analyzers.tests/Analyzers/X2000/AssertEqualShouldNotBeUsedForCollectionSizeCheckTests.cs +++ b/src/xunit.analyzers.tests/Analyzers/X2000/AssertEqualShouldNotBeUsedForCollectionSizeCheckTests.cs @@ -5,9 +5,10 @@ public class AssertEqualShouldNotBeUsedForCollectionSizeCheckTests { - public static TheoryData CollectionsWithExceptionThrowingGetEnumeratorMethod = new() + public static TheoryData AllowedCollections = new() { "new System.ArraySegment().Count", + "Microsoft.Extensions.Primitives.StringValues.Empty.Count", }; public static TheoryData Collections = new() @@ -42,8 +43,8 @@ public class AssertEqualShouldNotBeUsedForCollectionSizeCheckTests }; [Theory] - [MemberData(nameof(CollectionsWithExceptionThrowingGetEnumeratorMethod))] - public async void DoesNotFindWarningForCollectionsWithExceptionThrowingGetEnumeratorMethod(string collection) + [MemberData(nameof(AllowedCollections))] + public async void AllowedCollection_DoesNotTrigger(string collection) { var source = $@" using System.Linq; diff --git a/src/xunit.analyzers.tests/Utility/CodeAnalyzerHelper.cs b/src/xunit.analyzers.tests/Utility/CodeAnalyzerHelper.cs index 6c7a6b56..b6876512 100644 --- a/src/xunit.analyzers.tests/Utility/CodeAnalyzerHelper.cs +++ b/src/xunit.analyzers.tests/Utility/CodeAnalyzerHelper.cs @@ -31,6 +31,7 @@ static CodeAnalyzerHelper() CurrentXunitV2 = defaultAssemblies.AddPackages( ImmutableArray.Create( + new PackageIdentity("Microsoft.Extensions.Primitives", "8.0.0"), new PackageIdentity("System.Collections.Immutable", "1.6.0"), new PackageIdentity("System.Threading.Tasks.Extensions", "4.5.4"), new PackageIdentity("xunit.abstractions", "2.0.3"), @@ -41,6 +42,7 @@ static CodeAnalyzerHelper() CurrentXunitV2RunnerUtility = defaultAssemblies.AddPackages( ImmutableArray.Create( + new PackageIdentity("Microsoft.Extensions.Primitives", "8.0.0"), new PackageIdentity("System.Collections.Immutable", "1.6.0"), new PackageIdentity("System.Threading.Tasks.Extensions", "4.5.4"), new PackageIdentity("xunit.abstractions", "2.0.3"), @@ -51,6 +53,7 @@ static CodeAnalyzerHelper() CurrentXunitV3 = defaultAssemblies.AddPackages( ImmutableArray.Create( new PackageIdentity("Microsoft.Bcl.AsyncInterfaces", "8.0.0"), + new PackageIdentity("Microsoft.Extensions.Primitives", "8.0.0"), new PackageIdentity("System.Threading.Tasks.Extensions", "4.5.4"), new PackageIdentity("System.Text.Json", "8.0.0"), new PackageIdentity("xunit.v3.assert", "0.1.1-pre.342"), @@ -62,6 +65,7 @@ static CodeAnalyzerHelper() CurrentXunitV3RunnerUtility = defaultAssemblies.AddPackages( ImmutableArray.Create( new PackageIdentity("Microsoft.Bcl.AsyncInterfaces", "8.0.0"), + new PackageIdentity("Microsoft.Extensions.Primitives", "8.0.0"), new PackageIdentity("System.Threading.Tasks.Extensions", "4.5.4"), new PackageIdentity("System.Text.Json", "8.0.0"), new PackageIdentity("xunit.v3.common", "0.1.1-pre.342"), diff --git a/src/xunit.analyzers.tests/xunit.analyzers.tests.csproj b/src/xunit.analyzers.tests/xunit.analyzers.tests.csproj index 7a21ff86..217b2a81 100644 --- a/src/xunit.analyzers.tests/xunit.analyzers.tests.csproj +++ b/src/xunit.analyzers.tests/xunit.analyzers.tests.csproj @@ -8,6 +8,7 @@ + diff --git a/src/xunit.analyzers/X2000/AssertEqualShouldNotBeUsedForCollectionSizeCheck.cs b/src/xunit.analyzers/X2000/AssertEqualShouldNotBeUsedForCollectionSizeCheck.cs index 0cf8dc76..3de3b574 100644 --- a/src/xunit.analyzers/X2000/AssertEqualShouldNotBeUsedForCollectionSizeCheck.cs +++ b/src/xunit.analyzers/X2000/AssertEqualShouldNotBeUsedForCollectionSizeCheck.cs @@ -13,9 +13,12 @@ namespace Xunit.Analyzers; [DiagnosticAnalyzer(LanguageNames.CSharp)] public class AssertEqualShouldNotBeUsedForCollectionSizeCheck : AssertUsageAnalyzerBase { - static readonly HashSet collectionTypesWithExceptionThrowingGetEnumeratorMethod = new() + static readonly HashSet allowedCollections = new() { + // ArraySegment.GetEnumerator() can throw "System.ArraySegment", + // StringValues has an implicit string conversion that's preferred by the compiler, https://github.com/xunit/xunit/issues/2859 + "Microsoft.Extensions.Primitives.StringValues", }; static readonly HashSet sizeMethods = new() { @@ -73,7 +76,7 @@ protected override void AnalyzeInvocation( if (symbol is null) return; - if (IsCollectionsWithExceptionThrowingGetEnumeratorMethod(symbol) || + if (IsAllowedCollection(symbol) || !IsWellKnownSizeMethod(symbol) && !IsICollectionCountProperty(context, symbol) && !IsICollectionOfTCountProperty(context, symbol) && @@ -114,8 +117,8 @@ static string GetReplacementMethodName( return methodName == Constants.Asserts.Equal ? Constants.Asserts.Empty : Constants.Asserts.NotEmpty; } - static bool IsCollectionsWithExceptionThrowingGetEnumeratorMethod(ISymbol symbol) => - collectionTypesWithExceptionThrowingGetEnumeratorMethod.Contains(symbol.ContainingType.ConstructedFrom.ToDisplayString()); + static bool IsAllowedCollection(ISymbol symbol) => + allowedCollections.Contains(symbol.ContainingType.ConstructedFrom.ToDisplayString()); static bool IsWellKnownSizeMethod(ISymbol symbol) => sizeMethods.Contains(symbol.OriginalDefinition.ToDisplayString());