Remove analyzer project & resolve some build warnings (#2905)
* yeet analyzer & analyzer test * fix test warning * yeet analyzer from the workflow * resolve more warnings * forgot to push
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Import Project="../../Discord.Net.targets" />
|
||||
<PropertyGroup>
|
||||
<AssemblyName>Discord.Net.Analyzers</AssemblyName>
|
||||
<RootNamespace>Discord.Analyzers</RootNamespace>
|
||||
<Description>A Discord.Net extension adding support for design-time analysis of the API usage.</Description>
|
||||
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
|
||||
<IsTrimmable>false</IsTrimmable>
|
||||
<IsAotCompatible>false</IsAotCompatible>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.9.2" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Discord.Net.Commands\Discord.Net.Commands.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -1,72 +0,0 @@
|
||||
using Discord.Commands;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
using System;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
|
||||
namespace Discord.Analyzers
|
||||
{
|
||||
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||
public sealed class GuildAccessAnalyzer : DiagnosticAnalyzer
|
||||
{
|
||||
private const string DiagnosticId = "DNET0001";
|
||||
private const string Title = "Limit command to Guild contexts.";
|
||||
private const string MessageFormat = "Command method '{0}' is accessing 'Context.Guild' but is not restricted to Guild contexts.";
|
||||
private const string Description = "Accessing 'Context.Guild' in a command without limiting the command to run only in guilds.";
|
||||
private const string Category = "API Usage";
|
||||
|
||||
private static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description);
|
||||
|
||||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
|
||||
|
||||
public override void Initialize(AnalysisContext context)
|
||||
{
|
||||
context.EnableConcurrentExecution();
|
||||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
|
||||
context.RegisterSyntaxNodeAction(AnalyzeMemberAccess, SyntaxKind.SimpleMemberAccessExpression);
|
||||
}
|
||||
|
||||
private static void AnalyzeMemberAccess(SyntaxNodeAnalysisContext context)
|
||||
{
|
||||
// Bail out if the accessed member isn't named 'Guild'
|
||||
var memberAccessSymbol = context.SemanticModel.GetSymbolInfo(context.Node).Symbol;
|
||||
if (memberAccessSymbol.Name != "Guild")
|
||||
return;
|
||||
|
||||
// Bail out if it happens to be 'ContextType.Guild' in the '[RequireContext]' argument
|
||||
if (context.Node.Parent is AttributeArgumentSyntax)
|
||||
return;
|
||||
|
||||
// Bail out if the containing class doesn't derive from 'ModuleBase<T>'
|
||||
var typeNode = context.Node.FirstAncestorOrSelf<TypeDeclarationSyntax>();
|
||||
var typeSymbol = context.SemanticModel.GetDeclaredSymbol(typeNode);
|
||||
if (!typeSymbol.DerivesFromModuleBase())
|
||||
return;
|
||||
|
||||
// Bail out if the containing method isn't marked with '[Command]'
|
||||
var methodNode = context.Node.FirstAncestorOrSelf<MethodDeclarationSyntax>();
|
||||
var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodNode);
|
||||
var methodAttributes = methodSymbol.GetAttributes();
|
||||
if (!methodAttributes.Any(a => a.AttributeClass.Name == nameof(CommandAttribute)))
|
||||
return;
|
||||
|
||||
// Is the '[RequireContext]' attribute not applied to either the
|
||||
// method or the class, or its argument isn't 'ContextType.Guild'?
|
||||
var ctxAttribute = methodAttributes.SingleOrDefault(_attributeDataPredicate)
|
||||
?? typeSymbol.GetAttributes().SingleOrDefault(_attributeDataPredicate);
|
||||
|
||||
if (ctxAttribute == null || ctxAttribute.ConstructorArguments.Any(arg => !arg.Value.Equals((int)ContextType.Guild)))
|
||||
{
|
||||
// Report the diagnostic
|
||||
var diagnostic = Diagnostic.Create(Rule, context.Node.GetLocation(), methodSymbol.Name);
|
||||
context.ReportDiagnostic(diagnostic);
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly Func<AttributeData, bool> _attributeDataPredicate =
|
||||
(a => a.AttributeClass.Name == nameof(RequireContextAttribute));
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
using Discord.Commands;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using System;
|
||||
|
||||
namespace Discord.Analyzers
|
||||
{
|
||||
internal static class SymbolExtensions
|
||||
{
|
||||
private static readonly string _moduleBaseName = typeof(ModuleBase<>).Name;
|
||||
|
||||
public static bool DerivesFromModuleBase(this ITypeSymbol symbol)
|
||||
{
|
||||
for (var bType = symbol.BaseType; bType != null; bType = bType.BaseType)
|
||||
{
|
||||
if (bType.MetadataName == _moduleBaseName)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
# DNET0001
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td>TypeName</td>
|
||||
<td>GuildAccessAnalyzer</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>CheckId</td>
|
||||
<td>DNET0001</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Category</td>
|
||||
<td>API Usage</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
## Cause
|
||||
|
||||
A method identified as a command is accessing `Context.Guild` without the requisite precondition.
|
||||
|
||||
## Rule description
|
||||
|
||||
The value of `Context.Guild` is `null` if a command is invoked in a DM channel. Attempting to access
|
||||
guild properties in such a case will result in a `NullReferenceException` at runtime.
|
||||
This exception is entirely avoidable by using the library's provided preconditions.
|
||||
|
||||
## How to fix violations
|
||||
|
||||
Add the precondition `[RequireContext(ContextType.Guild)]` to the command or module class.
|
||||
Reference in New Issue
Block a user