private static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context) { var method = (MethodDeclarationSyntax)context.Node; BlockSyntax body = method.Body; if (body == null) { return; } ExpressionSyntax expression = UseExpressionBodiedMemberAnalysis.GetExpression(body); if (expression == null) { return; } AnalyzeExpression(context, body, expression); }
private static void AnalyzeConstructorDeclaration(SyntaxNodeAnalysisContext context) { var declaration = (ConstructorDeclarationSyntax)context.Node; BlockSyntax body = declaration.Body; if (body == null) { return; } ExpressionSyntax expression = UseExpressionBodiedMemberAnalysis.GetExpression(body); if (expression == null) { return; } AnalyzeExpression(context, body, expression); }
private static void AnalyzeLocalFunctionStatement(SyntaxNodeAnalysisContext context) { var localFunction = (LocalFunctionStatementSyntax)context.Node; BlockSyntax body = localFunction.Body; if (body == null) { return; } ExpressionSyntax expression = UseExpressionBodiedMemberAnalysis.GetExpression(body); if (expression == null) { return; } AnalyzeExpression(context, body, expression); }
private static void AnalyzeAccessorDeclaration(SyntaxNodeAnalysisContext context) { var accessor = (AccessorDeclarationSyntax)context.Node; if (accessor.ExpressionBody == null && !accessor.AttributeLists.Any()) { BlockSyntax body = accessor.Body; ExpressionSyntax expression = UseExpressionBodiedMemberAnalysis.GetExpression(body); if (expression?.IsSingleLine() == true) { if (accessor.Parent is AccessorListSyntax accessorList) { SyntaxList <AccessorDeclarationSyntax> accessors = accessorList.Accessors; if (accessors.Count == 1 && accessors.First().IsKind(SyntaxKind.GetAccessorDeclaration)) { if (accessorList.DescendantTrivia().All(f => f.IsWhitespaceOrEndOfLineTrivia())) { ReportDiagnostic(context, accessorList, expression); context.ReportToken(DiagnosticDescriptors.UseExpressionBodiedMemberFadeOut, accessor.Keyword); context.ReportBraces(DiagnosticDescriptors.UseExpressionBodiedMemberFadeOut, body); } return; } } if (accessor.DescendantTrivia().All(f => f.IsWhitespaceOrEndOfLineTrivia())) { ReportDiagnostic(context, body, expression); } } } }