Skip to content

Commit

Permalink
Fix violations of RS0030 (Do not used banned APIs)
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell authored and dotpaul committed Oct 23, 2019
1 parent c72c633 commit f21d049
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 20 deletions.
10 changes: 6 additions & 4 deletions src/MetaCompilation.Analyzers/Core/CodeFixProvider.cs
Expand Up @@ -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;
Expand Down Expand Up @@ -1183,7 +1185,7 @@ private async Task<Document> 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<StatementSyntax> statements = new SyntaxList<StatementSyntax>();
string name = "context";
SyntaxNode initializeDeclaration = CodeFixHelper.BuildInitialize(generator, notImplementedException, statements, name);
Expand Down Expand Up @@ -1553,7 +1555,7 @@ private async Task<Document> 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;
Expand Down Expand Up @@ -1726,7 +1728,7 @@ private async Task<Document> 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<SyntaxNode>();
Expand Down Expand Up @@ -2379,7 +2381,7 @@ internal static SyntaxNode CreateAnalysisMethod(SyntaxGenerator generator, strin
TypeSyntax type = SyntaxFactory.ParseTypeName("SyntaxNodeAnalysisContext");
SyntaxNode[] parameters = new[] { generator.ParameterDeclaration("context", type) };
SyntaxList<SyntaxNode> statements = new SyntaxList<SyntaxNode>();
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);
Expand Down
21 changes: 11 additions & 10 deletions src/MetaCompilation.Analyzers/Core/DiagnosticAnalyzer.cs
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -2766,7 +2767,7 @@ private CheckInitializeInfo CheckInitialize(CompilationAnalysisContext context)
private BlockSyntax InitializeOverview(CompilationAnalysisContext context)
{
ImmutableArray<IParameterSymbol> 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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
{
Expand All @@ -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))
{
Expand All @@ -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;
}
Expand Down
Expand Up @@ -12,4 +12,5 @@
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis" Version="$(MicrosoftCodeAnalysisVersion)" />
</ItemGroup>
<Import Project="..\..\Utilities\Compiler\Analyzer.Utilities.projitems" Label="Shared" />
</Project>
Expand Up @@ -108,7 +108,7 @@ private void OnCompilationStart(CompilationStartAnalysisContext compilationConte

private static ImmutableDictionary<IAssemblySymbol, ImmutableSortedSet<string>> GetRestrictedInternalsVisibleToMap(Compilation compilation)
{
var restrictedInternalsVisibleToAttribute = compilation.GetTypeByMetadataName(WellKnownTypeNames.SystemRuntimeCompilerServicesRestrictedInternalsVisibleToAttribute);
var restrictedInternalsVisibleToAttribute = compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemRuntimeCompilerServicesRestrictedInternalsVisibleToAttribute);
if (restrictedInternalsVisibleToAttribute == null)
{
return ImmutableDictionary<IAssemblySymbol, ImmutableSortedSet<string>>.Empty;
Expand Down
@@ -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
Expand All @@ -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)
Expand Down
@@ -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
Expand All @@ -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)
Expand Down
Expand Up @@ -493,7 +493,7 @@ internal void OnCompilationEnd(CompilationAnalysisContext context)

private void ProcessTypeForwardedAttributes(Compilation compilation, Action<Diagnostic> reportDiagnostic, CancellationToken cancellationToken)
{
var typeForwardedToAttribute = compilation.GetTypeByMetadataName(WellKnownTypeNames.SystemRuntimeCompilerServicesTypeForwardedToAttribute);
var typeForwardedToAttribute = compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemRuntimeCompilerServicesTypeForwardedToAttribute);

if (typeForwardedToAttribute != null)
{
Expand Down
10 changes: 8 additions & 2 deletions src/Utilities/Compiler/WellKnownTypeProvider.cs
Expand Up @@ -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)
Expand Down

0 comments on commit f21d049

Please sign in to comment.