Skip to content

Commit 74a601e

Browse files
authored
Merge pull request #3569 from bjornhellander/feature/sa1313-explicit-interface
Update SA1313 to also allow incorrect names in explicitly implemented methods from interfaces
2 parents 0521053 + 5aafbdc commit 74a601e

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1313UnitTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,57 @@ public void Method(int Param1, int param2, int Other)
277277
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
278278
}
279279

280+
[Fact]
281+
[WorkItem(3555, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3555")]
282+
public async Task TestNoViolationOnExplicitlyImplementedInterfaceParameterNameAsync()
283+
{
284+
var testCode = @"
285+
public interface ITest
286+
{
287+
void Method(int param1, int {|#0:Param2|});
288+
}
289+
290+
public class Test : ITest
291+
{
292+
void ITest.Method(int param1, int Param2)
293+
{
294+
}
295+
}";
296+
297+
var expected = new[]
298+
{
299+
Diagnostic().WithLocation(0).WithArguments("Param2"),
300+
};
301+
302+
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
303+
}
304+
305+
[Fact]
306+
[WorkItem(3555, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3555")]
307+
public async Task TestViolationOnRenamedExplicitlyImplementedInterfaceParameterNameAsync()
308+
{
309+
var testCode = @"
310+
public interface ITest
311+
{
312+
void Method(int param1, int {|#0:Param2|});
313+
}
314+
315+
public class Test : ITest
316+
{
317+
public void Method(int param1, int {|#1:Other|})
318+
{
319+
}
320+
}";
321+
322+
var expected = new[]
323+
{
324+
Diagnostic().WithLocation(0).WithArguments("Param2"),
325+
Diagnostic().WithLocation(1).WithArguments("Other"),
326+
};
327+
328+
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
329+
}
330+
280331
[Fact]
281332
public async Task TestNoViolationOnAbstractParameterNameAsync()
282333
{

StyleCop.Analyzers/StyleCop.Analyzers/NamingRules/SA1313ParameterNamesMustBeginWithLowerCaseLetter.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,23 @@ private static bool NameMatchesAbstraction(ParameterSyntax syntax, SemanticModel
137137

138138
if (methodSymbol.IsOverride)
139139
{
140-
// OverridenMethod can be null in case of an invalid method declaration -> exit because there is no meaningful analysis to be done.
140+
// OverriddenMethod can be null in case of an invalid method declaration -> exit because there is no meaningful analysis to be done.
141141
if ((methodSymbol.OverriddenMethod == null) || (methodSymbol.OverriddenMethod.Parameters[index].Name == syntax.Identifier.ValueText))
142142
{
143143
return true;
144144
}
145145
}
146+
else if (methodSymbol.ExplicitInterfaceImplementations.Length > 0)
147+
{
148+
// Checking explicitly implemented interface members here because the code below will not handle them correctly
149+
foreach (var interfaceMethod in methodSymbol.ExplicitInterfaceImplementations)
150+
{
151+
if (interfaceMethod.Parameters[index].Name == syntax.Identifier.ValueText)
152+
{
153+
return true;
154+
}
155+
}
156+
}
146157
else
147158
{
148159
var containingType = methodSymbol.ContainingType;

0 commit comments

Comments
 (0)