mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Fix nullref in SystemConsoleShouldBeAvoidedAnalyzer
This commit is contained in:
@@ -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;
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user