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);
}
}
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);
}
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()));
}
}
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");
}
}
Aggregations