diff --git a/src/xunit.analyzers.tests/Analyzers/X2000/SetEqualityAnalyzerTests.cs b/src/xunit.analyzers.tests/Analyzers/X2000/SetEqualityAnalyzerTests.cs index 401095de..adc04d6c 100644 --- a/src/xunit.analyzers.tests/Analyzers/X2000/SetEqualityAnalyzerTests.cs +++ b/src/xunit.analyzers.tests/Analyzers/X2000/SetEqualityAnalyzerTests.cs @@ -234,6 +234,29 @@ public void TestMethod() {{ await Verify.VerifyAnalyzer(LanguageVersion.CSharp7, new[] { code, customSetAndComparer }); } + [Fact] + public async void CastedSet_DoesNotTrigger() + { + var code = @" +using Xunit; +using System.Collections.Generic; + +public class TestClass { + [Fact] + public void TestMethod() { + var expected = new HashSet { ""bar"", ""foo"" }; + var actual = new HashSet { ""foo"", ""bar"" }; + + Assert.Equal(expected, actual); + Assert.Equal(expected, (ISet)actual); + Assert.Equal((ISet)expected, actual); + Assert.Equal((ISet)expected, (ISet)actual); + } +}"; + + await Verify.VerifyAnalyzer(code); + } + public static MatrixTheoryData MethodAndTypeAndInitializer => new( new[] { "Equal", "NotEqual" }, diff --git a/src/xunit.analyzers/X2000/SetEqualityAnalyzer.cs b/src/xunit.analyzers/X2000/SetEqualityAnalyzer.cs index 3416538a..9d6a755e 100644 --- a/src/xunit.analyzers/X2000/SetEqualityAnalyzer.cs +++ b/src/xunit.analyzers/X2000/SetEqualityAnalyzer.cs @@ -56,6 +56,7 @@ protected override void AnalyzeInvocation( var interface0Type = collection0Type .AllInterfaces + .Concat(new[] { collection0Type }) .Where(i => i.IsGenericType) .FirstOrDefault(i => setInterfaces.Contains(i.ConstructUnboundGenericType())); @@ -64,6 +65,7 @@ protected override void AnalyzeInvocation( var interface1Type = collection1Type .AllInterfaces + .Concat(new[] { collection1Type }) .Where(i => i.IsGenericType) .FirstOrDefault(i => setInterfaces.Contains(i.ConstructUnboundGenericType()));