Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't emit SA1414 for interface implementations #3644

Merged
merged 6 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace StyleCop.Analyzers.Test.CSharp7.MaintainabilityRules
{
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
Expand Down Expand Up @@ -117,5 +118,61 @@ public static explicit operator TestClass({typeExpression} p1)

await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
public Task ValidateTuplesFromInterfaceAsync()
sharwell marked this conversation as resolved.
Show resolved Hide resolved
{
const string testCode = @"
using System.Collections.Generic;

namespace Test {
class StringTupleComparer : IEqualityComparer<(string, string)>
{
public bool Equals((string, string) x, (string, string) y) => throw null;

public int GetHashCode((string, string) obj) => throw null;
}
}";
return VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, default);
sharwell marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
public Task ValidateTuplesFromExplicitInterfaceImplementationAsync()
sharwell marked this conversation as resolved.
Show resolved Hide resolved
{
const string testCode = @"
using System.Collections.Generic;

namespace Test {
class StringTupleComparer : IEqualityComparer<(string, string)>
{
bool IEqualityComparer<(string, string)>.Equals((string, string) x, (string, string) y) => throw null;

int IEqualityComparer<(string, string)>.GetHashCode((string, string) obj) => throw null;
}
}";
return VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, default);
sharwell marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
public Task ValidateTuplesFromBaseClassAsync()
sharwell marked this conversation as resolved.
Show resolved Hide resolved
{
const string testCode = @"
namespace Test {
class A : B
{
public override (string, string) Run((string, string) x) => throw null;

public override (int, int) Run((int, int) y) => throw null;
}

abstract class B
{
public abstract ([|string|], [|string|]) Run(([|string|], [|string|]) x);

public virtual ([|int|], [|int|]) Run(([|int|], [|int|]) y) => throw null;
}
}";
return VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, default);
sharwell marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace StyleCop.Analyzers.MaintainabilityRules
{
using System;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand Down Expand Up @@ -62,6 +63,25 @@ private static void HandleMethodDeclaration(SyntaxNodeAnalysisContext context)
}

var methodDeclaration = (MethodDeclarationSyntax)context.Node;
if (methodDeclaration.Modifiers.Any(SyntaxKind.OverrideKeyword))
{
return;
}

var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodDeclaration);
sharwell marked this conversation as resolved.
Show resolved Hide resolved
var containingType = methodSymbol.ContainingType;
if (containingType is null || methodSymbol.ExplicitInterfaceImplementations.Length > 0)
{
return;
}

foreach (var member in containingType.AllInterfaces.SelectMany(i => i.GetMembers(methodSymbol.Name).OfType<IMethodSymbol>()))
{
if (methodSymbol.Equals(containingType.FindImplementationForInterfaceMember(member)))
{
return;
}
}
sharwell marked this conversation as resolved.
Show resolved Hide resolved

CheckType(context, methodDeclaration.ReturnType);
CheckParameterList(context, methodDeclaration.ParameterList);
Expand Down