Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SA1313 to also allow incorrect names in explicitly implemented methods from interfaces #3569

Merged
Show file tree
Hide file tree
Changes from all 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 @@ -277,6 +277,57 @@ public void Method(int Param1, int param2, int Other)
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3555, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3555")]
public async Task TestNoViolationOnExplicitlyImplementedInterfaceParameterNameAsync()
{
var testCode = @"
public interface ITest
{
void Method(int param1, int {|#0:Param2|});
}

public class Test : ITest
{
void ITest.Method(int param1, int Param2)
{
}
}";

var expected = new[]
{
Diagnostic().WithLocation(0).WithArguments("Param2"),
};

await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3555, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3555")]
public async Task TestViolationOnRenamedExplicitlyImplementedInterfaceParameterNameAsync()
{
var testCode = @"
public interface ITest
{
void Method(int param1, int {|#0:Param2|});
}

public class Test : ITest
{
public void Method(int param1, int {|#1:Other|})
{
}
}";

var expected = new[]
{
Diagnostic().WithLocation(0).WithArguments("Param2"),
Diagnostic().WithLocation(1).WithArguments("Other"),
};

await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
public async Task TestNoViolationOnAbstractParameterNameAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,23 @@ private static bool NameMatchesAbstraction(ParameterSyntax syntax, SemanticModel

if (methodSymbol.IsOverride)
{
// OverridenMethod can be null in case of an invalid method declaration -> exit because there is no meaningful analysis to be done.
// OverriddenMethod can be null in case of an invalid method declaration -> exit because there is no meaningful analysis to be done.
if ((methodSymbol.OverriddenMethod == null) || (methodSymbol.OverriddenMethod.Parameters[index].Name == syntax.Identifier.ValueText))
{
return true;
}
}
else if (methodSymbol.ExplicitInterfaceImplementations.Length > 0)
{
// Checking explicitly implemented interface members here because the code below will not handle them correctly
foreach (var interfaceMethod in methodSymbol.ExplicitInterfaceImplementations)
{
if (interfaceMethod.Parameters[index].Name == syntax.Identifier.ValueText)
{
return true;
}
}
}
else
{
var containingType = methodSymbol.ContainingType;
Expand Down