Skip to content

Commit f21d049

Browse files
sharwelldotpaul
authored andcommitted
Fix violations of RS0030 (Do not used banned APIs)
1 parent c72c633 commit f21d049

8 files changed

Lines changed: 32 additions & 20 deletions

File tree

src/MetaCompilation.Analyzers/Core/CodeFixProvider.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using System.Linq;
88
using System.Threading;
99
using System.Threading.Tasks;
10+
using Analyzer.Utilities;
11+
using Analyzer.Utilities.Extensions;
1012
using Microsoft.CodeAnalysis;
1113
using Microsoft.CodeAnalysis.CodeActions;
1214
using Microsoft.CodeAnalysis.CodeFixes;
@@ -1183,7 +1185,7 @@ private async Task<Document> MissingInitAsync(Document document, ClassDeclaratio
11831185

11841186
SemanticModel semanticModel = await document.GetSemanticModelAsync().ConfigureAwait(false);
11851187

1186-
INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetTypeByMetadataName("System.NotImplementedException");
1188+
INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemNotImplementedException);
11871189
SyntaxList<StatementSyntax> statements = new SyntaxList<StatementSyntax>();
11881190
string name = "context";
11891191
SyntaxNode initializeDeclaration = CodeFixHelper.BuildInitialize(generator, notImplementedException, statements, name);
@@ -1553,7 +1555,7 @@ private async Task<Document> MissingAccessorAsync(Document document, PropertyDec
15531555

15541556
SemanticModel semanticModel = await document.GetSemanticModelAsync().ConfigureAwait(false);
15551557

1556-
INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetTypeByMetadataName("System.NotImplementedException");
1558+
INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemNotImplementedException);
15571559
SyntaxNode[] throwStatement = new[] { generator.ThrowStatement(generator.ObjectCreationExpression(notImplementedException)) };
15581560
SyntaxNode type = generator.GetType(declaration);
15591561
PropertyDeclarationSyntax newPropertyDeclaration = generator.PropertyDeclaration("SupportedDiagnostics", type, Accessibility.Public, DeclarationModifiers.Override, throwStatement) as PropertyDeclarationSyntax;
@@ -1726,7 +1728,7 @@ private async Task<Document> AddSuppDiagAsync(Document document, ClassDeclaratio
17261728
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
17271729

17281730
SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
1729-
INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetTypeByMetadataName("System.NotImplementedException");
1731+
INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemNotImplementedException);
17301732
PropertyDeclarationSyntax propertyDeclaration = CodeFixHelper.CreateSupportedDiagnostics(generator, notImplementedException);
17311733

17321734
var newNodes = new SyntaxList<SyntaxNode>();
@@ -2379,7 +2381,7 @@ internal static SyntaxNode CreateAnalysisMethod(SyntaxGenerator generator, strin
23792381
TypeSyntax type = SyntaxFactory.ParseTypeName("SyntaxNodeAnalysisContext");
23802382
SyntaxNode[] parameters = new[] { generator.ParameterDeclaration("context", type) };
23812383
SyntaxList<SyntaxNode> statements = new SyntaxList<SyntaxNode>();
2382-
INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetTypeByMetadataName("System.NotImplementedException");
2384+
INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemNotImplementedException);
23832385
statements = statements.Add(generator.ThrowStatement(generator.ObjectCreationExpression(notImplementedException)));
23842386

23852387
SyntaxNode newMethodDeclaration = generator.MethodDeclaration(methodName, parameters: parameters, accessibility: Accessibility.Private, statements: statements);

