Search in sources :

Example 6 with ForStatementTree

use of org.sonar.plugins.java.api.tree.ForStatementTree in project sonar-java by SonarSource.

the class ForLoopCounterChangedCheck method visitForStatement.

@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);
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 7 with ForStatementTree

use of org.sonar.plugins.java.api.tree.ForStatementTree in project sonar-java by SonarSource.

the class ForLoopIncrementAndUpdateCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ForStatementTree forStatementTree = (ForStatementTree) tree;
    if (forStatementTree.update().isEmpty() || forStatementTree.condition() == null) {
        return;
    }
    Collection<Symbol> symbolsFromConditionsNotUpdated = symbolsFromConditionsNotUpdated(forStatementTree);
    if (!symbolsFromConditionsNotUpdated.isEmpty()) {
        Map<Symbol, Tree> updatesInBody = singleUpdatesInBody(forStatementTree.statement(), symbolsFromConditionsNotUpdated);
        if (!updatesInBody.isEmpty()) {
            reportIssue(forStatementTree.forKeyword(), getMessage(updatesInBody.keySet()), getSecondaries(updatesInBody.values()), null);
        }
    }
}
Also used : ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ListTree(org.sonar.plugins.java.api.tree.ListTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 8 with ForStatementTree

use of org.sonar.plugins.java.api.tree.ForStatementTree in project sonar-java by SonarSource.

the class UnusedLocalVariableCheck method leaveNode.

@Override
public void leaveNode(Tree tree) {
    if (hasSemantic()) {
        if (tree.is(Tree.Kind.BLOCK, Tree.Kind.STATIC_INITIALIZER)) {
            BlockTree blockTree = (BlockTree) tree;
            addVariables(blockTree.body());
        } else if (tree.is(Tree.Kind.FOR_STATEMENT)) {
            ForStatementTree forStatementTree = (ForStatementTree) tree;
            addVariables(forStatementTree.initializer());
        } else if (tree.is(Tree.Kind.FOR_EACH_STATEMENT)) {
            ForEachStatement forEachStatement = (ForEachStatement) tree;
            addVariable(forEachStatement.variable());
        } else if (tree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
            leaveExpressionStatement((ExpressionStatementTree) tree);
        } else {
            checkVariableUsages();
            variables.clear();
            assignments.clear();
        }
    }
}
Also used : ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) ForEachStatement(org.sonar.plugins.java.api.tree.ForEachStatement)

Aggregations

ForStatementTree (org.sonar.plugins.java.api.tree.ForStatementTree)8 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)3 WhileStatementTree (org.sonar.plugins.java.api.tree.WhileStatementTree)3 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)2 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)2 DoWhileStatementTree (org.sonar.plugins.java.api.tree.DoWhileStatementTree)2 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)2 ForEachStatement (org.sonar.plugins.java.api.tree.ForEachStatement)2 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)2 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)2 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)2 Tree (org.sonar.plugins.java.api.tree.Tree)2 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)2 Test (org.junit.Test)1 CFG (org.sonar.java.cfg.CFG)1 JavaTree (org.sonar.java.model.JavaTree)1 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)1 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)1