Skip to content

Commit 8d3c174

Browse files
vweijsterssharwell
andcommitted
Update StyleCop.Analyzers/StyleCop.Analyzers/OrderingRules/SA1201ElementsMustAppearInTheCorrectOrder.cs
Co-authored-by: Sam Harwell <sam@tunnelvisionlabs.com>
1 parent b3141d8 commit 8d3c174

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed
Lines changed: 21 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ private static void HandleMemberList(SyntaxNodeAnalysisContext context, Immutabl
297297
if (index > nextIndex)
298298
{
299299
// [Issue #3160] Added hardening here to make sure that this won't crash when working with invalid code.
300-
var nextElementMemberName = MemberNames.ContainsKey(nextElementSyntaxKind) ? MemberNames[nextElementSyntaxKind] : "<unknown>";
301-
var elementMemberName = MemberNames.ContainsKey(elementSyntaxKind) ? MemberNames[elementSyntaxKind] : "<unknown>";
300+
var nextElementMemberName = MemberNames.GetValueOrDefault(nextElementSyntaxKind, "<unknown>");
301+
var elementMemberName = MemberNames.GetValueOrDefault(elementSyntaxKind, "<unknown>");
302302

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

0 commit comments

Comments
 (0)