Skip to content

Commit

Permalink
Handle explicit interface implementations of GetHashCode
Browse files Browse the repository at this point in the history
  • Loading branch information
david-acker committed Jan 12, 2024
1 parent 43456c1 commit 01acfa3
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private static List<MethodCategory> GetMethodCategories(Compilation compilation)
compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemNotSupportedException),
compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemArgumentException)),

new MethodCategory(IsGetHashCodeInterfaceImplementation, true,
new MethodCategory(IsGetHashCodeInterfaceImplementation, false,
HasAllowedExceptionsRule,
compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemArgumentException)),

Expand Down Expand Up @@ -281,7 +281,8 @@ private static bool IsEqualsInterfaceImplementation(IMethodSymbol method, Compil
/// <returns></returns>
private static bool IsGetHashCodeInterfaceImplementation(IMethodSymbol method, Compilation compilation)
{
if (method.Name != WellKnownMemberNames.ObjectGetHashCode)
if (method.Name != WellKnownMemberNames.ObjectGetHashCode &&
method.ExplicitInterfaceImplementations.Length == 0)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,32 @@ public int GetHashCode(object obj)
GetCSharpAllowedExceptionsResultAt(8, 9, "GetHashCode", "Exception"));
}

[Fact]
public async Task CSharpIHashCodeProviderGetHashCodeAsExplicitInterfaceImplementationAsync()
{
var code = @"
using System;
using System.Collections;
public class C : IHashCodeProvider
{
int IHashCodeProvider.GetHashCode(object obj)
{
throw new Exception();
}
}
public class D : IHashCodeProvider
{
int IHashCodeProvider.GetHashCode(object obj)
{
throw new ArgumentException(""obj""); // this is fine.
}
}
";
await VerifyCS.VerifyAnalyzerAsync(code,
GetCSharpAllowedExceptionsResultAt(8, 9, "System.Collections.IHashCodeProvider.GetHashCode", "Exception"));
}

[Fact]
public async Task BasicIHashCodeProviderGetHashCodeAsync()
{
Expand All @@ -420,6 +446,31 @@ End Class
GetBasicAllowedExceptionsResultAt(7, 9, "GetHashCode", "Exception"));
}

[Fact]
public async Task BasicIHashCodeProviderGetHashCodeAsExplicitInterfaceImplementationAsync()
{
var code = @"
Imports System
Imports System.Collections
Public Class C
Implements IHashCodeProvider
Private Function GetHashCode(obj As Object) As Integer Implements IHashCodeProvider.GetHashCode
Throw New Exception()
End Function
End Class
Public Class D
Implements IHashCodeProvider
Private Function GetHashCode(obj As Object) As Integer Implements IHashCodeProvider.GetHashCode
Throw New ArgumentException() ' This is fine.
End Function
End Class
";

await VerifyVB.VerifyAnalyzerAsync(code,
GetBasicAllowedExceptionsResultAt(7, 9, "GetHashCode", "Exception"));
}

[Fact]
public async Task CSharpIEqualityComparerAsync()
{
Expand All @@ -443,6 +494,29 @@ public int GetHashCode(C obj)
GetCSharpAllowedExceptionsResultAt(12, 9, "GetHashCode", "Exception"));
}

[Fact]
public async Task CSharpIEqualityComparerWithExplicitInterfaceImplementationsAsync()
{
var code = @"
using System;
using System.Collections.Generic;
public class C : IEqualityComparer<C>
{
bool IEqualityComparer<C>.Equals(C obj1, C obj2)
{
throw new Exception();
}
int IEqualityComparer<C>.GetHashCode(C obj)
{
throw new Exception();
}
}
";
await VerifyCS.VerifyAnalyzerAsync(code,
GetCSharpNoExceptionsResultAt(8, 9, "System.Collections.Generic.IEqualityComparer<C>.Equals", "Exception"),
GetCSharpAllowedExceptionsResultAt(12, 9, "System.Collections.Generic.IEqualityComparer<C>.GetHashCode", "Exception"));
}

[Fact]
public async Task BasicIEqualityComparerAsync()
{
Expand All @@ -458,6 +532,28 @@ End Function
Throw New Exception()
End Function
End Class
";

await VerifyVB.VerifyAnalyzerAsync(code,
GetBasicNoExceptionsResultAt(7, 9, "Equals", "Exception"),
GetBasicAllowedExceptionsResultAt(10, 9, "GetHashCode", "Exception"));
}

[Fact]
public async Task BasicIEqualityComparerWithExplicitInterfaceImplementationsAsync()
{
var code = @"
Imports System
Imports System.Collections.Generic
Public Class C
Implements IEqualityComparer(Of C)
Private Function Equals(obj1 As C, obj2 As C) As Boolean Implements IEqualityComparer(Of C).Equals
Throw New Exception()
End Function
Private Function GetHashCode(obj As C) As Integer Implements IEqualityComparer(Of C).GetHashCode
Throw New Exception()
End Function
End Class
";

await VerifyVB.VerifyAnalyzerAsync(code,
Expand Down

0 comments on commit 01acfa3

Please sign in to comment.