Fix nullref in SystemConsoleShouldBeAvoidedAnalyzer

This commit is contained in:
Tyrrrz
2021-03-31 11:28:48 +03:00
parent 6d46e82145
commit 54a4c32ddf
10 changed files with 36 additions and 7 deletions

View File

@@ -22,6 +22,9 @@ namespace CliFx.Analyzers
PropertyDeclarationSyntax propertyDeclaration,
IPropertySymbol property)
{
if (property.ContainingType is null)
return;
if (property.ContainingType.IsAbstract)
return;

View File

@@ -23,6 +23,9 @@ namespace CliFx.Analyzers
PropertyDeclarationSyntax propertyDeclaration,
IPropertySymbol property)
{
if (property.ContainingType is null)
return;
var option = CommandOptionSymbol.TryResolve(property);
if (option is null)
return;

View File

@@ -22,6 +22,9 @@ namespace CliFx.Analyzers
PropertyDeclarationSyntax propertyDeclaration,
IPropertySymbol property)
{
if (property.ContainingType is null)
return;
var option = CommandOptionSymbol.TryResolve(property);
if (option is null)
return;

View File

@@ -22,6 +22,9 @@ namespace CliFx.Analyzers
PropertyDeclarationSyntax propertyDeclaration,
IPropertySymbol property)
{
if (property.ContainingType is null)
return;
if (property.ContainingType.IsAbstract)
return;

View File

@@ -29,6 +29,9 @@ namespace CliFx.Analyzers
PropertyDeclarationSyntax propertyDeclaration,
IPropertySymbol property)
{
if (property.ContainingType is null)
return;
if (IsScalar(property.Type))
return;

View File

@@ -29,6 +29,9 @@ namespace CliFx.Analyzers
PropertyDeclarationSyntax propertyDeclaration,
IPropertySymbol property)
{
if (property.ContainingType is null)
return;
if (!CommandParameterSymbol.IsParameterProperty(property))
return;

View File

@@ -23,6 +23,9 @@ namespace CliFx.Analyzers
PropertyDeclarationSyntax propertyDeclaration,
IPropertySymbol property)
{
if (property.ContainingType is null)
return;
var parameter = CommandParameterSymbol.TryResolve(property);
if (parameter is null)
return;

View File

@@ -22,6 +22,9 @@ namespace CliFx.Analyzers
PropertyDeclarationSyntax propertyDeclaration,
IPropertySymbol property)
{
if (property.ContainingType is null)
return;
var parameter = CommandParameterSymbol.TryResolve(property);
if (parameter is null)
return;

View File

@@ -27,9 +27,9 @@ namespace CliFx.Analyzers
while (currentNode is MemberAccessExpressionSyntax memberAccess)
{
var symbol = context.SemanticModel.GetSymbolInfo(memberAccess).Symbol;
var member = context.SemanticModel.GetSymbolInfo(memberAccess).Symbol;
if (symbol is not null && symbol.ContainingType.DisplayNameMatches("System.Console"))
if (member?.ContainingType?.DisplayNameMatches("System.Console") == true)
{
return memberAccess;
}

View File

@@ -9,11 +9,16 @@ namespace CliFx.Analyzers.Utils.Extensions
internal static class RoslynExtensions
{
public static bool DisplayNameMatches(this ISymbol symbol, string name) =>
string.Equals(symbol.ToDisplayString(), name, StringComparison.Ordinal);
string.Equals(
// Fully qualified name, without `global::`
symbol.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat),
name,
StringComparison.Ordinal
);
public static void HandleClassDeclaration(
this AnalysisContext analysisContext,
Action<SyntaxNodeAnalysisContext, ClassDeclarationSyntax, ITypeSymbol> handler)
Action<SyntaxNodeAnalysisContext, ClassDeclarationSyntax, ITypeSymbol> analyze)
{
analysisContext.RegisterSyntaxNodeAction(ctx =>
{
@@ -24,13 +29,13 @@ namespace CliFx.Analyzers.Utils.Extensions
if (type is null)
return;
handler(ctx, classDeclaration, type);
analyze(ctx, classDeclaration, type);
}, SyntaxKind.ClassDeclaration);
}
public static void HandlePropertyDeclaration(
this AnalysisContext analysisContext,
Action<SyntaxNodeAnalysisContext, PropertyDeclarationSyntax, IPropertySymbol> handler)
Action<SyntaxNodeAnalysisContext, PropertyDeclarationSyntax, IPropertySymbol> analyze)
{
analysisContext.RegisterSyntaxNodeAction(ctx =>
{
@@ -41,7 +46,7 @@ namespace CliFx.Analyzers.Utils.Extensions
if (property is null)
return;
handler(ctx, propertyDeclaration, property);
analyze(ctx, propertyDeclaration, property);
}, SyntaxKind.PropertyDeclaration);
}
}