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, PropertyDeclarationSyntax propertyDeclaration,
IPropertySymbol property) IPropertySymbol property)
{ {
if (property.ContainingType is null)
return;
if (property.ContainingType.IsAbstract) if (property.ContainingType.IsAbstract)
return; return;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -27,9 +27,9 @@ namespace CliFx.Analyzers
while (currentNode is MemberAccessExpressionSyntax memberAccess) 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; return memberAccess;
} }

View File

@@ -9,11 +9,16 @@ namespace CliFx.Analyzers.Utils.Extensions
internal static class RoslynExtensions internal static class RoslynExtensions
{ {
public static bool DisplayNameMatches(this ISymbol symbol, string name) => 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( public static void HandleClassDeclaration(
this AnalysisContext analysisContext, this AnalysisContext analysisContext,
Action<SyntaxNodeAnalysisContext, ClassDeclarationSyntax, ITypeSymbol> handler) Action<SyntaxNodeAnalysisContext, ClassDeclarationSyntax, ITypeSymbol> analyze)
{ {
analysisContext.RegisterSyntaxNodeAction(ctx => analysisContext.RegisterSyntaxNodeAction(ctx =>
{ {
@@ -24,13 +29,13 @@ namespace CliFx.Analyzers.Utils.Extensions
if (type is null) if (type is null)
return; return;
handler(ctx, classDeclaration, type); analyze(ctx, classDeclaration, type);
}, SyntaxKind.ClassDeclaration); }, SyntaxKind.ClassDeclaration);
} }
public static void HandlePropertyDeclaration( public static void HandlePropertyDeclaration(
this AnalysisContext analysisContext, this AnalysisContext analysisContext,
Action<SyntaxNodeAnalysisContext, PropertyDeclarationSyntax, IPropertySymbol> handler) Action<SyntaxNodeAnalysisContext, PropertyDeclarationSyntax, IPropertySymbol> analyze)
{ {
analysisContext.RegisterSyntaxNodeAction(ctx => analysisContext.RegisterSyntaxNodeAction(ctx =>
{ {
@@ -41,7 +46,7 @@ namespace CliFx.Analyzers.Utils.Extensions
if (property is null) if (property is null)
return; return;
handler(ctx, propertyDeclaration, property); analyze(ctx, propertyDeclaration, property);
}, SyntaxKind.PropertyDeclaration); }, SyntaxKind.PropertyDeclaration);
} }
} }