private void AddExpressionsAndOperators( PrecedenceKind precedence, SyntaxNode expr, ArrayBuilder <SyntaxNodeOrToken> result ) { if ( expr is TBinaryExpressionSyntax && precedence == _precedenceService.GetPrecedenceKind(expr) ) { _syntaxFacts.GetPartsOfBinaryExpression( expr, out var left, out var opToken, out var right ); AddExpressionsAndOperators(precedence, left, result); result.Add(opToken); AddExpressionsAndOperators(precedence, right, result); } else { result.Add(expr); } }
private static bool TryAnalyzeBinaryExpressionCondition( ISyntaxFacts syntaxFacts, TBinaryExpressionSyntax condition, [NotNullWhen(true)] out SyntaxNode?conditionPartToCheck, out bool isEquals ) { var syntaxKinds = syntaxFacts.SyntaxKinds; isEquals = syntaxKinds.ReferenceEqualsExpression == condition.RawKind; var isNotEquals = syntaxKinds.ReferenceNotEqualsExpression == condition.RawKind; if (!isEquals && !isNotEquals) { conditionPartToCheck = null; return(false); } else { syntaxFacts.GetPartsOfBinaryExpression( condition, out var conditionLeft, out var conditionRight ); conditionPartToCheck = GetConditionPartToCheck( syntaxFacts, conditionLeft, conditionRight ); return(conditionPartToCheck != null); } }
public static SyntaxToken GetOperatorTokenOfBinaryExpression( this ISyntaxFacts syntaxFacts, SyntaxNode node ) { syntaxFacts.GetPartsOfBinaryExpression(node, out _, out var token, out _); return(token); }
private void AnalyzeBinaryOperator(OperationAnalysisContext context) { var operation = (IBinaryOperation)context.Operation; if (operation.OperatorMethod is not null) { // We shouldn't report diagnostic on overloaded operator as the behavior can change. return; } if (operation.OperatorKind is not(BinaryOperatorKind.Equals or BinaryOperatorKind.NotEquals)) { return; } if (!_syntaxFacts.IsBinaryExpression(operation.Syntax)) { return; } var rightOperand = operation.RightOperand; var leftOperand = operation.LeftOperand; if (rightOperand.Type is null || leftOperand.Type is null) { return; } if (rightOperand.Type.SpecialType != SpecialType.System_Boolean || leftOperand.Type.SpecialType != SpecialType.System_Boolean) { return; } var isOperatorEquals = operation.OperatorKind == BinaryOperatorKind.Equals; _syntaxFacts.GetPartsOfBinaryExpression(operation.Syntax, out _, out var operatorToken, out _); var properties = ImmutableDictionary.CreateBuilder <string, string?>(); if (TryGetLiteralValue(rightOperand) == isOperatorEquals) { properties.Add(RedundantEqualityConstants.RedundantSide, RedundantEqualityConstants.Right); } else if (TryGetLiteralValue(leftOperand) == isOperatorEquals) { properties.Add(RedundantEqualityConstants.RedundantSide, RedundantEqualityConstants.Left); } if (properties.Count == 1) { context.ReportDiagnostic(Diagnostic.Create(Descriptor, operatorToken.GetLocation(), additionalLocations: new[] { operation.Syntax.GetLocation() }, properties: properties.ToImmutable())); } return;
public static void GetPartsOfBinaryExpression(this ISyntaxFacts syntaxFacts, SyntaxNode node, out SyntaxNode left, out SyntaxNode right) => syntaxFacts.GetPartsOfBinaryExpression(node, out left, out _, out right);