Skip to content

Commit aaa19c7

Browse files
authored
Merge pull request #3179 from vweijsters/fix-3160
Workaround for issue #3160
2 parents d2bc750 + 8d3c174 commit aaa19c7

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Helpers
5+
{
6+
using System.Collections.Generic;
7+
8+
internal static class DictionaryExtensions
9+
{
10+
public static TValue GetValueOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
11+
where TKey : notnull
12+
{
13+
if (!dictionary.TryGetValue(key, out var value))
14+
{
15+
value = defaultValue;
16+
}
17+
18+
return value;
19+
}
20+
}
21+
}

StyleCop.Analyzers/StyleCop.Analyzers/OrderingRules/SA1201ElementsMustAppearInTheCorrectOrder.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,11 @@ private static void HandleMemberList(SyntaxNodeAnalysisContext context, Immutabl
296296

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

0 commit comments

Comments
 (0)