@Override public void visitNode(Tree tree) { BinaryExpressionTree binaryExpression = (BinaryExpressionTree) tree; if (isInvalidTest(binaryExpression.leftOperand(), binaryExpression.rightOperand())) { reportIssue(binaryExpression.operatorToken(), "Only the sign of the result should be examined."); } }
private static boolean isEmptyComparison(BinaryExpressionTree tree) { boolean result; if (isEqualityExpression(tree)) { result = isZero(tree.leftOperand()) || isZero(tree.rightOperand()); } else if (tree.is(Kind.GREATER_THAN_OR_EQUAL_TO) || tree.is(Kind.LESS_THAN)) { result = isZero(tree.leftOperand()) || isOne(tree.rightOperand()); } else if (tree.is(Kind.GREATER_THAN) || tree.is(Kind.LESS_THAN_OR_EQUAL_TO)) { result = isOne(tree.leftOperand()) || isZero(tree.rightOperand()); } else { result = false; } return result; }
private static boolean isIndirectEquality(BinaryExpressionTree binaryExpressionTree, Tree.Kind indirectOperator, Tree.Kind comparator1, Tree.Kind comparator2) { if (binaryExpressionTree.is(indirectOperator) && binaryExpressionTree.leftOperand().is(comparator1, comparator2)) { BinaryExpressionTree leftOp = (BinaryExpressionTree) binaryExpressionTree.leftOperand(); if (binaryExpressionTree.rightOperand().is(comparator1, comparator2)) { BinaryExpressionTree rightOp = (BinaryExpressionTree) binaryExpressionTree.rightOperand(); if (leftOp.kind().equals(rightOp.kind())) { //same operator return SyntacticEquivalence.areEquivalent(leftOp.leftOperand(), rightOp.rightOperand()) && SyntacticEquivalence.areEquivalent(leftOp.rightOperand(), rightOp.leftOperand()); } else { //different operator return SyntacticEquivalence.areEquivalent(leftOp.leftOperand(), rightOp.leftOperand()) && SyntacticEquivalence.areEquivalent(leftOp.rightOperand(), rightOp.rightOperand()); } } } return false; }
@Override public void visitBinaryExpression(BinaryExpressionTree tree) { if (tree.is(Tree.Kind.CONDITIONAL_AND, Tree.Kind.CONDITIONAL_OR)) { blame.add(tree.operatorToken()); } super.visitBinaryExpression(tree); } }
private static boolean isAndWithNullComparison(BinaryExpressionTree tree) { return tree.is(Tree.Kind.CONDITIONAL_AND) && isEqualNullComparison(tree.leftOperand()); }
private static ExpressionTree getNonNullOperand(ExpressionTree expressionTree) { BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) ExpressionUtils.skipParentheses(expressionTree); if (binaryExpressionTree.leftOperand().is(Tree.Kind.NULL_LITERAL)) { return binaryExpressionTree.rightOperand(); } return binaryExpressionTree.leftOperand(); }
@Override public void visitNode(Tree tree) { if(tree.is(Tree.Kind.METHOD_INVOCATION)) { MethodInvocationTree mit = (MethodInvocationTree) tree; if (EQUALS_MATCHER.anyMatch(mit)) { reportIssue(mit.methodSelect(), "Equality tests should not be made with floating point values."); } return; } BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) tree; if (binaryExpressionTree.is(Tree.Kind.CONDITIONAL_AND, Tree.Kind.CONDITIONAL_OR) && isIndirectEquality(binaryExpressionTree)) { binaryExpressionTree = (BinaryExpressionTree) binaryExpressionTree.leftOperand(); } if ((hasFloatingType(binaryExpressionTree.leftOperand()) || hasFloatingType(binaryExpressionTree.rightOperand())) && !isNanTest(binaryExpressionTree)) { reportIssue(binaryExpressionTree.operatorToken(), "Equality tests should not be made with floating point values."); } }
@Override public void visitNode(Tree tree) { if (!hasSemantic()) { return; } BinaryExpressionTree binaryExpression = (BinaryExpressionTree) tree; ExpressionTree leftOp = ExpressionUtils.skipParentheses(binaryExpression.leftOperand()); ExpressionTree rightOp = ExpressionUtils.skipParentheses(binaryExpression.rightOperand()); if ((is(Tree.Kind.EQUAL_TO, leftOp, rightOp) && nullCheckWithInstanceOf(leftOp, rightOp, binaryExpression.kind(), Tree.Kind.CONDITIONAL_OR)) || (is(Tree.Kind.NOT_EQUAL_TO, leftOp, rightOp) && nullCheckWithInstanceOf(leftOp, rightOp, binaryExpression.kind(), Tree.Kind.CONDITIONAL_AND))) { reportIssue(treeToReport(leftOp, rightOp), "Remove this unnecessary null check; \"instanceof\" returns false for nulls."); } }
@Override public void visitNode(Tree tree) { BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) tree; if (hasEquivalentOperand(binaryExpressionTree)) { addIssue(binaryExpressionTree.rightOperand(), "Identical sub-expressions on both sides of operator \"" + binaryExpressionTree.operatorToken().text() + "\""); } }
@Override public void visitNode(Tree tree) { BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) tree; if (isBoolean(binaryExpressionTree.leftOperand())) { String operator = binaryExpressionTree.operatorToken().text(); String replacement = REPLACEMENTS.get(operator); addIssue(tree, "Correct this \"" + operator + "\" to \"" + replacement + "\"."); } }
@Override public void visitBinaryExpression(BinaryExpressionTree tree) { if (tree.is(CONDITIONAL_AND, CONDITIONAL_OR) && !ignored.contains(tree)) { List<BinaryExpressionTree> flattenedLogicalExpressions = flattenLogicalExpression(tree).collect(Collectors.toList()); BinaryExpressionTree previous = null; for (BinaryExpressionTree current : flattenedLogicalExpressions) { if (previous == null || !previous.is(current.kind())) { increaseComplexityByOne(current.operatorToken()); } previous = current; } } super.visitBinaryExpression(tree); }
@Override public void visitBinaryExpression(BinaryExpressionTree tree) { Tree.Kind peek = stack.peek(); Tree.Kind kind = tree.kind(); if (requiresParenthesis(peek, kind)) { raiseIssue(tree.operatorToken().line(), tree); } stack.push(kind); super.visitBinaryExpression(tree); stack.pop(); }
private static SyntaxToken operatorToken(Tree tree) { if (tree instanceof BinaryExpressionTree) { return ((BinaryExpressionTree) tree).operatorToken(); } return ((AssignmentExpressionTree) tree).operatorToken(); }
@CheckForNull public static ExpressionTree equivalentOperand(BinaryExpressionTree tree, ExpressionTree rightOperand) { if (isNanTest(tree) || isLeftShiftOnOne(tree)) { return null; } return equivalentOperand(tree.leftOperand(), rightOperand, tree.kind()); }
private void buildConditionalOr(BinaryExpressionTree tree) { Block trueBlock = currentBlock; currentBlock = createBlock(trueBlock); // process RHS build(tree.rightOperand()); // process LHS buildConditionalBinaryLHS(tree, trueBlock, currentBlock); }
private static boolean isBiggerTreeWithSameTruthiness(@Nullable Tree parent, Tree child, boolean isTrue) { if (parent == null) { return false; } if (parent.is(Tree.Kind.PARENTHESIZED_EXPRESSION)) { return true; } Tree.Kind operator = isTrue ? Tree.Kind.CONDITIONAL_OR : Tree.Kind.CONDITIONAL_AND; return parent.is(operator) && ((BinaryExpressionTree) parent).leftOperand() == child; } }
@Override public void visitBinaryExpression(BinaryExpressionTree tree) { boolean continueVisit = true; if (tree.is(Tree.Kind.DIVIDE) && isIntOrLong(tree.symbolType())) { continueVisit = checkIntegerDivisionInsideFloatingPointExpression(tree); } if (continueVisit) { super.visitBinaryExpression(tree); } }
@Override public void visitBinaryExpression(BinaryExpressionTree tree) { switch (tree.kind()) { case MULTIPLY: case PLUS: case MINUS: case DIVIDE: case REMAINDER: ProgramState.Pop unstackValue = programState.unstackValue(2); checkExpression(tree, unstackValue.values.get(1), unstackValue.values.get(0), unstackValue.valuesAndSymbols.get(0).symbol()); break; default: // do nothing } }
public ConditionType(ExpressionTree condition, UpdatesCollector collector) { if (condition.is(Tree.Kind.LESS_THAN, Tree.Kind.LESS_THAN_OR_EQUAL_TO)) { matched = canBeMatched(((BinaryExpressionTree) condition).leftOperand(), ((BinaryExpressionTree) condition).rightOperand(), collector); } else if (condition.is(Tree.Kind.GREATER_THAN, Tree.Kind.GREATER_THAN_OR_EQUAL_TO)) { matched = canBeMatched(((BinaryExpressionTree) condition).rightOperand(), ((BinaryExpressionTree) condition).leftOperand(), collector); } else { matched = true; } }