Inheritance: Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer
        private static void AnalyzeInvocation(SyntaxNodeAnalysisContext context)
        {
            // http://stackoverflow.com/questions/29614112/how-to-get-invoked-method-name-in-roslyn
            var invocationNode   = (InvocationExpressionSyntax)context.Node;
            var invocationSymbol = context.SemanticModel.GetSymbolInfo(invocationNode.Expression).Symbol;

            if ((invocationSymbol?.ContainingType?.IsBusinessBase() ?? false))
            {
                if (context.CancellationToken.IsCancellationRequested)
                {
                    return;
                }

                if (invocationSymbol?.Name == "Save")
                {
                    FindSaveAssignmentIssueAnalyzer.CheckForCondition(context, invocationNode,
                                                                      invocationNode.Parent, FindSaveAssignmentIssueAnalyzer.saveResultIsNotAssignedRule);
                }
                else if (invocationSymbol?.Name == "SaveAsync")
                {
                    FindSaveAssignmentIssueAnalyzer.CheckForCondition(context, invocationNode,
                                                                      invocationNode.Parent?.Parent, FindSaveAssignmentIssueAnalyzer.saveAsyncResultIsNotAssignedRule);
                }
            }
        }
        private static void AnalyzeInvocation(SyntaxNodeAnalysisContext context)
        {
            // http://stackoverflow.com/questions/29614112/how-to-get-invoked-method-name-in-roslyn
            var invocationNode = (InvocationExpressionSyntax)context.Node;

            if (!invocationNode.ContainsDiagnostics)
            {
                var invocationSymbol = context.SemanticModel.GetSymbolInfo(invocationNode.Expression).Symbol;

                if ((invocationSymbol?.ContainingType?.IsBusinessBase() ?? false))
                {
                    context.CancellationToken.ThrowIfCancellationRequested();
                    var expressionStatementNode = invocationNode.FindParent <ExpressionStatementSyntax>();

                    if (invocationSymbol?.Name == Constants.SaveMethodNames.Save)
                    {
                        FindSaveAssignmentIssueAnalyzer.CheckForCondition(context, invocationNode,
                                                                          expressionStatementNode, FindSaveAssignmentIssueAnalyzer.saveResultIsNotAssignedRule);
                    }
                    else if (invocationSymbol?.Name == Constants.SaveMethodNames.SaveAsync)
                    {
                        FindSaveAssignmentIssueAnalyzer.CheckForCondition(context, invocationNode,
                                                                          expressionStatementNode, FindSaveAssignmentIssueAnalyzer.saveAsyncResultIsNotAssignedRule);
                    }
                }
            }
        }
示例#3
0
 private static void CheckForCondition(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocationNode,
                                       SyntaxNode expressionStatementParent, DiagnosticDescriptor descriptor)
 {
     if ((!expressionStatementParent?.DescendantNodesAndTokens()?.Any(_ => _.IsKind(SyntaxKind.EqualsToken)) ?? false) &&
         !(invocationNode.DescendantNodes()?.Any(_ => new ContainsInvocationExpressionWalker(_).HasIssue) ?? false) &&
         !FindSaveAssignmentIssueAnalyzer.IsReturnValue(invocationNode))
     {
         context.ReportDiagnostic(Diagnostic.Create(descriptor, invocationNode.GetLocation()));
     }
 }
 private static void CheckForCondition(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocationNode,
                                       SyntaxNode expressionStatementParent, DiagnosticDescriptor descriptor)
 {
     // Make sure the invocation's containing type is not the same as the class that contains it
     if ((invocationNode.DescendantNodesAndTokens().Any(_ => _.IsKind(SyntaxKind.DotToken)) &&
          !invocationNode.DescendantNodesAndTokens().Any(_ => _.IsKind(SyntaxKind.ThisExpression) || _.IsKind(SyntaxKind.BaseExpression))) &&
         (!expressionStatementParent?.DescendantNodesAndTokens()?.Any(_ => _.IsKind(SyntaxKind.EqualsToken)) ?? false) &&
         !(invocationNode.DescendantNodes()?.Any(_ => new ContainsInvocationExpressionWalker(_).HasIssue) ?? false) &&
         !FindSaveAssignmentIssueAnalyzer.IsReturnValue(invocationNode))
     {
         context.ReportDiagnostic(Diagnostic.Create(descriptor, invocationNode.GetLocation()));
     }
 }