Skip to content

Commit b9fa57a

Browse files
committed
Merge pull request #1547 from Noryoko/fix-1529
Add additional null checks to SA1313 analyzer
2 parents 7fd1f09 + fa32e52 commit b9fa57a

2 files changed

Lines changed: 42 additions & 8 deletions

File tree

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,37 @@ public void MethodName(int _)
474474
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
475475
}
476476

477+
/// <summary>
478+
/// This is a regression test for DotNetAnalyzers/StyleCopAnalyzers#1529:
479+
/// https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1529
480+
/// </summary>
481+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
482+
[Fact]
483+
public async Task TestInheritedInterfacesWithOverloadedMembersAsync()
484+
{
485+
var testCode = @"
486+
public interface ITest
487+
{
488+
void Method(int Param1, int param2, int Param3);
489+
void Method();
490+
}
491+
492+
public interface IEmptyInterface { }
493+
494+
public interface IDerivedTest : ITest, IEmptyInterface
495+
{
496+
void Method(int Param1, int param2, int param3);
497+
}";
498+
var expected = new[]
499+
{
500+
this.CSharpDiagnostic().WithLocation(4, 21).WithArguments("Param1"),
501+
this.CSharpDiagnostic().WithLocation(4, 45).WithArguments("Param3"),
502+
this.CSharpDiagnostic().WithLocation(12, 21).WithArguments("Param1"),
503+
};
504+
505+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
506+
}
507+
477508
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
478509
{
479510
yield return new SA1313ParameterNamesMustBeginWithLowerCaseLetter();

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,20 @@ private static bool NameMatchesAbstraction(ParameterSyntax syntax, SemanticModel
142142
}
143143
else
144144
{
145-
var implementedInterfaces = methodSymbol.ContainingType.Interfaces;
146-
if (implementedInterfaces.Length != 0)
145+
var containingType = methodSymbol.ContainingType;
146+
if (containingType == null)
147147
{
148-
foreach (var @interface in implementedInterfaces)
148+
return false;
149+
}
150+
151+
var implementedInterfaces = containingType.Interfaces;
152+
foreach (var implementedInterface in implementedInterfaces)
153+
{
154+
foreach (var member in implementedInterface.GetMembers(methodSymbol.Name).OfType<IMethodSymbol>())
149155
{
150-
foreach (var member in @interface.GetMembers(methodSymbol.Name).OfType<IMethodSymbol>())
156+
if (containingType.FindImplementationForInterfaceMember(member) == methodSymbol)
151157
{
152-
if (methodSymbol.ContainingType.FindImplementationForInterfaceMember(member).Equals(methodSymbol))
153-
{
154-
return member.Parameters[index].Name == syntax.Identifier.ValueText;
155-
}
158+
return member.Parameters[index].Name == syntax.Identifier.ValueText;
156159
}
157160
}
158161
}

0 commit comments

Comments
 (0)