committed by
GitHub
29 changed files with 136 additions and 9 deletions
@ -0,0 +1,9 @@ |
|||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|||
<ItemGroup> |
|||
<ProjectReference Include="$(MSBuildThisFileDirectory)..\src\Tools\DevAnalyzers\DevAnalyzers.csproj" |
|||
PrivateAssets="all" |
|||
ReferenceOutputAssembly="false" |
|||
OutputItemType="Analyzer" |
|||
SetTargetFramework="TargetFramework=netstandard2.0"/> |
|||
</ItemGroup> |
|||
</Project> |
|||
@ -0,0 +1,17 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netstandard2.0</TargetFramework> |
|||
<LangVersion>10</LangVersion> |
|||
<Nullable>enable</Nullable> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3"> |
|||
<PrivateAssets>all</PrivateAssets> |
|||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> |
|||
</PackageReference> |
|||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,40 @@ |
|||
using System.Collections.Immutable; |
|||
using Microsoft.CodeAnalysis; |
|||
using Microsoft.CodeAnalysis.Diagnostics; |
|||
|
|||
namespace DevAnalyzers; |
|||
|
|||
[DiagnosticAnalyzer(LanguageNames.CSharp)] |
|||
public class GenericVirtualAnalyzer : DiagnosticAnalyzer |
|||
{ |
|||
public const string DiagnosticId = "AVADEV1001"; |
|||
|
|||
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor( |
|||
DiagnosticId, |
|||
"Do not use generic virtual methods", |
|||
"Method '{0}' is a generic virtual method", |
|||
"Performance", |
|||
DiagnosticSeverity.Warning, |
|||
isEnabledByDefault: true, |
|||
description: "Generic virtual methods affect JIT startup time adversly and should be avoided."); |
|||
|
|||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); |
|||
|
|||
public override void Initialize(AnalysisContext context) |
|||
{ |
|||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
|||
context.EnableConcurrentExecution(); |
|||
context.RegisterSymbolAction(AnalyzeMethod, SymbolKind.Method); |
|||
} |
|||
|
|||
private static void AnalyzeMethod(SymbolAnalysisContext context) |
|||
{ |
|||
var symbol = (IMethodSymbol)context.Symbol; |
|||
|
|||
if (symbol.IsGenericMethod && |
|||
(symbol.IsVirtual || symbol.ContainingType.TypeKind == TypeKind.Interface)) |
|||
{ |
|||
context.ReportDiagnostic(Diagnostic.Create(Rule, symbol.Locations[0], symbol.Name)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,8 @@ |
|||
// This file is used by Code Analysis to maintain SuppressMessage
|
|||
// attributes that are applied to this project.
|
|||
// Project-level suppressions either have no target or are given
|
|||
// a specific target and scoped to a namespace, type, member, etc.
|
|||
|
|||
using System.Diagnostics.CodeAnalysis; |
|||
|
|||
[assembly: SuppressMessage("MicrosoftCodeAnalysisReleaseTracking", "RS2008:Enable analyzer release tracking")] |
|||
Loading…
Reference in new issue