private void AnalyzeAssignmentExpression(SyntaxNodeAnalysisContext context) { if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true) { return; } var assignment = (AssignmentExpressionSyntax)context.Node; ExpressionSyntax left = assignment.Left; ExpressionSyntax right = assignment.Right; if (left?.IsMissing == false && right?.IsNumericLiteralExpression(1) == true) { ITypeSymbol typeSymbol = context.SemanticModel.GetTypeInfo(left, context.CancellationToken).Type; if (typeSymbol?.SupportsPrefixOrPostfixUnaryOperator() == true && !assignment.SpanContainsDirectives()) { context.ReportDiagnostic( DiagnosticDescriptors.UsePostfixUnaryOperatorInsteadOfAssignment, assignment.GetLocation(), UsePostfixUnaryOperatorInsteadOfAssignmentRefactoring.GetOperatorText(assignment)); } } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out AssignmentExpressionSyntax assignment)) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case DiagnosticIdentifiers.UseCompoundAssignment: { var binaryExpression = (BinaryExpressionSyntax)assignment.Right; string operatorText = UseCompoundAssignmentRefactoring.GetCompoundOperatorText(binaryExpression); CodeAction codeAction = CodeAction.Create( $"Use {operatorText} operator", cancellationToken => UseCompoundAssignmentRefactoring.RefactorAsync(context.Document, assignment, cancellationToken), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.UsePostfixUnaryOperatorInsteadOfAssignment: { string operatorText = UsePostfixUnaryOperatorInsteadOfAssignmentRefactoring.GetOperatorText(assignment); CodeAction codeAction = CodeAction.Create( $"Use {operatorText} operator", c => UsePostfixUnaryOperatorInsteadOfAssignmentRefactoring.RefactorAsync(context.Document, assignment, c), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.RemoveRedundantDelegateCreation: { CodeAction codeAction = CodeAction.Create( "Remove redundant delegate creation", cancellationToken => { return(RemoveRedundantDelegateCreationRefactoring.RefactorAsync( context.Document, (ObjectCreationExpressionSyntax)assignment.Right, cancellationToken)); }, GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } } } }
private void AnalyzeSimpleAssignment(SyntaxNodeAnalysisContext context) { if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true) { return; } var assignment = (AssignmentExpressionSyntax)context.Node; ExpressionSyntax left = assignment.Left; ExpressionSyntax right = assignment.Right; if (left?.IsMissing == false && right?.IsMissing == false) { if (!assignment.IsParentKind(SyntaxKind.ObjectInitializerExpression) && SupportsCompoundAssignment(right)) { var binaryExpression = (BinaryExpressionSyntax)right; ExpressionSyntax binaryLeft = binaryExpression.Left; ExpressionSyntax binaryRight = binaryExpression.Right; if (binaryLeft?.IsMissing == false && binaryRight?.IsMissing == false && left.IsEquivalentTo(binaryLeft, topLevel: false) && ContainsOnlyWhitespaceOrEndOfLineTrivia(assignment)) { context.ReportDiagnostic( DiagnosticDescriptors.SimplifyAssignmentExpression, assignment.GetLocation()); context.FadeOutNode(DiagnosticDescriptors.SimplifyAssignmentExpressionFadeOut, binaryLeft); } } if (right.IsKind(SyntaxKind.AddExpression, SyntaxKind.SubtractExpression)) { var binaryExpression = (BinaryExpressionSyntax)right; ExpressionSyntax binaryLeft = binaryExpression.Left; ExpressionSyntax binaryRight = binaryExpression.Right; if (binaryLeft?.IsMissing == false && binaryRight?.IsNumericLiteralExpression(1) == true) { ITypeSymbol typeSymbol = context.SemanticModel.GetTypeInfo(left, context.CancellationToken).Type; if (typeSymbol?.SupportsPrefixOrPostfixUnaryOperator() == true && left.IsEquivalentTo(binaryLeft, topLevel: false) && !assignment.SpanContainsDirectives()) { context.ReportDiagnostic( DiagnosticDescriptors.UsePostfixUnaryOperatorInsteadOfAssignment, assignment.GetLocation(), UsePostfixUnaryOperatorInsteadOfAssignmentRefactoring.GetOperatorText(assignment)); } } } } }
private void AnalyzeAssignmentExpression(SyntaxNodeAnalysisContext context) { var assignment = (AssignmentExpressionSyntax)context.Node; UsePostfixUnaryOperatorInsteadOfAssignmentRefactoring.Analyze(context, assignment); RemoveRedundantDelegateCreationRefactoring.Analyze(context, assignment); }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); AssignmentExpressionSyntax assignment = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <AssignmentExpressionSyntax>(); if (assignment == null) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case DiagnosticIdentifiers.SimplifyAssignmentExpression: { CodeAction codeAction = CodeAction.Create( "Simplify assignment expression", cancellationToken => { return(SimplifyAssignmentExpressionRefactoring.RefactorAsync( context.Document, assignment, cancellationToken)); }, diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.UsePostfixUnaryOperatorInsteadOfAssignment: { SyntaxKind kind = UsePostfixUnaryOperatorInsteadOfAssignmentRefactoring.GetPostfixUnaryOperatorKind(assignment); string operatorText = UsePostfixUnaryOperatorInsteadOfAssignmentRefactoring.GetOperatorText(kind); CodeAction codeAction = CodeAction.Create( $"Use {operatorText} operator", cancellationToken => { return(UsePostfixUnaryOperatorInsteadOfAssignmentRefactoring.RefactorAsync( context.Document, assignment, kind, cancellationToken)); }, diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } } } }
private void AnalyzeSimpleAssignment(SyntaxNodeAnalysisContext context) { var assignment = (AssignmentExpressionSyntax)context.Node; if (UseCompoundAssignmentRefactoring.CanRefactor(assignment)) { var binaryExpression = (BinaryExpressionSyntax)assignment.Right; context.ReportDiagnostic(DiagnosticDescriptors.UseCompoundAssignment, assignment, UseCompoundAssignmentRefactoring.GetCompoundOperatorText(binaryExpression)); context.ReportNode(DiagnosticDescriptors.UseCompoundAssignmentFadeOut, binaryExpression.Left); } UsePostfixUnaryOperatorInsteadOfAssignmentRefactoring.Analyze(context, assignment); }