Skip to content
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()
{
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);
}

[Fact]
public Task ValidateTuplesFromExplicitInterfaceImplementationAsync()
{
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);
}

[Fact]
public Task ValidateTuplesFromBaseClassAsync()
{
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);
}
}
}
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);
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;
}
}

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