public static void AnalyzeLogicalAndExpression(SyntaxNodeAnalysisContext context, INamedTypeSymbol expressionType) { var logicalAndExpression = (BinaryExpressionSyntax)context.Node; if (!logicalAndExpression.ContainsDiagnostics) { ExpressionSyntax expression = SyntaxInfo.NullCheckExpressionInfo(logicalAndExpression.Left, allowedKinds: NullCheckKind.NotEqualsToNull).Expression; if (expression != null && context.SemanticModel .GetTypeSymbol(expression, context.CancellationToken)? .IsReferenceType == true) { ExpressionSyntax right = logicalAndExpression.Right?.WalkDownParentheses(); if (right != null && ValidateRightExpression(right, context.SemanticModel, context.CancellationToken) && !RefactoringHelper.ContainsOutArgumentWithLocal(right, context.SemanticModel, context.CancellationToken)) { ExpressionSyntax expression2 = FindExpressionThatCanBeConditionallyAccessed(expression, right); if (expression2?.SpanContainsDirectives() == false && !logicalAndExpression.IsInExpressionTree(expressionType, context.SemanticModel, context.CancellationToken)) { context.ReportDiagnostic(DiagnosticDescriptors.UseConditionalAccess, logicalAndExpression); } } } } }
private static void Analyze( SyntaxNodeAnalysisContext context, ConditionalExpressionInfo conditionalExpressionInfo, ExpressionSyntax whenNull, ExpressionSyntax whenNotNull, SemanticModel semanticModel, CancellationToken cancellationToken) { ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(whenNotNull, cancellationToken); if (typeSymbol?.IsErrorType() == false && semanticModel.IsDefaultValue(typeSymbol, whenNull, cancellationToken) && !RefactoringHelper.ContainsOutArgumentWithLocal(whenNotNull, semanticModel, cancellationToken) && !conditionalExpressionInfo.Node.IsInExpressionTree(semanticModel, cancellationToken)) { context.ReportDiagnostic( DiagnosticDescriptors.UseConditionalAccessInsteadOfConditionalExpression, conditionalExpressionInfo.Node); } }