src/MetaCompilation.Analyzers/Core/DiagnosticAnalyzer.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Collections.Immutable;
55
using System.Linq;
6+
using Analyzer.Utilities.Extensions;
67
using Microsoft.CodeAnalysis;
78
using Microsoft.CodeAnalysis.CSharp;
89
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -2607,7 +2608,7 @@ private bool CheckMethods(InvocationExpressionSyntax invocationExpression, Compi
26072608
ReportDiagnostic(context, IncorrectAnalysisReturnTypeRule, analysisMethodSyntax.Identifier.GetLocation(), analysisMethodSyntax.Identifier.ValueText);
26082609
return false;
26092610
}
2610-
else if (analysisMethod.Parameters.Length != 1 || !Equals(analysisMethod.Parameters.First().Type, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.SyntaxNodeAnalysisContext")))
2611+
else if (analysisMethod.Parameters.Length != 1 || !Equals(analysisMethod.Parameters.First().Type, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.SyntaxNodeAnalysisContext")))
26112612
{
26122613
ReportDiagnostic(context, IncorrectAnalysisParameterRule, analysisMethodSyntax.ParameterList.GetLocation(), analysisMethodSyntax.Identifier.ValueText);
26132614
return false;
@@ -2766,7 +2767,7 @@ private CheckInitializeInfo CheckInitialize(CompilationAnalysisContext context)
27662767
private BlockSyntax InitializeOverview(CompilationAnalysisContext context)
27672768
{
27682769
ImmutableArray<IParameterSymbol> parameters = _initializeSymbol.Parameters;
2769-
if (parameters.Length != 1 || !Equals(parameters[0].Type, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.AnalysisContext"))
2770+
if (parameters.Length != 1 || !Equals(parameters[0].Type, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.AnalysisContext"))
27702771
|| _initializeSymbol.DeclaredAccessibility != Accessibility.Public || !_initializeSymbol.IsOverride || !_initializeSymbol.ReturnsVoid)
27712772
{
27722773
ReportDiagnostic(context, IncorrectInitSigRule, _initializeSymbol.Locations[0], _initializeSymbol.Name);
@@ -2866,9 +2867,9 @@ internal protected void AddMethod(SymbolAnalysisContext context)
28662867
return;
28672868
}
28682869

2869-
if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
2870+
if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
28702871
{
2871-
if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider")))
2872+
if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider")))
28722873
{
28732874
return;
28742875
}
@@ -2918,9 +2919,9 @@ internal protected void AddProperty(SymbolAnalysisContext context)
29182919
return;
29192920
}
29202921

2921-
if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
2922+
if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
29222923
{
2923-
if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider")))
2924+
if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider")))
29242925
{
29252926
return;
29262927
}
@@ -2967,7 +2968,7 @@ internal protected void AddField(SymbolAnalysisContext context)
29672968
return;
29682969
}
29692970

2970-
if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
2971+
if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
29712972
{
29722973
return;
29732974
}
@@ -2995,7 +2996,7 @@ internal protected void AddClass(SymbolAnalysisContext context)
29952996
return;
29962997
}
29972998

2998-
if (!Equals(sym.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
2999+
if (!Equals(sym.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
29993000
{
30003001
if (sym.ContainingType == null)
30013002
{
@@ -3007,7 +3008,7 @@ internal protected void AddClass(SymbolAnalysisContext context)
30073008
return;
30083009
}
30093010

3010-
if (Equals(sym.ContainingType.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
3011+
if (Equals(sym.ContainingType.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
30113012
{
30123013
if (_otherAnalyzerClassSymbols.Contains(sym))
30133014
{
@@ -3021,7 +3022,7 @@ internal protected void AddClass(SymbolAnalysisContext context)
30213022
}
30223023
}
30233024

3024-
if (Equals(sym.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
3025+
if (Equals(sym.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
30253026
{
30263027
_analyzerClassSymbol = sym;
30273028
}

src/MetaCompilation.Analyzers/Core/MetaCompilation.Analyzers.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
<ItemGroup>
1313
<PackageReference Include="Microsoft.CodeAnalysis" Version="$(MicrosoftCodeAnalysisVersion)" />
1414
</ItemGroup>
15+
<Import Project="..\..\Utilities\Compiler\Analyzer.Utilities.projitems" Label="Shared" />
1516
</Project>

src/Microsoft.CodeAnalysis.BannedApiAnalyzers/Core/RestrictedInternalsVisibleToAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private void OnCompilationStart(CompilationStartAnalysisContext compilationConte
108108

109109
private static ImmutableDictionary<IAssemblySymbol, ImmutableSortedSet<string>> GetRestrictedInternalsVisibleToMap(Compilation compilation)
110110
{
111-
var restrictedInternalsVisibleToAttribute = compilation.GetTypeByMetadataName(WellKnownTypeNames.SystemRuntimeCompilerServicesRestrictedInternalsVisibleToAttribute);
111+
var restrictedInternalsVisibleToAttribute = compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemRuntimeCompilerServicesRestrictedInternalsVisibleToAttribute);
112112
if (restrictedInternalsVisibleToAttribute == null)
113113
{
114114
return ImmutableDictionary<IAssemblySymbol, ImmutableSortedSet<string>>.Empty;

src/PerformanceSensitiveAnalyzers/Core/AbstractAllocationAnalyzer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
22

33
using System.Collections.Immutable;
4+
using Analyzer.Utilities.Extensions;
45
using Microsoft.CodeAnalysis.Diagnostics;
56

67
namespace Microsoft.CodeAnalysis.PerformanceSensitiveAnalyzers
@@ -26,7 +27,7 @@ public override void Initialize(AnalysisContext context)
2627
context.RegisterCompilationStartAction(compilationStartContext =>
2728
{
2829
var compilation = compilationStartContext.Compilation;
29-
var attributeSymbol = compilation.GetTypeByMetadataName(AllocationRules.PerformanceSensitiveAttributeName);
30+
var attributeSymbol = compilation.GetOrCreateTypeByMetadataName(AllocationRules.PerformanceSensitiveAttributeName);
3031

3132
// Bail if PerformanceSensitiveAttribute is not delcared in the compilation.
3233
if (attributeSymbol == null)

src/PerformanceSensitiveAnalyzers/Core/AbstractAllocationAnalyzer`1.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
22

33
using System.Collections.Immutable;
4+
using Analyzer.Utilities.Extensions;
45
using Microsoft.CodeAnalysis.Diagnostics;
56

67
namespace Microsoft.CodeAnalysis.PerformanceSensitiveAnalyzers
@@ -27,7 +28,7 @@ public override void Initialize(AnalysisContext context)
2728
context.RegisterCompilationStartAction(compilationStartContext =>
2829
{
2930
var compilation = compilationStartContext.Compilation;
30-
var attributeSymbol = compilation.GetTypeByMetadataName(AllocationRules.PerformanceSensitiveAttributeName);
31+
var attributeSymbol = compilation.GetOrCreateTypeByMetadataName(AllocationRules.PerformanceSensitiveAttributeName);
3132

3233
// Bail if PerformanceSensitiveAttribute is not delcared in the compilation.
3334
if (attributeSymbol == null)

src/PublicApiAnalyzers/Core/Analyzers/DeclarePublicApiAnalyzer.Impl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ internal void OnCompilationEnd(CompilationAnalysisContext context)
493493

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

498498
if (typeForwardedToAttribute != null)
499499
{

src/Utilities/Compiler/WellKnownTypeProvider.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,14 @@ public bool TryGetOrCreateTypeByMetadataName(string fullTypeName, out INamedType
5151
fullyQualifiedMetadataName =>
5252
{
5353
// Caching null results in our cache is intended.
54-
var type = Compilation.GetTypeByMetadataName(fullyQualifiedMetadataName)
55-
?? Compilation.Assembly.GetTypeByMetadataName(fullyQualifiedMetadataName);
54+
55+
#pragma warning disable RS0030 // Do not used banned APIs
56+
// Use of Compilation.GetTypeByMetadataName is allowed here (this is our wrapper for it which
57+
// includes fallback handling for cases where GetTypeByMetadataName returns null).
58+
var type = Compilation.GetTypeByMetadataName(fullyQualifiedMetadataName);
59+
#pragma warning restore RS0030 // Do not used banned APIs
60+
61+
type ??= Compilation.Assembly.GetTypeByMetadataName(fullyQualifiedMetadataName);
5662
if (type is null)
5763
{
5864
foreach (var module in Compilation.Assembly.Modules)

0 commit comments

Comments
 (0)