Skip to content

Commit 19e86c6

Browse files
committed
Fixes issue in SA1313 where symbol is marked as override, but the overriden method field is null
1 parent 6d80369 commit 19e86c6

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
@@ -529,6 +529,42 @@ public TestClass(string text, bool flag)
529529
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
530530
}
531531

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