Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ private static ImmutableDictionary<string, TargetScope> CreateTargetScopesMap()
{
var builder = ImmutableDictionary.CreateBuilder<string, TargetScope>(StringComparer.OrdinalIgnoreCase);

#pragma warning disable CS8605 // Unboxing a possibly null value.
foreach (TargetScope targetScope in Enum.GetValues(typeof(TargetScope)))
#pragma warning restore CS8605 // Unboxing a possibly null value.
foreach (var targetScope in Enum.GetValues<TargetScope>())
{
if (targetScope == TargetScope.None)
{
Expand Down
4 changes: 2 additions & 2 deletions src/CodeStyle/Tools/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ private static void CreateGlobalConfigFiles(string language, string outputDir, I
}

var configDir = Path.Combine(outputDir, "config");
foreach (var analysisMode in Enum.GetValues(typeof(AnalysisMode)))
foreach (var analysisMode in Enum.GetValues<AnalysisMode>())
{
CreateGlobalconfig(
configDir,
$"AnalysisLevelStyle_{analysisMode}.globalconfig".ToLowerInvariant(),
(AnalysisMode)analysisMode!,
analysisMode,
allRulesById);
}

Expand Down
27 changes: 27 additions & 0 deletions src/Dependencies/Contracts/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

namespace System;

internal static partial class RoslynEnumExtensions
{
#if NET // binary compatibility
public static TEnum[] GetValues<TEnum>() where TEnum : struct, Enum
=> Enum.GetValues<TEnum>();

public static string[] GetNames<TEnum>() where TEnum : struct, Enum
=> Enum.GetNames<TEnum>();
#else
extension(Enum)
{
public static TEnum[] GetValues<TEnum>() where TEnum : struct, Enum
=> (TEnum[])Enum.GetValues(typeof(TEnum));

public static string[] GetNames<TEnum>() where TEnum : struct, Enum
=> Enum.GetNames(typeof(TEnum));
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public enum ReferenceType

private static IEnumerable<object[]> CombineWithReferenceTypeData(IEnumerable<List<object>> data)
{
foreach (var refKind in Enum.GetValues(typeof(ReferenceType)))
foreach (var refKind in Enum.GetValues<ReferenceType>())
{
foreach (var d in data)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected override ImmutableArray<string> ExpectedDiagnosticCodes
{
get
{
var errorCodes = Enum.GetValues(typeof(ErrorCode));
var errorCodes = Enum.GetValues<ErrorCode>();
var supported = new CSharpCompilerDiagnosticAnalyzer().GetSupportedErrorCodes();
var builder = ImmutableArray.CreateBuilder<string>();
foreach (int errorCode in errorCodes)
Expand Down
2 changes: 1 addition & 1 deletion src/EditorFeatures/Core/Logging/FunctionIdOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private static Option2<bool> CreateOption(FunctionId id)
}

private static IEnumerable<FunctionId> GetFunctionIds()
=> Enum.GetValues(typeof(FunctionId)).Cast<FunctionId>();
=> Enum.GetValues<FunctionId>();

public static IEnumerable<IOption2> GetOptions()
=> GetFunctionIds().Select(GetOption);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public static void CodeStyleSettingEnumFactory(DayOfWeek defaultValue)
description: "TestDesciption",
options,
updater: null!,
enumValues: (DayOfWeek[])Enum.GetValues(typeof(DayOfWeek)),
valueDescriptions: Enum.GetNames(typeof(DayOfWeek)));
enumValues: Enum.GetValues<DayOfWeek>(),
valueDescriptions: Enum.GetNames<DayOfWeek>());

Assert.Equal(string.Empty, setting.Category);
Assert.Equal("TestDesciption", setting.Description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ResultProviderTests
[Fact]
public void DkmEvaluationFlagsConflict()
{
var values = Enum.GetValues(typeof(DkmEvaluationFlags)).Cast<DkmEvaluationFlags>().ToArray();
var values = Enum.GetValues<DkmEvaluationFlags>();
Assert.False(values.Contains(ResultProvider.NoResults));
Assert.False(values.Contains(ResultProvider.NotRoot));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected override ImmutableArray<string> UnsupportedDiagnosticIds
{
get
{
var errorCodes = Enum.GetValues(typeof(ErrorCode));
var errorCodes = Enum.GetValues<ErrorCode>();
var supported = ((CSharpCompilerDiagnosticAnalyzer)OtherAnalyzers[0]).GetSupportedErrorCodes();
using var _ = ArrayBuilder<string>.GetInstance(out var builder);
foreach (int errorCode in errorCodes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private static IEnumerable<TextSpan> GetExpectedSpans(string source)
private static void TestErrorSpansAllKinds(Func<SyntaxKind, bool> hasLabel)
{
var unhandledKinds = new List<SyntaxKind>();
foreach (var kind in Enum.GetValues(typeof(SyntaxKind)).Cast<SyntaxKind>().Where(hasLabel))
foreach (var kind in Enum.GetValues<SyntaxKind>().Where(hasLabel))
{
TextSpan? span;
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,7 @@ void AddProjectRudeEdit(ProjectSettingKind kind)
AddGeneralDiagnostic(EditAndContinueErrorCode.ChangingMultiVersionReferences, nameof(FeaturesResources.Project_references_mutliple_assemblies_of_the_same_simple_name_0_1_Changing_a_reference_to_such_an_assembly_requires_restarting_the_application));
AddGeneralDiagnostic(EditAndContinueErrorCode.ChangingReference, nameof(FeaturesResources.Changing_project_or_package_reference_caused_the_identity_of_referenced_assembly_to_change_from_0_to_1_which_requires_restarting_the_application));

#if NET
foreach (var value in Enum.GetValues<ProjectSettingKind>())
#else
foreach (ProjectSettingKind value in Enum.GetValues(typeof(ProjectSettingKind)))
#endif
{
AddProjectRudeEdit(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public DeclaredSymbolInfoKindSet(IEnumerable<string> navigateToItemKinds)
// The 'Contains' method implementation assumes that the DeclaredSymbolInfoKind type is unsigned.
Debug.Assert(Enum.GetUnderlyingType(typeof(DeclaredSymbolInfoKind)) == typeof(byte));

var lookupTable = new bool[Enum.GetValues(typeof(DeclaredSymbolInfoKind)).Length];
var lookupTable = new bool[Enum.GetValues<DeclaredSymbolInfoKind>().Length];
foreach (var navigateToItemKind in navigateToItemKinds)
{
switch (navigateToItemKind)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal static class QuickInfoContentBuilder

private static FrozenDictionary<Glyph, QuickInfoGlyphElement> CreateGlyphToElementMap()
{
var glyphs = (Glyph[])Enum.GetValues(typeof(Glyph));
var glyphs = Enum.GetValues<Glyph>();
var result = new Dictionary<Glyph, QuickInfoGlyphElement>(capacity: glyphs.Length);

foreach (var glyph in glyphs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void ToDiagnostic()
RudeEditKind.ChangingNamespace,
};

var allKinds = Enum.GetValues(typeof(RudeEditKind)).Cast<RudeEditKind>();
var allKinds = Enum.GetValues<RudeEditKind>();

foreach (var kind in allKinds)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -992,12 +992,12 @@ void CreateGlobalConfigsForVersion(

foreach (var warnAsError in new[] { true, false })
{
foreach (var analysisMode in Enum.GetValues(typeof(AnalysisMode)))
foreach (var analysisMode in Enum.GetValues<AnalysisMode>())
{
CreateGlobalConfig(version, isShippedVersion, analysisLevelVersionString, (AnalysisMode)analysisMode!, warnAsError, releaseTrackingData, category: null);
CreateGlobalConfig(version, isShippedVersion, analysisLevelVersionString, analysisMode, warnAsError, releaseTrackingData, category: null);
foreach (var category in categories!)
{
CreateGlobalConfig(version, isShippedVersion, analysisLevelVersionString, (AnalysisMode)analysisMode!, warnAsError, releaseTrackingData, category);
CreateGlobalConfig(version, isShippedVersion, analysisLevelVersionString, analysisMode, warnAsError, releaseTrackingData, category);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal static partial class AnalyzerOptionsExtensions
{
private static readonly ConditionalWeakTable<AnalyzerOptions, ICategorizedAnalyzerConfigOptions> s_cachedOptions = new();
private static readonly ImmutableHashSet<OutputKind> s_defaultOutputKinds =
ImmutableHashSet.CreateRange(Enum.GetValues(typeof(OutputKind)).Cast<OutputKind>());
ImmutableHashSet.CreateRange(Enum.GetValues<OutputKind>());

private static bool TryGetSyntaxTreeForOption(ISymbol symbol, [NotNullWhen(returnValue: true)] out SyntaxTree? tree)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private class ValuePool

public ValuePool()
{
int[] values = Enum.GetValues(typeof(PropertySetAbstractValueKind)).Cast<int>().ToArray();
int[] values = Enum.GetValues<PropertySetAbstractValueKind>().Cast<int>().ToArray();
Array.Sort(values);
for (int i = 0; i < values.Length; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private static TaintedDataConfig Create(Compilation compilation)
PooledDictionary<(ImmutableHashSet<SourceInfo> SourceInfos, ImmutableHashSet<SanitizerInfo> SanitizerInfos), (ImmutableHashSet<SinkKind>.Builder SinkKinds, ImmutableHashSet<SinkInfo>.Builder SinkInfos)>.GetInstance(out var sourceSanitizersToSinks);

// Using LazyThreadSafetyMode.ExecutionAndPublication to avoid instantiating multiple times.
foreach (SinkKind sinkKind in Enum.GetValues(typeof(SinkKind)))
foreach (var sinkKind in Enum.GetValues<SinkKind>())
{
ImmutableHashSet<SourceInfo> sources = GetSourceInfos(sinkKind);
if (!sourcesToSymbolMap.TryGetValue(sources, out Lazy<TaintedDataSymbolMap<SourceInfo>> lazySourceSymbolMap))
Expand Down
2 changes: 1 addition & 1 deletion src/Scripting/CSharpTest/CommandLineRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ public void LangVersions()
var runner = CreateRunner(["/langversion:?"]);
Assert.Equal(0, runner.RunInteractive());

var expected = Enum.GetValues(typeof(LanguageVersion)).Cast<LanguageVersion>()
var expected = Enum.GetValues<LanguageVersion>()
.Select(v => v.ToDisplayString());

var actual = runner.Console.Out.ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static void SetLoggers(IGlobalOptionService globalOptions, IThreadingCont
var client = threadingContext.JoinableTaskFactory.Run(() => RemoteHostClient.TryGetClientAsync(workspaceServices, CancellationToken.None));
if (client != null)
{
var functionIds = Enum.GetValues(typeof(FunctionId)).Cast<FunctionId>().Where(isEnabled).ToImmutableArray();
var functionIds = Enum.GetValues<FunctionId>().Where(isEnabled).ToImmutableArray();

threadingContext.JoinableTaskFactory.Run(async () => _ = await client.TryInvokeAsync<IRemoteProcessTelemetryService>(
(service, cancellationToken) => service.EnableLoggingAsync(loggerTypeNames, functionIds, cancellationToken),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ private void OnCopy(object sender, RoutedEventArgs e)

private static string GetTelemetryString()
{
var fixAllScopeValues = Enum.GetValues(typeof(FixAllScope));

var sb = new StringBuilder();
var seenType = new HashSet<Type>();

Expand Down
2 changes: 1 addition & 1 deletion src/Workspaces/Core/Portable/Log/FunctionIdExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.CodeAnalysis.Internal.Log;
internal static class FunctionIdExtensions
{
private static readonly Lazy<ImmutableDictionary<FunctionId, string>> s_functionIdsToString = new(
() => Enum.GetValues(typeof(FunctionId)).Cast<FunctionId>().ToImmutableDictionary(f => f, f => f.ToString()));
() => Enum.GetValues<FunctionId>().ToImmutableDictionary(f => f, f => f.ToString()));

public static string Convert(this FunctionId functionId) => s_functionIdsToString.Value[functionId];
}