From eda6cd83e671ca3430c9c28154d8243a92ce3e60 Mon Sep 17 00:00:00 2001 From: Anne Thompson Date: Fri, 11 Aug 2023 10:44:54 -0700 Subject: [PATCH] clean up --- .../AZC0020Tests.cs | 33 ++++++------------- .../BannedTypesAnalyzer.cs | 26 +++++++++------ .../SymbolAnalyzerBase.cs | 6 ++-- 3 files changed, 28 insertions(+), 37 deletions(-) diff --git a/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers.Tests/AZC0020Tests.cs b/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers.Tests/AZC0020Tests.cs index 68d86cac279..f2e2ad16873 100644 --- a/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers.Tests/AZC0020Tests.cs +++ b/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers.Tests/AZC0020Tests.cs @@ -28,7 +28,7 @@ internal sealed partial class MutableJsonDocument ("MutableJsonElement.cs", @" namespace Azure.Core.Json { - internal sealed partial class MutableJsonElement + internal partial struct MutableJsonElement { } } @@ -51,26 +51,6 @@ public class Model internal MutableJsonDocument {|AZC0020:Document|} => {|AZC0020:_document|}; internal event EventHandler {|AZC0020:_docEvent|}; - internal MutableJsonDocument {|AZC0020:GetDocument|}(MutableJsonDocument {|AZC0020:value|}) - { - MutableJsonDocument mdoc = new MutableJsonDocument(); - return mdoc; - } - } -}"; - await Verifier.VerifyAnalyzerAsync(code, _sharedSourceFiles); - } - - [Fact] - public async Task AZC0020ProducedForMutableJsonDocumentUsage2() - { - string code = @" -using Azure.Core.Json; - -namespace LibraryNamespace -{ - public class Model - { internal MutableJsonDocument {|AZC0020:GetDocument|}(MutableJsonDocument {|AZC0020:value|}) { {|AZC0020:MutableJsonDocument mdoc = new MutableJsonDocument();|} @@ -91,7 +71,14 @@ namespace LibraryNamespace { public class Model { - MutableJsonElement {|AZC0020:_element|}; + private MutableJsonElement {|AZC0020:_element|}; + internal MutableJsonElement {|AZC0020:Element|} => {|AZC0020:_element|}; + + internal MutableJsonElement {|AZC0020:GetDocument|}(MutableJsonElement {|AZC0020:value|}) + { + {|AZC0020:MutableJsonElement element = new MutableJsonElement();|} + return element; + } } }"; await Verifier.VerifyAnalyzerAsync(code, _sharedSourceFiles); @@ -114,7 +101,7 @@ public class Model } [Fact] - public async Task AZC0020NotProducedForTypeWithBannedNameButAllowedNamespace() + public async Task AZC0020NotProducedForTypeWithBannedNameInAllowedNamespace() { string code = @" namespace LibraryNamespace diff --git a/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/BannedTypesAnalyzer.cs b/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/BannedTypesAnalyzer.cs index 56e1eb70ca1..c81b8ad7504 100644 --- a/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/BannedTypesAnalyzer.cs +++ b/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/BannedTypesAnalyzer.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Linq; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Diagnostics; @@ -34,6 +35,13 @@ public sealed class BannedTypesAnalyzer : SymbolAnalyzerBase SymbolKind.Property, }; + public override void Initialize(AnalysisContext context) + { + base.Initialize(context); + + context.RegisterSyntaxNodeAction(c => AnalyzeNode(c), SyntaxKind.LocalDeclarationStatement); + } + public override void Analyze(ISymbolAnalysisContext context) { Debug.WriteLine($"{context.Symbol}"); @@ -86,18 +94,16 @@ public override void AnalyzeNode(SyntaxNodeAnalysisContext context) return; } - LocalDeclarationStatementSyntax declaration = context.Node as LocalDeclarationStatementSyntax; - - TypeInfo info = context.SemanticModel.GetTypeInfo(declaration.Declaration.Type); - - ISymbol symbol = info.Type; - ITypeSymbol type = info.Type; - - if (type is INamedTypeSymbol namedTypeSymbol) + if (context.Node is LocalDeclarationStatementSyntax declaration) { - if (IsBannedType(namedTypeSymbol)) + ITypeSymbol type = context.SemanticModel.GetTypeInfo(declaration.Declaration.Type).Type; + + if (type is INamedTypeSymbol namedTypeSymbol) { - context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0020, context.Node.GetLocation(), BannedTypesMessageArgs)); + if (IsBannedType(namedTypeSymbol)) + { + context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0020, context.Node.GetLocation(), BannedTypesMessageArgs)); + } } } } diff --git a/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/SymbolAnalyzerBase.cs b/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/SymbolAnalyzerBase.cs index d4c52bc5cda..f04d800ca15 100644 --- a/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/SymbolAnalyzerBase.cs +++ b/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/SymbolAnalyzerBase.cs @@ -2,7 +2,6 @@ // Licensed under the MIT License. using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Diagnostics; namespace Azure.ClientSdk.Analyzers @@ -19,13 +18,12 @@ public virtual void AnalyzeNode(SyntaxNodeAnalysisContext context) { } protected INamedTypeSymbol ClientOptionsType { get; private set; } - public sealed override void Initialize(AnalysisContext context) + public override void Initialize(AnalysisContext context) { context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); - //context.EnableConcurrentExecution(); + context.EnableConcurrentExecution(); context.RegisterCompilationStartAction(CompilationStart); context.RegisterSymbolAction(c => Analyze(new RoslynSymbolAnalysisContext(c)), SymbolKinds); - context.RegisterSyntaxNodeAction(c => AnalyzeNode(c), SyntaxKind.LocalDeclarationStatement); } #pragma warning disable RS1012 // Start action has no registered actions.