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);
     }
 }
Ejemplo n.º 2
0
        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);
            }
        }
Ejemplo n.º 3
0
 public static SyntaxToken GetOperatorTokenOfBinaryExpression(
     this ISyntaxFacts syntaxFacts,
     SyntaxNode node
     )
 {
     syntaxFacts.GetPartsOfBinaryExpression(node, out _, out var token, out _);
     return(token);
 }
Ejemplo n.º 4
0
        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;
Ejemplo n.º 5
0
 public static void GetPartsOfBinaryExpression(this ISyntaxFacts syntaxFacts, SyntaxNode node, out SyntaxNode left, out SyntaxNode right)
 => syntaxFacts.GetPartsOfBinaryExpression(node, out left, out _, out right);