Skip to content

Commit

Permalink
Merge pull request DotNetAnalyzers#3644 from CollinAlpert/fix_sa1414
Browse files Browse the repository at this point in the history
Don't emit SA1414 for interface implementations
  • Loading branch information
sharwell authored May 18, 2023
2 parents d45a445 + bda8c5a commit b80f0c2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
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 async 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;
}
}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
public async 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;
}
}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
public async 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;
}
}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
}
}
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,10 @@ private static void HandleMethodDeclaration(SyntaxNodeAnalysisContext context)
}

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

CheckType(context, methodDeclaration.ReturnType);
CheckParameterList(context, methodDeclaration.ParameterList);
Expand Down Expand Up @@ -161,7 +166,7 @@ private static void CheckTupleType(SyntaxNodeAnalysisContext context, TupleTypeS
{
CheckType(context, tupleElementSyntax.Type);

if (tupleElementSyntax.Identifier.IsKind(SyntaxKind.None))
if (tupleElementSyntax.Identifier.IsKind(SyntaxKind.None) && !NamedTypeHelpers.IsImplementingAnInterfaceMember(context.SemanticModel.GetDeclaredSymbol(context.Node)))
{
var location = tupleElementSyntax.SyntaxNode.GetLocation();
context.ReportDiagnostic(Diagnostic.Create(Descriptor, location));
Expand Down

0 comments on commit b80f0c2

Please sign in to comment.