Search in sources :

Example 1 with BinaryExpressionLike

use of org.sonar.uast.helpers.BinaryExpressionLike in project sonar-go by SonarSource.

the class CognitiveComplexity method flattenBinaryExpressions.

private static void flattenBinaryExpressions(UastNode node, List<BinaryExpressionLike> expressionAsList) {
    ParenthesizedLike parenthesizedNode = ParenthesizedLike.from(node);
    if (parenthesizedNode != null) {
        flattenBinaryExpressions(parenthesizedNode.expression(), expressionAsList);
        return;
    }
    BinaryExpressionLike binaryExpression = BinaryExpressionLike.from(node);
    if (binaryExpression != null && logicalOperatorKind(binaryExpression) != null) {
        flattenBinaryExpressions(binaryExpression.leftOperand(), expressionAsList);
        expressionAsList.add(binaryExpression);
        flattenBinaryExpressions(binaryExpression.rightOperand(), expressionAsList);
    }
}
Also used : ParenthesizedLike(org.sonar.uast.helpers.ParenthesizedLike) BinaryExpressionLike(org.sonar.uast.helpers.BinaryExpressionLike)

Example 2 with BinaryExpressionLike

use of org.sonar.uast.helpers.BinaryExpressionLike in project sonar-go by SonarSource.

the class CognitiveComplexity method visitBinaryExpression.

private void visitBinaryExpression(UastNode node) {
    List<BinaryExpressionLike> expressionAsList = new ArrayList<>();
    flattenBinaryExpressions(node, expressionAsList);
    UastNode.Kind lastLogicalOperatorKind = null;
    for (BinaryExpressionLike binaryExpression : expressionAsList) {
        UastNode.Kind kind = logicalOperatorKind(binaryExpression);
        if (kind != null) {
            if (binaryExpression.node() != node) {
                ignoredNode.add(binaryExpression.node());
            }
            if (kind != lastLogicalOperatorKind) {
                increaseComplexityByOne(binaryExpression.operator());
            }
        }
        lastLogicalOperatorKind = kind;
    }
    visitChildren(node);
}
Also used : ArrayList(java.util.ArrayList) UastNode(org.sonar.uast.UastNode) BinaryExpressionLike(org.sonar.uast.helpers.BinaryExpressionLike)

Example 3 with BinaryExpressionLike

use of org.sonar.uast.helpers.BinaryExpressionLike in project sonar-go by SonarSource.

the class BinaryOperatorIdenticalExpressionsCheck method visitNode.

@Override
public void visitNode(UastNode node) {
    BinaryExpressionLike binaryExpression = BinaryExpressionLike.from(node);
    if (binaryExpression != null && !isExcluded(binaryExpression) && syntacticallyEquivalent(binaryExpression.leftOperand(), binaryExpression.rightOperand())) {
        String operator = binaryExpression.operator().joinTokens();
        reportIssue(binaryExpression.rightOperand(), "Correct one of the identical sub-expressions on both sides of operator \"" + operator + "\".", new Issue.Message(binaryExpression.leftOperand()));
    }
}
Also used : Issue(org.sonar.commonruleengine.Issue) BinaryExpressionLike(org.sonar.uast.helpers.BinaryExpressionLike)

Example 4 with BinaryExpressionLike

use of org.sonar.uast.helpers.BinaryExpressionLike in project sonar-go by SonarSource.

the class RedundantBooleanLiteralCheck method visitNode.

@Override
public void visitNode(UastNode node) {
    BinaryExpressionLike binExpr = BinaryExpressionLike.from(node);
    if (binExpr == null) {
        return;
    }
    Optional<UastNode> booleanLiteralOperand = findBooleanLiteralOperand(binExpr);
    if (binExpr.operator().is(UastNode.Kind.OPERATOR_EQUAL, UastNode.Kind.OPERATOR_NOT_EQUAL, UastNode.Kind.OPERATOR_LOGICAL_AND, UastNode.Kind.OPERATOR_LOGICAL_OR) && booleanLiteralOperand.isPresent()) {
        reportIssue(booleanLiteralOperand.get(), "Remove this redundant boolean literal");
    }
}
Also used : UastNode(org.sonar.uast.UastNode) BinaryExpressionLike(org.sonar.uast.helpers.BinaryExpressionLike)

Aggregations

BinaryExpressionLike (org.sonar.uast.helpers.BinaryExpressionLike)4 UastNode (org.sonar.uast.UastNode)2 ArrayList (java.util.ArrayList)1 Issue (org.sonar.commonruleengine.Issue)1 ParenthesizedLike (org.sonar.uast.helpers.ParenthesizedLike)1