public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { if (!Settings.IsCodeFixEnabled(CodeFixIdentifiers.RemoveUnusedLabel)) { return; } SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out LabeledStatementSyntax labeledStatement)) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case CompilerDiagnosticIdentifiers.LabelHasNotBeenReferenced: { CodeFixRegistrator.RemoveStatement(context, diagnostic, labeledStatement, title: "Remove unused label"); break; } } } }
public override async Task RegisterCodeFixesAsync(CodeFixContext context) { Diagnostic diagnostic = context.Diagnostics[0]; if (!Settings.IsEnabled(diagnostic.Id, CodeFixIdentifiers.RemoveUnusedLabel)) { return; } SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out LabeledStatementSyntax labeledStatement)) { return; } CodeFixRegistrator.RemoveStatement(context, diagnostic, labeledStatement, title: "Remove unused label"); }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { Diagnostic diagnostic = context.Diagnostics[0]; if (!Settings.IsEnabled(diagnostic.Id, CodeFixIdentifiers.RemoveRedundantAssignment)) { return; } SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out AssignmentExpressionSyntax assignmentExpression)) { return; } if (!(assignmentExpression.Parent is ExpressionStatementSyntax expressionStatement)) { return; } CodeFixRegistrator.RemoveStatement(context, diagnostic, expressionStatement, title: "Remove assignment"); }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { if (!Settings.IsEnabled(CodeFixIdentifiers.RemoveRedundantAssignment)) { return; } SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out AssignmentExpressionSyntax assignmentExpression)) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case CompilerDiagnosticIdentifiers.AssignmentMadeToSameVariable: { if (!Settings.IsEnabled(CodeFixIdentifiers.RemoveRedundantAssignment)) { break; } if (!(assignmentExpression.Parent is ExpressionStatementSyntax expressionStatement)) { break; } CodeFixRegistrator.RemoveStatement(context, diagnostic, expressionStatement, title: "Remove assignment"); break; } } } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { if (!Settings.IsAnyCodeFixEnabled( CodeFixIdentifiers.RemoveEmptySwitchStatement, CodeFixIdentifiers.RemoveJumpStatement, CodeFixIdentifiers.ReplaceBreakWithContinue)) { return; } SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out StatementSyntax statement)) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case CompilerDiagnosticIdentifiers.EmptySwitchBlock: { if (!Settings.IsCodeFixEnabled(CodeFixIdentifiers.RemoveEmptySwitchStatement)) { break; } if (!(statement is SwitchStatementSyntax switchStatement)) { break; } CodeFixRegistrator.RemoveStatement(context, diagnostic, switchStatement); break; } case CompilerDiagnosticIdentifiers.NoEnclosingLoopOutOfWhichToBreakOrContinue: { if (Settings.IsCodeFixEnabled(CodeFixIdentifiers.RemoveJumpStatement)) { CodeFixRegistrator.RemoveStatement(context, diagnostic, statement); } if (Settings.IsCodeFixEnabled(CodeFixIdentifiers.ReplaceBreakWithContinue) && statement.Kind() == SyntaxKind.BreakStatement) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); if (semanticModel.GetEnclosingSymbol(statement.SpanStart, context.CancellationToken) is IMethodSymbol methodSymbol) { if (methodSymbol.ReturnsVoid || (methodSymbol.IsAsync && methodSymbol.ReturnType.Equals(semanticModel.GetTypeByMetadataName(MetadataNames.System_Threading_Tasks_Task)))) { CodeAction codeAction = CodeAction.Create( "Replace 'break' with 'return'", cancellationToken => { var breakStatement = (BreakStatementSyntax)statement; SyntaxToken breakKeyword = breakStatement.BreakKeyword; ReturnStatementSyntax newStatement = SyntaxFactory.ReturnStatement( SyntaxFactory.Token(breakKeyword.LeadingTrivia, SyntaxKind.ReturnKeyword, breakKeyword.TrailingTrivia), null, breakStatement.SemicolonToken); return(context.Document.ReplaceNodeAsync(statement, newStatement, context.CancellationToken)); }, GetEquivalenceKey(diagnostic, CodeFixIdentifiers.ReplaceBreakWithContinue)); context.RegisterCodeFix(codeAction, diagnostic); } } } break; } } } }