Skip to content

Commit b80f0c2

Browse files
authored
Merge pull request #3644 from CollinAlpert/fix_sa1414
Don't emit SA1414 for interface implementations
2 parents d45a445 + bda8c5a commit b80f0c2

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/MaintainabilityRules/SA1414CSharp7UnitTests.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace StyleCop.Analyzers.Test.CSharp7.MaintainabilityRules
77
{
8+
using System;
89
using System.Threading;
910
using System.Threading.Tasks;
1011
using Microsoft.CodeAnalysis.Testing;
@@ -117,5 +118,61 @@ public static explicit operator TestClass({typeExpression} p1)
117118

118119
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
119120
}
121+
122+
[Fact]
123+
public async Task ValidateTuplesFromInterfaceAsync()
124+
{
125+
const string testCode = @"
126+
using System.Collections.Generic;
127+
128+
namespace Test {
129+
class StringTupleComparer : IEqualityComparer<(string, string)>
130+
{
131+
public bool Equals((string, string) x, (string, string) y) => throw null;
132+
133+
public int GetHashCode((string, string) obj) => throw null;
134+
}
135+
}";
136+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
137+
}
138+
139+
[Fact]
140+
public async Task ValidateTuplesFromExplicitInterfaceImplementationAsync()
141+
{
142+
const string testCode = @"
143+
using System.Collections.Generic;
144+
145+
namespace Test {
146+
class StringTupleComparer : IEqualityComparer<(string, string)>
147+
{
148+
bool IEqualityComparer<(string, string)>.Equals((string, string) x, (string, string) y) => throw null;
149+
150+
int IEqualityComparer<(string, string)>.GetHashCode((string, string) obj) => throw null;
151+
}
152+
}";
153+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
154+
}
155+
156+
[Fact]
157+
public async Task ValidateTuplesFromBaseClassAsync()
158+
{
159+
const string testCode = @"
160+
namespace Test {
161+
class A : B
162+
{
163+
public override (string, string) Run((string, string) x) => throw null;
164+
165+
public override (int, int) Run((int, int) y) => throw null;
166+
}
167+
168+
abstract class B
169+
{
170+
public abstract ([|string|], [|string|]) Run(([|string|], [|string|]) x);
171+
172+
public virtual ([|int|], [|int|]) Run(([|int|], [|int|]) y) => throw null;
173+
}
174+
}";
175+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
176+
}
120177
}
121178
}

StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1414TupleTypesInSignaturesShouldHaveElementNames.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace StyleCop.Analyzers.MaintainabilityRules
77
{
88
using System;
99
using System.Collections.Immutable;
10+
using System.Linq;
1011
using Microsoft.CodeAnalysis;
1112
using Microsoft.CodeAnalysis.CSharp;
1213
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -62,6 +63,10 @@ private static void HandleMethodDeclaration(SyntaxNodeAnalysisContext context)
6263
}
6364

6465
var methodDeclaration = (MethodDeclarationSyntax)context.Node;
66+
if (methodDeclaration.Modifiers.Any(SyntaxKind.OverrideKeyword))
67+
{
68+
return;
69+
}
6570

6671
CheckType(context, methodDeclaration.ReturnType);
6772
CheckParameterList(context, methodDeclaration.ParameterList);
@@ -161,7 +166,7 @@ private static void CheckTupleType(SyntaxNodeAnalysisContext context, TupleTypeS
161166
{
162167
CheckType(context, tupleElementSyntax.Type);
163168

164-
if (tupleElementSyntax.Identifier.IsKind(SyntaxKind.None))
169+
if (tupleElementSyntax.Identifier.IsKind(SyntaxKind.None) && !NamedTypeHelpers.IsImplementingAnInterfaceMember(context.SemanticModel.GetDeclaredSymbol(context.Node)))
165170
{
166171
var location = tupleElementSyntax.SyntaxNode.GetLocation();
167172
context.ReportDiagnostic(Diagnostic.Create(Descriptor, location));

0 commit comments

Comments
 (0)