private static ExpressionTree getNonNullLiteralOperand(BinaryExpressionTree binaryExpressionTree) { if (isNullOrUndefined(binaryExpressionTree.leftOperand())) { return binaryExpressionTree.rightOperand(); } return binaryExpressionTree.leftOperand(); }
private void checkBinaryExpression(BinaryExpressionTree tree, Builder<ExpressionTree> operandListBuilder) { operandListBuilder.add(tree.rightOperand()); scan(tree.rightOperand()); if (tree.is(Kind.PLUS)) { if (tree.leftOperand().is(Kind.PLUS)) { checkBinaryExpression((BinaryExpressionTree) tree.leftOperand(), operandListBuilder); } else { operandListBuilder.add(tree.leftOperand()); scan(tree.leftOperand()); } } else { scan(tree.leftOperand()); } } }
@Override public void visitBinaryExpression(BinaryExpressionTree tree) { if (!isNullLiteral(tree.leftOperand()) && !isNullLiteral(tree.rightOperand())) { if (tree.is(Tree.Kind.EQUAL_TO)) { getContext().addIssue(this, tree.operator(), "Replace \"==\" with \"===\"."); } else if (tree.is(Tree.Kind.NOT_EQUAL_TO)) { getContext().addIssue(this, tree.operator(), "Replace \"!=\" with \"!==\"."); } } super.visitBinaryExpression(tree); }
private SyntaxToken getFirstComma(BinaryExpressionTree tree) { SyntaxToken result = tree.operator(); ExpressionTree currentExpression = tree.leftOperand(); while (currentExpression.is(Kind.COMMA_OPERATOR)) { result = ((BinaryExpressionTree)currentExpression).operator(); currentExpression = ((BinaryExpressionTree) currentExpression).leftOperand(); } return result; }
private static boolean isPotentialNanComparison(BinaryExpressionTree tree) { return tree.is(Kind.STRICT_NOT_EQUAL_TO, Kind.STRICT_EQUAL_TO) && (tree.leftOperand().is( Kind.IDENTIFIER_REFERENCE, Kind.IDENTIFIER, Kind.BRACKET_MEMBER_EXPRESSION, Kind.DOT_MEMBER_EXPRESSION) || tree.leftOperand() instanceof UnaryExpressionTree); }
@Override void raiseIssue(BinaryExpressionTree tree) { addIssue(tree.operatorToken(), MESSAGE) .secondary(tree.leftOperand()) .secondary(tree.rightOperand()); }
private void raiseIssue(BinaryExpressionTree tree) { String message = tree.is(Kind.STRICT_EQUAL_TO) ? MESSAGE_EQUAL : MESSAGE_NOT_EQUAL; addIssue(tree.operatorToken(), message) .secondary(tree.leftOperand()) .secondary(tree.rightOperand()); } }
@Override public void visitNode(Tree tree) { BinaryExpressionTree equalityExpr = (BinaryExpressionTree) tree; if (isNaN(equalityExpr.leftOperand()) || isNaN(equalityExpr.rightOperand())) { addIssue(tree, "Use a test of the format \"a " + equalityExpr.operator().text() + " a\" instead."); } }
private static boolean isNullConditionException(ForStatementTree forStatement) { ExpressionTree condition = forStatement.condition(); return condition != null && condition.is(Tree.Kind.NOT_EQUAL_TO) && ((BinaryExpressionTree) condition).rightOperand().is(Tree.Kind.NULL_LITERAL); }
@Override public void visitNode(Tree tree) { if (lonelyBitwiseAndOr == null && tree.is(Kind.BITWISE_AND, Kind.BITWISE_OR) && !((BinaryExpressionTree) tree).rightOperand().is(Kind.NUMERIC_LITERAL)) { lonelyBitwiseAndOr = ((BinaryExpressionTree) tree).operatorToken(); } else { fileContainsBitwiseOperations = true; } }
private static List<SyntaxToken> getCommas(BinaryExpressionTree tree) { List<SyntaxToken> commas = new ArrayList<>(); commas.add(tree.operatorToken()); ExpressionTree currentExpression = tree.leftOperand(); while (currentExpression.is(Kind.COMMA_OPERATOR)) { commas.add(((BinaryExpressionTree) currentExpression).operatorToken()); currentExpression = ((BinaryExpressionTree) currentExpression).leftOperand(); } return Lists.reverse(commas); }
/** * Gets the number of "," characters in the specified expression. * <p> * Example 1: expression 10 (as in "case 10:") returns 0. * <p> * Example 2: expression 10,11,12,13 (as in "case 10,11,12,13:") returns 3. */ private static int getNumberOfCommas(ExpressionTree expression) { int nbCommas = 0; if (expression.is(Kind.COMMA_OPERATOR)) { BinaryExpressionTree binaryExpression = (BinaryExpressionTree) expression; nbCommas = getNumberOfCommas(binaryExpression.leftOperand()) + 1; } return nbCommas; }
@Override public void visitBinaryExpression(BinaryExpressionTree tree) { if (!tree.is(Kind.COMMA_OPERATOR)) { super.visitBinaryExpression(tree); return; } raiseIssue(tree); getAllSubExpressions(tree).forEach(expression -> super.scan(expression)); }
private void addIssue(BinaryExpressionTree condition) { String message = String.format(MESSAGE, condition.operatorToken().text()); addIssue(condition.operatorToken(), message); }
private void addIssue(ExpressionTree condition) { String message = String.format("Replace '%s' operator with one of '<=', '>=', '<', or '>' comparison operators.", ((BinaryExpressionTree) condition).operator().text()); getContext().addIssue(this, condition, message); }
public ExpressionComponents(Tree element) { if (element instanceof AssignmentExpressionTree) { AssignmentExpressionTree assignment = (AssignmentExpressionTree) element; leftOperand = assignment.variable(); rightOperand = assignment.expression(); operator = assignment.operatorToken(); } else { BinaryExpressionTree binaryExpression = (BinaryExpressionTree) element; leftOperand = binaryExpression.leftOperand(); rightOperand = binaryExpression.rightOperand(); operator = binaryExpression.operatorToken(); } }
@Override public void endOfFile(ScriptTree scriptTree) { EqualityVisitor equalityVisitor = new EqualityVisitor(); equalityVisitor.scanTree(scriptTree); equalityVisitor.equalityExpressions .stream() .filter(equalityExpression -> !ignoredList.contains(equalityExpression)) .forEach(equalityExpression -> addIssue(equalityExpression.operatorToken(), equalityExpression.is(Kind.EQUAL_TO) ? "Replace \"==\" with \"===\"." : "Replace \"!=\" with \"!==\".") .secondary(equalityExpression.leftOperand()) .secondary(equalityExpression.rightOperand())); }
private boolean isOrWithNonEqualToNull(BinaryExpressionTree tree) { return tree.is(Tree.Kind.CONDITIONAL_OR) && isNullComparison(tree.leftOperand(), Tree.Kind.NOT_EQUAL_TO, Tree.Kind.STRICT_NOT_EQUAL_TO); }
@Override public void visitCaseClause(CaseClauseTree tree) { ExpressionTree expression = tree.expression(); if (expression.is(Kind.COMMA_OPERATOR)) { int nbCommas = getNumberOfCommas(expression); raiseIssue(expression, nbCommas + 1, ((BinaryExpressionTree) expression).rightOperand()); } if (expression.is(Kind.CONDITIONAL_OR)) { List<ExpressionTree> expressionTrees = orExpressionOperands(expression); if (!expressionTrees.isEmpty()) { raiseIssue(expression, expressionTrees.size(), expressionTrees.get(0)); } } super.visitCaseClause(tree); }
@Override public void beforeBlockElement(ProgramState currentState, Tree element, ProgramPoint programPoint) { if (element.is(Kind.RELATIONAL_IN)) { SymbolicValue rightOperandValue = currentState.peekStack(); Constraint rightOperandConstraint = currentState.getConstraint(rightOperandValue); if (rightOperandConstraint.isIncompatibleWith(Constraint.OBJECT)) { BinaryExpressionTree inOperation = (BinaryExpressionTree) element; addUniqueIssue(inOperation.rightOperand(), MESSAGE, new IssueLocation(inOperation.operatorToken())); } } }