Skip to content

Commit

Permalink
Merge pull request #7140 from david-acker/bug-CA1065-explicit-interfa…
Browse files Browse the repository at this point in the history
…ce-implementations

CA1065: Consider explicit interface implementations of Equals, GetHashCode
  • Loading branch information
mavasani authored Jan 16, 2024
2 parents ae3bd81 + 01acfa3 commit 2e64f1f
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ 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)),

new MethodCategory(IsEqualsOverrideOrInterfaceImplementation, true,
new MethodCategory(IsEqualsOverrideOrInterfaceImplementation, false,
NoAllowedExceptionsRule),

new MethodCategory(IsComparisonOperator, true,
Expand Down Expand Up @@ -243,7 +243,8 @@ private static bool IsEqualsOverrideOrInterfaceImplementation(IMethodSymbol meth
/// </summary>
private static bool IsEqualsInterfaceImplementation(IMethodSymbol method, Compilation compilation)
{
if (method.Name != WellKnownMemberNames.ObjectEquals)
if (method.Name != WellKnownMemberNames.ObjectEquals &&
method.ExplicitInterfaceImplementations.Length == 0)
{
return false;
}
Expand Down Expand Up @@ -280,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 @@ -315,6 +315,24 @@ await VerifyCS.VerifyAnalyzerAsync(code,
GetCSharpNoExceptionsResultAt(8, 9, "Equals", "Exception"));
}

[Fact]
public async Task CSharpIEquatableEqualsAsExplicitInterfaceImplementationWithExceptionsAsync()
{
var code = @"
using System;
public class C : IEquatable<C>
{
bool IEquatable<C>.Equals(C obj)
{
throw new Exception();
}
}
";
await VerifyCS.VerifyAnalyzerAsync(code,
GetCSharpNoExceptionsResultAt(8, 9, "System.IEquatable<C>.Equals", "Exception"));
}

[Fact]
public async Task BasicIEquatableEqualsExceptionsAsync()
{
Expand All @@ -333,6 +351,24 @@ await VerifyVB.VerifyAnalyzerAsync(code,
GetBasicNoExceptionsResultAt(7, 9, "Equals", "Exception"));
}

[Fact]
public async Task BasicIEquatableEqualsAsExplicitInterfaceImplementationExceptionsAsync()
{
var code = @"
Imports System
Public Class C
Implements IEquatable(Of C)
Private Function Equals(obj As C) As Boolean Implements IEquatable(Of C).Equals
Throw New Exception()
End Function
End Class
";

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

[Fact]
public async Task CSharpIHashCodeProviderGetHashCodeAsync()
{
Expand All @@ -359,6 +395,32 @@ await VerifyCS.VerifyAnalyzerAsync(code,
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 @@ -384,6 +446,31 @@ await VerifyVB.VerifyAnalyzerAsync(code,
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 @@ -407,6 +494,29 @@ await VerifyCS.VerifyAnalyzerAsync(code,
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 @@ -422,6 +532,28 @@ Public Function GetHashCode(obj As C) As Integer Implements IEqualityComparer(Of
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 2e64f1f

Please sign in to comment.