Skip to content

Commit 50d2e33

Browse files
authored
Add Enum static extensions (#79546)
1 parent fadd60c commit 50d2e33

File tree

22 files changed

+50
-31
lines changed

22 files changed

+50
-31
lines changed

src/Analyzers/Core/Analyzers/RemoveUnnecessarySuppressions/SuppressMessageAttributeState.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ private static ImmutableDictionary<string, TargetScope> CreateTargetScopesMap()
2626
{
2727
var builder = ImmutableDictionary.CreateBuilder<string, TargetScope>(StringComparer.OrdinalIgnoreCase);
2828

29-
#pragma warning disable CS8605 // Unboxing a possibly null value.
30-
foreach (TargetScope targetScope in Enum.GetValues(typeof(TargetScope)))
31-
#pragma warning restore CS8605 // Unboxing a possibly null value.
29+
foreach (var targetScope in Enum.GetValues<TargetScope>())
3230
{
3331
if (targetScope == TargetScope.None)
3432
{

src/CodeStyle/Tools/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ private static void CreateGlobalConfigFiles(string language, string outputDir, I
6666
}
6767

6868
var configDir = Path.Combine(outputDir, "config");
69-
foreach (var analysisMode in Enum.GetValues(typeof(AnalysisMode)))
69+
foreach (var analysisMode in Enum.GetValues<AnalysisMode>())
7070
{
7171
CreateGlobalconfig(
7272
configDir,
7373
$"AnalysisLevelStyle_{analysisMode}.globalconfig".ToLowerInvariant(),
74-
(AnalysisMode)analysisMode!,
74+
analysisMode,
7575
allRulesById);
7676
}
7777

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
#nullable enable
6+
7+
namespace System;
8+
9+
internal static partial class RoslynEnumExtensions
10+
{
11+
#if NET // binary compatibility
12+
public static TEnum[] GetValues<TEnum>() where TEnum : struct, Enum
13+
=> Enum.GetValues<TEnum>();
14+
15+
public static string[] GetNames<TEnum>() where TEnum : struct, Enum
16+
=> Enum.GetNames<TEnum>();
17+
#else
18+
extension(Enum)
19+
{
20+
public static TEnum[] GetValues<TEnum>() where TEnum : struct, Enum
21+
=> (TEnum[])Enum.GetValues(typeof(TEnum));
22+
23+
public static string[] GetNames<TEnum>() where TEnum : struct, Enum
24+
=> Enum.GetNames(typeof(TEnum));
25+
}
26+
#endif
27+
}

src/EditorFeatures/CSharpTest/Completion/CompletionProviders/ExtensionMethodImportCompletionProviderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public enum ReferenceType
4040

4141
private static IEnumerable<object[]> CombineWithReferenceTypeData(IEnumerable<List<object>> data)
4242
{
43-
foreach (var refKind in Enum.GetValues(typeof(ReferenceType)))
43+
foreach (var refKind in Enum.GetValues<ReferenceType>())
4444
{
4545
foreach (var d in data)
4646
{

src/EditorFeatures/CSharpTest/LanguageServer/CSharpLspBuildOnlyDiagnosticsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected override ImmutableArray<string> ExpectedDiagnosticCodes
2121
{
2222
get
2323
{
24-
var errorCodes = Enum.GetValues(typeof(ErrorCode));
24+
var errorCodes = Enum.GetValues<ErrorCode>();
2525
var supported = new CSharpCompilerDiagnosticAnalyzer().GetSupportedErrorCodes();
2626
var builder = ImmutableArray.CreateBuilder<string>();
2727
foreach (int errorCode in errorCodes)

src/EditorFeatures/Core/Logging/FunctionIdOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private static Option2<bool> CreateOption(FunctionId id)
3535
}
3636

3737
private static IEnumerable<FunctionId> GetFunctionIds()
38-
=> Enum.GetValues(typeof(FunctionId)).Cast<FunctionId>();
38+
=> Enum.GetValues<FunctionId>();
3939

4040
public static IEnumerable<IOption2> GetOptions()
4141
=> GetFunctionIds().Select(GetOption);

src/EditorFeatures/Test/EditorConfigSettings/Data/CodeStyleSettingsTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public static void CodeStyleSettingEnumFactory(DayOfWeek defaultValue)
6666
description: "TestDesciption",
6767
options,
6868
updater: null!,
69-
enumValues: (DayOfWeek[])Enum.GetValues(typeof(DayOfWeek)),
70-
valueDescriptions: Enum.GetNames(typeof(DayOfWeek)));
69+
enumValues: Enum.GetValues<DayOfWeek>(),
70+
valueDescriptions: Enum.GetNames<DayOfWeek>());
7171

7272
Assert.Equal(string.Empty, setting.Category);
7373
Assert.Equal("TestDesciption", setting.Description);

src/ExpressionEvaluator/CSharp/Test/ResultProvider/ResultProviderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class ResultProviderTests
1515
[Fact]
1616
public void DkmEvaluationFlagsConflict()
1717
{
18-
var values = Enum.GetValues(typeof(DkmEvaluationFlags)).Cast<DkmEvaluationFlags>().ToArray();
18+
var values = Enum.GetValues<DkmEvaluationFlags>();
1919
Assert.False(values.Contains(ResultProvider.NoResults));
2020
Assert.False(values.Contains(ResultProvider.NotRoot));
2121
}

src/Features/CSharpTest/Diagnostics/Suppression/RemoveUnnecessaryPragmaSuppressionsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ protected override ImmutableArray<string> UnsupportedDiagnosticIds
136136
{
137137
get
138138
{
139-
var errorCodes = Enum.GetValues(typeof(ErrorCode));
139+
var errorCodes = Enum.GetValues<ErrorCode>();
140140
var supported = ((CSharpCompilerDiagnosticAnalyzer)OtherAnalyzers[0]).GetSupportedErrorCodes();
141141
using var _ = ArrayBuilder<string>.GetInstance(out var builder);
142142
foreach (int errorCode in errorCodes)

src/Features/CSharpTest/EditAndContinue/CSharpEditAndContinueAnalyzerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private static IEnumerable<TextSpan> GetExpectedSpans(string source)
8787
private static void TestErrorSpansAllKinds(Func<SyntaxKind, bool> hasLabel)
8888
{
8989
var unhandledKinds = new List<SyntaxKind>();
90-
foreach (var kind in Enum.GetValues(typeof(SyntaxKind)).Cast<SyntaxKind>().Where(hasLabel))
90+
foreach (var kind in Enum.GetValues<SyntaxKind>().Where(hasLabel))
9191
{
9292
TextSpan? span;
9393
try

0 commit comments

Comments
 (0)