Skip to content

Commit 40fe029

Browse files
committed
Need to skip all non handled syntax kinds early to avoid looking them up in a dictionary and thus causing a key not found exception.
1 parent 172bc26 commit 40fe029

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1202UnitTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,43 @@ public async Task TestIncompleteMemberAsync()
929929
{
930930
private string Test;
931931
public string
932+
private string
933+
}
934+
";
935+
936+
// We don't care about the syntax errors.
937+
var expected = new[]
938+
{
939+
new DiagnosticResult
940+
{
941+
Id = "CS1585",
942+
Message = "Member modifier 'public' must precede the member type and name",
943+
Severity = DiagnosticSeverity.Error,
944+
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 5, 5) }
945+
},
946+
new DiagnosticResult
947+
{
948+
Id = "CS1519",
949+
Message = "Invalid token '}' in class, struct, or interface member declaration",
950+
Severity = DiagnosticSeverity.Error,
951+
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 6, 1) }
952+
}
953+
};
954+
955+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
956+
}
957+
958+
/// <summary>
959+
/// Verifies that the analyzer will properly handle incomplete members.
960+
/// </summary>
961+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
962+
[Fact]
963+
public async Task Issue150RegressionAsync()
964+
{
965+
string testCode = @"public class OuterType
966+
{
967+
private string Test;
968+
private string
932969
public string
933970
}
934971
";

StyleCop.Analyzers/StyleCop.Analyzers/OrderingRules/SA1202ElementsMustBeOrderedByAccess.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ private static void HandleMemberList(SyntaxNodeAnalysisContext context, SyntaxLi
122122
{
123123
var currentSyntaxKind = member.Kind();
124124
currentSyntaxKind = currentSyntaxKind == SyntaxKind.EventFieldDeclaration ? SyntaxKind.EventDeclaration : currentSyntaxKind;
125+
126+
// if the syntaxkind of this member (e.g. SyntaxKind.IncompleteMember) will not
127+
// be handled, skip early.
128+
if (!((ICollection<SyntaxKind>)MemberNames.Keys).Contains(currentSyntaxKind))
129+
{
130+
continue;
131+
}
132+
125133
AccessLevel currentAccessLevel;
126134
var modifiers = member.GetModifiers();
127135
if ((currentSyntaxKind == SyntaxKind.ConstructorDeclaration && modifiers.Any(SyntaxKind.StaticKeyword))

0 commit comments

Comments
 (0)