From a7f11d5bc94f3cb648f57428f7672487eeba6461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Tue, 2 Jan 2024 17:20:48 +0100 Subject: [PATCH] Remove unnecessary SupportsGetImportScopes method in SemanticModelExtensions #3594 --- .../Helpers/SyntaxTreeHelpers.cs | 9 ++----- .../Lightup/SemanticModelExtensions.cs | 25 ++++++------------- 2 files changed, 9 insertions(+), 25 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Helpers/SyntaxTreeHelpers.cs b/StyleCop.Analyzers/StyleCop.Analyzers/Helpers/SyntaxTreeHelpers.cs index 8bc9a6764..10155cf4d 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/Helpers/SyntaxTreeHelpers.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/Helpers/SyntaxTreeHelpers.cs @@ -100,13 +100,8 @@ private static bool ContainsUsingAliasNoCache(SyntaxTree tree, SemanticModel sem } // Check for global using aliases - if (SemanticModelExtensions.SupportsGetImportScopes) - { - var scopes = semanticModel.GetImportScopes(0, cancellationToken); - return scopes.Any(x => x.Aliases.Length > 0); - } - - return false; + var scopes = semanticModel.GetImportScopes(0, cancellationToken); + return scopes.Any(x => x.Aliases.Length > 0); } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SemanticModelExtensions.cs b/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SemanticModelExtensions.cs index 95e56f0c8..a10116a94 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SemanticModelExtensions.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SemanticModelExtensions.cs @@ -18,21 +18,15 @@ internal static class SemanticModelExtensions static SemanticModelExtensions() { - CreateGetImportScopesAccessor(out var getImportScopesAccessor, out var supportsGetImportScopes); - GetImportScopesAccessor = getImportScopesAccessor; - SupportsGetImportScopes = supportsGetImportScopes; + GetImportScopesAccessor = CreateGetImportScopesAccessor(); } - public static bool SupportsGetImportScopes { get; } - public static ImmutableArray GetImportScopes(this SemanticModel semanticModel, int position, CancellationToken cancellationToken = default) { return GetImportScopesAccessor(semanticModel, position, cancellationToken); } - private static void CreateGetImportScopesAccessor( - out Func> accessor, - out bool isSupported) + private static Func> CreateGetImportScopesAccessor() { var semanticModelType = typeof(SemanticModel); @@ -40,17 +34,14 @@ private static void CreateGetImportScopesAccessor( var nativeImportScopeType = codeAnalysisWorkspacesAssembly.GetType("Microsoft.CodeAnalysis.IImportScope"); if (nativeImportScopeType == null) { - accessor = FallbackAccessor; - isSupported = false; - return; + return FallbackAccessor; } var method = semanticModelType.GetTypeInfo().GetDeclaredMethods("GetImportScopes").SingleOrDefault(IsCorrectGetImportScopesMethod); if (method == null) { - accessor = FallbackAccessor; - isSupported = false; - return; + // This should not happen, since this missing method and the type we successfully managed to retrieve above was added in the same Roslyn version + return FallbackAccessor; } var importScopeWrapperFromObjectMethod = typeof(IImportScopeWrapper).GetTypeInfo().GetDeclaredMethod("FromObject"); @@ -134,8 +125,8 @@ private static void CreateGetImportScopesAccessor( block, new[] { semanticModelParameter, positionParameter, cancellationTokenParameter }); - accessor = lambda.Compile(); - isSupported = true; + var accessor = lambda.Compile(); + return accessor; static bool IsCorrectGetImportScopesMethod(MethodInfo method) { @@ -153,8 +144,6 @@ static bool IsCorrectCreateBuilderMethod(MethodInfo method) static ImmutableArray FallbackAccessor(SemanticModel semanticModel, int position, CancellationToken cancellationToken) { - Debug.Assert(false, $"This method should not have been called. Check {nameof(SupportsGetImportScopes)} first!"); - return ImmutableArray.Empty; } }