private static void Analyze(SyntaxNodeAnalysisContext context, SyntaxNode node, BlockSyntax body) { if (body != null) { SyntaxList <StatementSyntax> statements = body.Statements; if (statements.Count == 2) { var ifStatement = statements[0] as IfStatementSyntax; if (ifStatement != null) { var returnStatement = statements[1] as ReturnStatementSyntax; if (returnStatement != null && CanRefactor(context, ifStatement, returnStatement)) { TextSpan span = TextSpan.FromBounds(ifStatement.SpanStart, returnStatement.Span.End); if (!body.ContainsDirectives(TextSpan.FromBounds(ifStatement.SpanStart, returnStatement.Span.End))) { context.ReportDiagnostic( DiagnosticDescriptors.SimplifyLazilyInitializedProperty, Location.Create(node.SyntaxTree, span)); } } } } } }
private static void Analyze(SyntaxNodeAnalysisContext context, SyntaxNode node, BlockSyntax body) { if (body?.ContainsDiagnostics == false) { SyntaxList <StatementSyntax> statements = body.Statements; if (statements.Count == 2 && statements[0].IsKind(SyntaxKind.IfStatement) && statements[1].IsKind(SyntaxKind.ReturnStatement)) { var ifStatement = (IfStatementSyntax)statements[0]; var returnStatement = (ReturnStatementSyntax)statements[1]; if (CanRefactor(context, ifStatement, returnStatement)) { TextSpan span = TextSpan.FromBounds(ifStatement.SpanStart, returnStatement.Span.End); if (!body.ContainsDirectives(TextSpan.FromBounds(ifStatement.SpanStart, returnStatement.Span.End))) { context.ReportDiagnostic( DiagnosticDescriptors.SimplifyLazilyInitializedProperty, Location.Create(node.SyntaxTree, span)); } } } } }
private static void Analyze(SyntaxNodeAnalysisContext context, SyntaxNode node, BlockSyntax body) { if (body == null) { return; } if (body.ContainsDiagnostics) { return; } SyntaxList <StatementSyntax> statements = body.Statements; if (statements.Count != 2) { return; } if (!(statements[0] is IfStatementSyntax ifStatement)) { return; } if (!(statements[1] is ReturnStatementSyntax returnStatement)) { return; } TextSpan span = TextSpan.FromBounds(ifStatement.SpanStart, returnStatement.Span.End); if (body.ContainsDirectives(span)) { return; } SimpleIfStatementInfo simpleIf = SyntaxInfo.SimpleIfStatementInfo(ifStatement); if (!simpleIf.Success) { return; } StatementSyntax statement = simpleIf.Statement.SingleNonBlockStatementOrDefault(); if (statement == null) { return; } SemanticModel semanticModel = context.SemanticModel; CancellationToken cancellationToken = context.CancellationToken; NullCheckExpressionInfo nullCheck = SyntaxInfo.NullCheckExpressionInfo(simpleIf.Condition, allowedKinds: NullCheckKind.IsNull, semanticModel: semanticModel, cancellationToken: cancellationToken); if (!nullCheck.Success) { return; } IdentifierNameSyntax identifierName = GetIdentifierName(nullCheck.Expression); if (identifierName == null) { return; } if (!(semanticModel.GetSymbol(identifierName, cancellationToken) is IFieldSymbol fieldSymbol)) { return; } SimpleAssignmentStatementInfo assignmentInfo = SyntaxInfo.SimpleAssignmentStatementInfo(statement); if (!assignmentInfo.Success) { return; } string name = identifierName.Identifier.ValueText; IdentifierNameSyntax identifierName2 = GetIdentifierName(assignmentInfo.Left); if (!IsBackingField(identifierName2, name, fieldSymbol, semanticModel, cancellationToken)) { return; } IdentifierNameSyntax identifierName3 = GetIdentifierName(returnStatement.Expression, semanticModel, cancellationToken); if (!IsBackingField(identifierName3, name, fieldSymbol, semanticModel, cancellationToken)) { return; } context.ReportDiagnostic( DiagnosticDescriptors.SimplifyLazyInitialization, Location.Create(node.SyntaxTree, span)); }