Skip to content

Commit 7f8a417

Browse files
authored
Merge pull request #2235 from vweijsters/fix-2189
Fixes issue in SA1313 where symbol is marked as override, but the ove…
2 parents 1c59b8b + 19e86c6 commit 7f8a417

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,42 @@ public TestClass(string text, bool flag)
531531
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
532532
}
533533

534+
/// <summary>
535+
/// Verify that an invalid method override will not produce a diagnostic nor crash the analyzer.
536+
/// This is a regression test for #2189
537+
/// </summary>
538+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
539+
[Fact]
540+
public async Task InvalidMethodOverrideShouldNotProduceDiagnosticAsync()
541+
{
542+
var testCode = @"
543+
namespace TestNamespace
544+
{
545+
public abstract class BaseClass
546+
{
547+
public abstract void TestMethod(int p1, int p2);
548+
}
549+
550+
public class TestClass : BaseClass
551+
{
552+
public override void TestMethod(int p1, X int P2)
553+
{
554+
}
555+
}
556+
}
557+
";
558+
DiagnosticResult[] expected =
559+
{
560+
this.CSharpCompilerError("CS0534").WithLocation(9, 18).WithMessage("'TestClass' does not implement inherited abstract member 'BaseClass.TestMethod(int, int)'"),
561+
this.CSharpCompilerError("CS0115").WithLocation(11, 30).WithMessage("'TestClass.TestMethod(int, X, int)': no suitable method found to override"),
562+
this.CSharpCompilerError("CS0246").WithLocation(11, 49).WithMessage("The type or namespace name 'X' could not be found (are you missing a using directive or an assembly reference?)"),
563+
this.CSharpCompilerError("CS1001").WithLocation(11, 51).WithMessage("Identifier expected"),
564+
this.CSharpCompilerError("CS1003").WithLocation(11, 51).WithMessage("Syntax error, ',' expected"),
565+
};
566+
567+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
568+
}
569+
534570
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
535571
{
536572
yield return new SA1313ParameterNamesMustBeginWithLowerCaseLetter();

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ private static bool NameMatchesAbstraction(ParameterSyntax syntax, SemanticModel
128128

129129
if (methodSymbol.IsOverride)
130130
{
131-
if (methodSymbol.OverriddenMethod.Parameters[index].Name == syntax.Identifier.ValueText)
131+
// OverridenMethod can be null in case of an invalid method declaration -> exit because there is no meaningful analysis to be done.
132+
if ((methodSymbol.OverriddenMethod == null) || (methodSymbol.OverriddenMethod.Parameters[index].Name == syntax.Identifier.ValueText))
132133
{
133134
return true;
134135
}

0 commit comments

Comments
 (0)