private static void AnalyzeReturnStatement(SyntaxNodeAnalysisContext context) { MergeLocalDeclarationWithReturnStatementRefactoring.Analyze(context); var returnStatement = (ReturnStatementSyntax)context.Node; ReplaceReturnStatementWithExpressionStatementRefactoring.Analyze(context, returnStatement); }
public override void Initialize(AnalysisContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } base.Initialize(context); context.RegisterSyntaxNodeAction( f => ReplaceReturnStatementWithExpressionStatementRefactoring.Analyze(f, (YieldStatementSyntax)f.Node), SyntaxKind.YieldReturnStatement); }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); StatementSyntax statement = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <StatementSyntax>(); Debug.Assert(statement != null, $"{nameof(statement)} is null"); if (statement == null) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case DiagnosticIdentifiers.AddEmptyLineAfterLastStatementInDoStatement: { CodeAction codeAction = CodeAction.Create( "Add empty line", cancellationToken => { return(AddEmptyLineAfterLastStatementInDoStatementRefactoring.RefactorAsync( context.Document, statement, cancellationToken)); }, diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.ReplaceReturnStatementWithExpressionStatement: { switch (statement.Kind()) { case SyntaxKind.ReturnStatement: { CodeAction codeAction = CodeAction.Create( "Remove 'return'", cancellationToken => ReplaceReturnStatementWithExpressionStatementRefactoring.RefactorAsync(context.Document, (ReturnStatementSyntax)statement, cancellationToken), diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } case SyntaxKind.YieldReturnStatement: { CodeAction codeAction = CodeAction.Create( "Remove 'yield return'", cancellationToken => ReplaceReturnStatementWithExpressionStatementRefactoring.RefactorAsync(context.Document, (YieldStatementSyntax)statement, cancellationToken), diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } default: { Debug.Assert(false, statement.Kind().ToString()); break; } } break; } case DiagnosticIdentifiers.UseCoalesceExpression: { CodeAction codeAction = CodeAction.Create( "Use coalesce expression", cancellationToken => { return(UseCoalesceExpressionRefactoring.RefactorAsync( context.Document, statement, cancellationToken)); }, diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.RemoveRedundantDisposeOrCloseCall: { var expressionStatement = (ExpressionStatementSyntax)statement; var invocation = (InvocationExpressionSyntax)expressionStatement.Expression; var memberAccess = (MemberAccessExpressionSyntax)invocation.Expression; CodeAction codeAction = CodeAction.Create( $"Remove redundant '{memberAccess.Name?.Identifier.ValueText}' call", cancellationToken => RemoveRedundantDisposeOrCloseCallRefactoring.RefactorAsync(context.Document, expressionStatement, cancellationToken), diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.RemoveRedundantContinueStatement: { CodeAction codeAction = CodeAction.Create( "Remove continue;", cancellationToken => RemoveRedundantContinueStatementRefactoring.RefactorAsync(context.Document, (ContinueStatementSyntax)statement, cancellationToken), diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } } } }
private static void AnalyzeReturnStatement(SyntaxNodeAnalysisContext context) { var yieldStatement = (YieldStatementSyntax)context.Node; ReplaceReturnStatementWithExpressionStatementRefactoring.Analyze(context, yieldStatement); }