From c06f2810b9643144d7b889fd6c94b56b2e12c88e Mon Sep 17 00:00:00 2001 From: Tyrrrz Date: Fri, 23 Oct 2020 18:23:58 +0300 Subject: [PATCH] Cleanup analyzers --- CliFx.Analyzers/ConsoleUsageAnalyzer.cs | 76 ++++++++++++------------- 1 file changed, 35 insertions(+), 41 deletions(-) diff --git a/CliFx.Analyzers/ConsoleUsageAnalyzer.cs b/CliFx.Analyzers/ConsoleUsageAnalyzer.cs index cbcf2ce..31e3e3c 100644 --- a/CliFx.Analyzers/ConsoleUsageAnalyzer.cs +++ b/CliFx.Analyzers/ConsoleUsageAnalyzer.cs @@ -18,55 +18,49 @@ namespace CliFx.Analyzers SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocationSyntax) { - // Get the method member access (Console.WriteLine or Console.Error.WriteLine) - if (!(invocationSyntax.Expression is MemberAccessExpressionSyntax memberAccessSyntax)) - return false; + if (invocationSyntax.Expression is MemberAccessExpressionSyntax memberAccessSyntax && + context.SemanticModel.GetSymbolInfo(memberAccessSyntax).Symbol is IMethodSymbol methodSymbol) + { + // Direct call to System.Console (e.g. System.Console.WriteLine()) + if (KnownSymbols.IsSystemConsole(methodSymbol.ContainingType)) + { + return true; + } - // Get the semantic model for the invoked method - if (!(context.SemanticModel.GetSymbolInfo(memberAccessSyntax).Symbol is IMethodSymbol methodSymbol)) - return false; - - // Check if contained within System.Console - if (KnownSymbols.IsSystemConsole(methodSymbol.ContainingType)) - return true; - - // In case with Console.Error.WriteLine that wouldn't work, we need to check parent member access too - if (!(memberAccessSyntax.Expression is MemberAccessExpressionSyntax parentMemberAccessSyntax)) - return false; - - // Get the semantic model for the parent member - if (!(context.SemanticModel.GetSymbolInfo(parentMemberAccessSyntax).Symbol is IPropertySymbol propertySymbol)) - return false; - - // Check if contained within System.Console - if (KnownSymbols.IsSystemConsole(propertySymbol.ContainingType)) - return true; + // Indirect call to System.Console (e.g. System.Console.Error.WriteLine()) + if (memberAccessSyntax.Expression is MemberAccessExpressionSyntax parentMemberAccessSyntax && + context.SemanticModel.GetSymbolInfo(parentMemberAccessSyntax).Symbol is IPropertySymbol propertySymbol) + { + return KnownSymbols.IsSystemConsole(propertySymbol.ContainingType); + } + } return false; } private static void CheckSystemConsoleUsage(SyntaxNodeAnalysisContext context) { - if (!(context.Node is InvocationExpressionSyntax invocationSyntax)) - return; + if (context.Node is InvocationExpressionSyntax invocationSyntax && + IsSystemConsoleInvocation(context, invocationSyntax)) + { + // Check if IConsole is available in scope as alternative to System.Console + var isConsoleInterfaceAvailable = invocationSyntax + .Ancestors() + .OfType() + .SelectMany(m => m.ParameterList.Parameters) + .Select(p => p.Type) + .Select(t => context.SemanticModel.GetSymbolInfo(t).Symbol) + .Where(s => s != null) + .Any(KnownSymbols.IsConsoleInterface!); - if (!IsSystemConsoleInvocation(context, invocationSyntax)) - return; - - // Check if IConsole is available in the scope as a viable alternative - var isConsoleInterfaceAvailable = invocationSyntax - .Ancestors() - .OfType() - .SelectMany(m => m.ParameterList.Parameters) - .Select(p => p.Type) - .Select(t => context.SemanticModel.GetSymbolInfo(t).Symbol) - .Where(s => s != null) - .Any(KnownSymbols.IsConsoleInterface!); - - if (!isConsoleInterfaceAvailable) - return; - - context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.CliFx0100, invocationSyntax.GetLocation())); + if (isConsoleInterfaceAvailable) + { + context.ReportDiagnostic(Diagnostic.Create( + DiagnosticDescriptors.CliFx0100, + invocationSyntax.GetLocation() + )); + } + } } public override void Initialize(AnalysisContext context)