@Override public void visitForStatement(ForStatementTree tree) { if (tree.condition() == null) { checkLoopWithAlwaysTrueCondition(context, tree); } else if (isConditionUnreachable(tree)) { context.reportIssue(tree, NoWayOutLoopCheck.this, "Correct this loop's end condition."); } }
private static Collection<Symbol> symbolsFromConditionsNotUpdated(ForStatementTree forStatementTree) { Collection<Symbol> symbols = getConditionSymbols(forStatementTree.condition()); symbols.removeAll(getUpdatedSymbols(forStatementTree.update())); return symbols; }
@Override public void visitForStatement(ForStatementTree tree) { if (tree.condition() == null) { checkLoopWithAlwaysTrueCondition(context, tree); } else if (isConditionUnreachable(tree)) { context.reportIssue(tree, NoWayOutLoopCheck.this, "Correct this loop's end condition."); } }
@Override public void visitNode(Tree tree) { if (tree.is(Tree.Kind.METHOD_INVOCATION)) { super.visitNode(tree); } else if(tree.is(Tree.Kind.FOR_STATEMENT)) { ForStatementTree fst = (ForStatementTree) tree; inWhileLoop.push(fst.initializer().isEmpty() && fst.condition()==null && fst.update().isEmpty()); } else { inWhileLoop.push(true); } }
@Override public void visitForStatement(ForStatementTree forStatement) { ExpressionTree condition = forStatement.condition(); if (condition != null && (isAlwaysFalseCondition(condition) || isConditionFalseAtInitialization(forStatement))) { reportIssue(condition, "This loop will never execute."); } }
@Override public void visitForStatement(ForStatementTree forStatement) { ExpressionTree condition = forStatement.condition(); ForLoopIncrement loopIncrement = ForLoopIncrement.findInUpdates(forStatement); if (condition == null || loopIncrement == null || !loopIncrement.hasValue()) { return; } checkIncrementSign(condition, loopIncrement); }
@Override public void visitForStatement(ForStatementTree tree) { checkModelProviderInLoop(tree, tree.condition(), tree.statement()); super.visitForStatement(tree); }
@Override public void visitNode(Tree tree) { if (tree.is(Tree.Kind.METHOD_INVOCATION)) { super.visitNode(tree); } else if (tree.is(Tree.Kind.FOR_STATEMENT)) { ForStatementTree fst = (ForStatementTree) tree; inWhileLoop.push(fst.initializer().isEmpty() && fst.condition() == null && fst.update().isEmpty()); } else { inWhileLoop.push(true); } }
private boolean isConditionUnreachable(ForStatementTree tree) { UpdatesCollector collector = new UpdatesCollector(); tree.accept(collector); ConditionType condition = new ConditionType(tree.condition(), collector); return !condition.isMatched(); } }
private boolean isConditionUnreachable(ForStatementTree tree) { UpdatesCollector collector = new UpdatesCollector(); tree.accept(collector); ConditionType condition = new ConditionType(tree.condition(), collector); return !condition.isMatched(); } }
@Override public void visitForStatement(ForStatementTree tree) { Set<String> pendingLoopCounters = Sets.newHashSet(); for (StatementTree statementTree : tree.initializer()) { if (statementTree.is(Tree.Kind.VARIABLE)) { pendingLoopCounters.add(((VariableTree) statementTree).simpleName().name()); } } scan(tree.initializer()); scan(tree.condition()); scan(tree.update()); loopCounters.addAll(pendingLoopCounters); scan(tree.statement()); loopCounters.removeAll(pendingLoopCounters); }
@Override public void visitForStatement(ForStatementTree tree) { // Updates in initializer are not of interest scan(tree.condition()); scan(tree.update()); scan(tree.statement()); }
@Override public void visitForStatement(ForStatementTree tree) { // Updates in initializer are not of interest scan(tree.condition()); scan(tree.update()); scan(tree.statement()); }
@Override public void visitNode(Tree tree) { ForStatementTree forStatementTree = (ForStatementTree) tree; if (forStatementTree.initializer().isEmpty() && forStatementTree.update().isEmpty() && forStatementTree.condition() != null) { context.reportIssue(this, forStatementTree.forKeyword(), "Replace this \"for\" loop with a \"while\" loop."); } } }
@Override public void visitNode(Tree tree) { ForStatementTree forStatementTree = (ForStatementTree) tree; if (forStatementTree.initializer().isEmpty() && forStatementTree.update().isEmpty() && forStatementTree.condition() != null) { context.reportIssue(this, forStatementTree.forKeyword(), "Replace this \"for\" loop with a \"while\" loop."); } } }
@Override public void visitForStatement(ForStatementTree tree) { scan(tree.initializer()); scan(tree.condition()); scan(tree.update()); scan(tree.statement()); }
@Override public void visitForStatement(ForStatementTree tree) { scan(tree.initializer()); scan(tree.condition()); scan(tree.update()); scan(tree.statement()); }
@Override public void visitForStatement(ForStatementTree tree) { scan(tree.condition()); scan(tree.initializer()); scan(tree.update()); visitStatement(tree.statement()); }
@Override public void visitForStatement(ForStatementTree tree) { scan(tree.initializer()); scan(tree.condition()); scan(tree.update()); scan(tree.statement()); }
private static boolean isConditionFalseAtInitialization(ForStatementTree forStatement) { Iterable<ForLoopInitializer> initializers = ForLoopInitializer.list(forStatement); ExpressionTree condition = forStatement.condition(); if (!condition.is(Tree.Kind.GREATER_THAN, Tree.Kind.GREATER_THAN_OR_EQUAL_TO, Tree.Kind.LESS_THAN, Tree.Kind.LESS_THAN_OR_EQUAL_TO)) { return false; } BinaryExpressionTree binaryCondition = (BinaryExpressionTree) condition; Integer leftOperand = eval(binaryCondition.leftOperand(), initializers); Integer rightOperand = eval(binaryCondition.rightOperand(), initializers); if (leftOperand != null && rightOperand != null) { return !evaluateCondition(condition, leftOperand, rightOperand); } return false; }