public static void AnalyzeIfStatement(SyntaxNodeAnalysisContext context) { var ifStatement = (IfStatementSyntax)context.Node; if (ifStatement.IsSimpleIf()) { NotEqualsToNullExpression notEqualsToNull; if (NotEqualsToNullExpression.TryCreate(ifStatement.Condition, out notEqualsToNull)) { MemberInvocationExpression memberInvocation; if (MemberInvocationExpression.TryCreate(ifStatement.GetSingleStatementOrDefault(), out memberInvocation) && notEqualsToNull.Left.IsEquivalentTo(memberInvocation.Expression, topLevel: false) && !ifStatement.SpanContainsDirectives()) { context.ReportDiagnostic(DiagnosticDescriptors.UseConditionalAccess, ifStatement); } } } }
public static void AnalyzeIfStatement(SyntaxNodeAnalysisContext context, INamedTypeSymbol expressionType) { var ifStatement = (IfStatementSyntax)context.Node; if (ifStatement.IsSimpleIf() && !ifStatement.ContainsDiagnostics) { NotEqualsToNullExpression notEqualsToNull; if (NotEqualsToNullExpression.TryCreate(ifStatement.Condition, out notEqualsToNull)) { MemberInvocationStatement memberInvocation; if (MemberInvocationStatement.TryCreate(ifStatement.GetSingleStatementOrDefault(), out memberInvocation) && SyntaxComparer.AreEquivalent(notEqualsToNull.Left, memberInvocation.Expression) && !ifStatement.IsInExpressionTree(expressionType, context.SemanticModel, context.CancellationToken) && !ifStatement.SpanContainsDirectives()) { context.ReportDiagnostic(DiagnosticDescriptors.UseConditionalAccess, ifStatement); } } } }