From f21d049feaf49718d094f513e8d63cc8af2424f8 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Tue, 22 Oct 2019 08:20:51 -0700 Subject: [PATCH] Fix violations of RS0030 (Do not used banned APIs) --- .../Core/CodeFixProvider.cs | 10 +++++---- .../Core/DiagnosticAnalyzer.cs | 21 ++++++++++--------- .../Core/MetaCompilation.Analyzers.csproj | 1 + .../RestrictedInternalsVisibleToAnalyzer.cs | 2 +- .../Core/AbstractAllocationAnalyzer.cs | 3 ++- .../Core/AbstractAllocationAnalyzer`1.cs | 3 ++- .../DeclarePublicApiAnalyzer.Impl.cs | 2 +- .../Compiler/WellKnownTypeProvider.cs | 10 +++++++-- 8 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/MetaCompilation.Analyzers/Core/CodeFixProvider.cs b/src/MetaCompilation.Analyzers/Core/CodeFixProvider.cs index 47496adfc6..6a676486ec 100644 --- a/src/MetaCompilation.Analyzers/Core/CodeFixProvider.cs +++ b/src/MetaCompilation.Analyzers/Core/CodeFixProvider.cs @@ -7,6 +7,8 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; +using Analyzer.Utilities; +using Analyzer.Utilities.Extensions; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; @@ -1183,7 +1185,7 @@ private async Task MissingInitAsync(Document document, ClassDeclaratio SemanticModel semanticModel = await document.GetSemanticModelAsync().ConfigureAwait(false); - INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetTypeByMetadataName("System.NotImplementedException"); + INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemNotImplementedException); SyntaxList statements = new SyntaxList(); string name = "context"; SyntaxNode initializeDeclaration = CodeFixHelper.BuildInitialize(generator, notImplementedException, statements, name); @@ -1553,7 +1555,7 @@ private async Task MissingAccessorAsync(Document document, PropertyDec SemanticModel semanticModel = await document.GetSemanticModelAsync().ConfigureAwait(false); - INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetTypeByMetadataName("System.NotImplementedException"); + INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemNotImplementedException); SyntaxNode[] throwStatement = new[] { generator.ThrowStatement(generator.ObjectCreationExpression(notImplementedException)) }; SyntaxNode type = generator.GetType(declaration); PropertyDeclarationSyntax newPropertyDeclaration = generator.PropertyDeclaration("SupportedDiagnostics", type, Accessibility.Public, DeclarationModifiers.Override, throwStatement) as PropertyDeclarationSyntax; @@ -1726,7 +1728,7 @@ private async Task AddSuppDiagAsync(Document document, ClassDeclaratio SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document); SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); - INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetTypeByMetadataName("System.NotImplementedException"); + INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemNotImplementedException); PropertyDeclarationSyntax propertyDeclaration = CodeFixHelper.CreateSupportedDiagnostics(generator, notImplementedException); var newNodes = new SyntaxList(); @@ -2379,7 +2381,7 @@ internal static SyntaxNode CreateAnalysisMethod(SyntaxGenerator generator, strin TypeSyntax type = SyntaxFactory.ParseTypeName("SyntaxNodeAnalysisContext"); SyntaxNode[] parameters = new[] { generator.ParameterDeclaration("context", type) }; SyntaxList statements = new SyntaxList(); - INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetTypeByMetadataName("System.NotImplementedException"); + INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemNotImplementedException); statements = statements.Add(generator.ThrowStatement(generator.ObjectCreationExpression(notImplementedException))); SyntaxNode newMethodDeclaration = generator.MethodDeclaration(methodName, parameters: parameters, accessibility: Accessibility.Private, statements: statements); diff --git a/src/MetaCompilation.Analyzers/Core/DiagnosticAnalyzer.cs b/src/MetaCompilation.Analyzers/Core/DiagnosticAnalyzer.cs index 25676d3b7f..7c7977b168 100644 --- a/src/MetaCompilation.Analyzers/Core/DiagnosticAnalyzer.cs +++ b/src/MetaCompilation.Analyzers/Core/DiagnosticAnalyzer.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; +using Analyzer.Utilities.Extensions; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -2607,7 +2608,7 @@ private bool CheckMethods(InvocationExpressionSyntax invocationExpression, Compi ReportDiagnostic(context, IncorrectAnalysisReturnTypeRule, analysisMethodSyntax.Identifier.GetLocation(), analysisMethodSyntax.Identifier.ValueText); return false; } - else if (analysisMethod.Parameters.Length != 1 || !Equals(analysisMethod.Parameters.First().Type, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.SyntaxNodeAnalysisContext"))) + else if (analysisMethod.Parameters.Length != 1 || !Equals(analysisMethod.Parameters.First().Type, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.SyntaxNodeAnalysisContext"))) { ReportDiagnostic(context, IncorrectAnalysisParameterRule, analysisMethodSyntax.ParameterList.GetLocation(), analysisMethodSyntax.Identifier.ValueText); return false; @@ -2766,7 +2767,7 @@ private CheckInitializeInfo CheckInitialize(CompilationAnalysisContext context) private BlockSyntax InitializeOverview(CompilationAnalysisContext context) { ImmutableArray parameters = _initializeSymbol.Parameters; - if (parameters.Length != 1 || !Equals(parameters[0].Type, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.AnalysisContext")) + if (parameters.Length != 1 || !Equals(parameters[0].Type, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.AnalysisContext")) || _initializeSymbol.DeclaredAccessibility != Accessibility.Public || !_initializeSymbol.IsOverride || !_initializeSymbol.ReturnsVoid) { ReportDiagnostic(context, IncorrectInitSigRule, _initializeSymbol.Locations[0], _initializeSymbol.Name); @@ -2866,9 +2867,9 @@ internal protected void AddMethod(SymbolAnalysisContext context) return; } - if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer"))) + if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer"))) { - if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider"))) + if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider"))) { return; } @@ -2918,9 +2919,9 @@ internal protected void AddProperty(SymbolAnalysisContext context) return; } - if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer"))) + if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer"))) { - if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider"))) + if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider"))) { return; } @@ -2967,7 +2968,7 @@ internal protected void AddField(SymbolAnalysisContext context) return; } - if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer"))) + if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer"))) { return; } @@ -2995,7 +2996,7 @@ internal protected void AddClass(SymbolAnalysisContext context) return; } - if (!Equals(sym.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer"))) + if (!Equals(sym.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer"))) { if (sym.ContainingType == null) { @@ -3007,7 +3008,7 @@ internal protected void AddClass(SymbolAnalysisContext context) return; } - if (Equals(sym.ContainingType.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer"))) + if (Equals(sym.ContainingType.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer"))) { if (_otherAnalyzerClassSymbols.Contains(sym)) { @@ -3021,7 +3022,7 @@ internal protected void AddClass(SymbolAnalysisContext context) } } - if (Equals(sym.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer"))) + if (Equals(sym.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer"))) { _analyzerClassSymbol = sym; } diff --git a/src/MetaCompilation.Analyzers/Core/MetaCompilation.Analyzers.csproj b/src/MetaCompilation.Analyzers/Core/MetaCompilation.Analyzers.csproj index b5e212d1d2..aea6868e7a 100644 --- a/src/MetaCompilation.Analyzers/Core/MetaCompilation.Analyzers.csproj +++ b/src/MetaCompilation.Analyzers/Core/MetaCompilation.Analyzers.csproj @@ -12,4 +12,5 @@ + diff --git a/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/Core/RestrictedInternalsVisibleToAnalyzer.cs b/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/Core/RestrictedInternalsVisibleToAnalyzer.cs index 5fb03deaa2..1b030a97e4 100644 --- a/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/Core/RestrictedInternalsVisibleToAnalyzer.cs +++ b/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/Core/RestrictedInternalsVisibleToAnalyzer.cs @@ -108,7 +108,7 @@ private void OnCompilationStart(CompilationStartAnalysisContext compilationConte private static ImmutableDictionary> GetRestrictedInternalsVisibleToMap(Compilation compilation) { - var restrictedInternalsVisibleToAttribute = compilation.GetTypeByMetadataName(WellKnownTypeNames.SystemRuntimeCompilerServicesRestrictedInternalsVisibleToAttribute); + var restrictedInternalsVisibleToAttribute = compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemRuntimeCompilerServicesRestrictedInternalsVisibleToAttribute); if (restrictedInternalsVisibleToAttribute == null) { return ImmutableDictionary>.Empty; diff --git a/src/PerformanceSensitiveAnalyzers/Core/AbstractAllocationAnalyzer.cs b/src/PerformanceSensitiveAnalyzers/Core/AbstractAllocationAnalyzer.cs index c4eed3649d..cfaccc789c 100644 --- a/src/PerformanceSensitiveAnalyzers/Core/AbstractAllocationAnalyzer.cs +++ b/src/PerformanceSensitiveAnalyzers/Core/AbstractAllocationAnalyzer.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Immutable; +using Analyzer.Utilities.Extensions; using Microsoft.CodeAnalysis.Diagnostics; namespace Microsoft.CodeAnalysis.PerformanceSensitiveAnalyzers @@ -26,7 +27,7 @@ public override void Initialize(AnalysisContext context) context.RegisterCompilationStartAction(compilationStartContext => { var compilation = compilationStartContext.Compilation; - var attributeSymbol = compilation.GetTypeByMetadataName(AllocationRules.PerformanceSensitiveAttributeName); + var attributeSymbol = compilation.GetOrCreateTypeByMetadataName(AllocationRules.PerformanceSensitiveAttributeName); // Bail if PerformanceSensitiveAttribute is not delcared in the compilation. if (attributeSymbol == null) diff --git a/src/PerformanceSensitiveAnalyzers/Core/AbstractAllocationAnalyzer`1.cs b/src/PerformanceSensitiveAnalyzers/Core/AbstractAllocationAnalyzer`1.cs index bad0046a8f..43ca59526f 100644 --- a/src/PerformanceSensitiveAnalyzers/Core/AbstractAllocationAnalyzer`1.cs +++ b/src/PerformanceSensitiveAnalyzers/Core/AbstractAllocationAnalyzer`1.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Immutable; +using Analyzer.Utilities.Extensions; using Microsoft.CodeAnalysis.Diagnostics; namespace Microsoft.CodeAnalysis.PerformanceSensitiveAnalyzers @@ -27,7 +28,7 @@ public override void Initialize(AnalysisContext context) context.RegisterCompilationStartAction(compilationStartContext => { var compilation = compilationStartContext.Compilation; - var attributeSymbol = compilation.GetTypeByMetadataName(AllocationRules.PerformanceSensitiveAttributeName); + var attributeSymbol = compilation.GetOrCreateTypeByMetadataName(AllocationRules.PerformanceSensitiveAttributeName); // Bail if PerformanceSensitiveAttribute is not delcared in the compilation. if (attributeSymbol == null) diff --git a/src/PublicApiAnalyzers/Core/Analyzers/DeclarePublicApiAnalyzer.Impl.cs b/src/PublicApiAnalyzers/Core/Analyzers/DeclarePublicApiAnalyzer.Impl.cs index 8ca7a90150..c6f50f7da5 100644 --- a/src/PublicApiAnalyzers/Core/Analyzers/DeclarePublicApiAnalyzer.Impl.cs +++ b/src/PublicApiAnalyzers/Core/Analyzers/DeclarePublicApiAnalyzer.Impl.cs @@ -493,7 +493,7 @@ internal void OnCompilationEnd(CompilationAnalysisContext context) private void ProcessTypeForwardedAttributes(Compilation compilation, Action reportDiagnostic, CancellationToken cancellationToken) { - var typeForwardedToAttribute = compilation.GetTypeByMetadataName(WellKnownTypeNames.SystemRuntimeCompilerServicesTypeForwardedToAttribute); + var typeForwardedToAttribute = compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemRuntimeCompilerServicesTypeForwardedToAttribute); if (typeForwardedToAttribute != null) { diff --git a/src/Utilities/Compiler/WellKnownTypeProvider.cs b/src/Utilities/Compiler/WellKnownTypeProvider.cs index 98c026e76d..aee76962ed 100644 --- a/src/Utilities/Compiler/WellKnownTypeProvider.cs +++ b/src/Utilities/Compiler/WellKnownTypeProvider.cs @@ -51,8 +51,14 @@ public bool TryGetOrCreateTypeByMetadataName(string fullTypeName, out INamedType fullyQualifiedMetadataName => { // Caching null results in our cache is intended. - var type = Compilation.GetTypeByMetadataName(fullyQualifiedMetadataName) - ?? Compilation.Assembly.GetTypeByMetadataName(fullyQualifiedMetadataName); + +#pragma warning disable RS0030 // Do not used banned APIs + // Use of Compilation.GetTypeByMetadataName is allowed here (this is our wrapper for it which + // includes fallback handling for cases where GetTypeByMetadataName returns null). + var type = Compilation.GetTypeByMetadataName(fullyQualifiedMetadataName); +#pragma warning restore RS0030 // Do not used banned APIs + + type ??= Compilation.Assembly.GetTypeByMetadataName(fullyQualifiedMetadataName); if (type is null) { foreach (var module in Compilation.Assembly.Modules)