@Override public void visitForStatement(ForStatementTree tree) { if (tree.condition() == null) { addIssue(tree.forKeyword(), "Add an end condition for this loop.").secondary(new IssueLocation(tree.firstSemicolonToken(), tree.secondSemicolonToken(), null)); } else { createIssue(tree.forKeyword(), tree.condition()); } }
private static boolean isNontrivialConditionException(ForStatementTree forStatement) { ExpressionTree condition = forStatement.condition(); ExpressionTree update = forStatement.update(); if (update != null && condition != null && condition.is(KindSet.EQUALITY_KINDS)) { Set<String> counters = new HashSet<>(); counters(update, counters); ExpressionTree leftOperand = ((BinaryExpressionTree) condition).leftOperand(); return !leftOperand.is(IDENTIFIER_REFERENCE) || !counters.contains(((IdentifierTree) leftOperand).name()); } return false; }
@Override public void visitForStatement(ForStatementTree forStatement) { List<ExpressionTree> updatedExpressions = updatedExpressions(forStatement.update()); ExpressionTree condition = forStatement.condition(); if (!updatedExpressions.isEmpty() && condition != null) { ConditionVisitor conditionVisitor = new ConditionVisitor(updatedExpressions); condition.accept(conditionVisitor); if (!conditionVisitor.foundUpdatedExpression) { String updated = expressionList(updatedExpressions); String tested = expressionList(conditionVisitor.testedExpressions); String message = String.format(MESSAGE, tested, updated); addIssue(forStatement.forKeyword(), message); } } super.visitForStatement(forStatement); }
private static Stream<JavaScriptTree> addUpdateExpression(IterationStatementTree iterationStatement, Stream<JavaScriptTree> iterationTrees) { if (iterationStatement instanceof ForStatementTree) { return Stream.concat(iterationTrees, Stream.of((JavaScriptTree) ((ForStatementTree) iterationStatement).update())); } return iterationTrees; }
@Override public void visitForStatement(ForStatementTree tree) { checkIndentation(tree.forKeyword(), tree.statement()); super.visitForStatement(tree); }
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); }
/** * In * <pre> * for (i = 0; i < arr.length; arr[i++] = 0); * </pre> * the semicolon is necessary (so no issue is raised) as there are no curly brackets. */ @Override public void visitForStatement(ForStatementTree tree) { except(tree.statement()); super.visitForStatement(tree); }
@Override public void visitForStatement(ForStatementTree tree) { Tree init = tree.init(); if (init != null && init.is(Tree.Kind.LET_DECLARATION)) { List<IdentifierTree> identifiers = ((VariableDeclarationTreeImpl) init).variableIdentifiers(); ignore(identifiers); } super.visitForStatement(tree); }
@Override public void visitForStatement(ForStatementTree tree) { enterScope(); super.visitForStatement(tree); leaveScopeAndCheckNumberOfJump(tree.forKeyword()); }
private void checkLoop(Tree parent, SyntaxToken openCurly) { if (parent.is(Kind.DO_WHILE_STATEMENT)) { issueIfLineMismatch(openCurly, ((DoWhileStatementTree) parent).doKeyword()); } if (parent.is(Kind.WHILE_STATEMENT)) { issueIfLineMismatch(openCurly, ((WhileStatementTree) parent).closeParenthesisToken()); } if (parent.is(Kind.FOR_STATEMENT)) { issueIfLineMismatch(openCurly, ((ForStatementTree) parent).closeParenthesisToken()); } if (parent.is(Kind.FOR_IN_STATEMENT, Kind.FOR_OF_STATEMENT)) { issueIfLineMismatch(openCurly, ((ForObjectStatementTree) parent).closeParenthesisToken()); } }
private static boolean isTrivialIteratorException(ForStatementTree forStatement) { // todo(Lena): SONARJS-383 consider usage of counter inside the loop. Do it with symbol table. ExpressionTree condition = forStatement.condition(); if (condition != null && condition.is(NOT_EQUAL_TO)) { ExpressionTree update = forStatement.update(); Tree init = forStatement.init(); if (init != null && update != null) { return checkForTrivialIteratorException(init, condition, update); } } return false; }
@Override public void visitForStatement(ForStatementTree tree) { ExpressionTree condition = tree.condition(); ExpressionTree update = tree.update(); boolean conditionCondition = condition != null && isEquality(condition); boolean updateCondition = update != null && isUpdateIncDec(update); if (conditionCondition && updateCondition && !isException(tree)) { addIssue((BinaryExpressionTree) condition); } super.visitForStatement(tree); }
private static boolean hasPredecessorInsideLoopBody(CfgBranchingBlock conditionBlock, IterationStatementTree loopTree) { for (CfgBlock loopPredecessor : conditionBlock.predecessors()) { List<Tree> predecessorElements = loopPredecessor.elements(); Tree predecessorLastElement = predecessorElements.get(predecessorElements.size() - 1); if (loopTree.is(Kind.FOR_STATEMENT)) { ForStatementTree forTree = (ForStatementTree) loopTree; if (forTree.update() != null && forTree.update().equals(predecessorLastElement)) { return !loopPredecessor.predecessors().isEmpty(); } } StatementTree loopBody = loopTree.statement(); if (isDescendant(predecessorLastElement, loopBody)) { return true; } } return false; }
@Override public void visitForStatement(ForStatementTree forStatement) { ExpressionTree condition = forStatement.condition(); ForLoopIncrement loopIncrement = ForLoopIncrement.findInLoopUpdate(forStatement); if (condition == null || loopIncrement == null || !loopIncrement.hasValue()) { return; } checkIncrementSign(condition, loopIncrement); super.visitForStatement(forStatement); }
@Override public void visitForStatement(ForStatementTree tree) { increaseAndCheckNestedLevel(tree.forKeyword()); super.visitForStatement(tree); decreaseNestedLevel(); }