Skip to content

Commit

Permalink
Merge pull request #3179 from vweijsters/fix-3160
Browse files Browse the repository at this point in the history
Workaround for issue #3160
  • Loading branch information
sharwell authored Jul 30, 2020
2 parents d2bc750 + 8d3c174 commit aaa19c7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Helpers
{
using System.Collections.Generic;

internal static class DictionaryExtensions
{
public static TValue GetValueOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
where TKey : notnull
{
if (!dictionary.TryGetValue(key, out var value))
{
value = defaultValue;
}

return value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,11 @@ private static void HandleMemberList(SyntaxNodeAnalysisContext context, Immutabl

if (index > nextIndex)
{
context.ReportDiagnostic(Diagnostic.Create(Descriptor, NamedTypeHelpers.GetNameOrIdentifierLocation(members[i + 1]), MemberNames[nextElementSyntaxKind], MemberNames[elementSyntaxKind]));
// [Issue #3160] Added hardening here to make sure that this won't crash when working with invalid code.
var nextElementMemberName = MemberNames.GetValueOrDefault(nextElementSyntaxKind, "<unknown>");
var elementMemberName = MemberNames.GetValueOrDefault(elementSyntaxKind, "<unknown>");

context.ReportDiagnostic(Diagnostic.Create(Descriptor, NamedTypeHelpers.GetNameOrIdentifierLocation(members[i + 1]), nextElementMemberName, elementMemberName));
}
}
}
Expand Down

0 comments on commit aaa19c7

Please sign in to comment.