Search in sources :

Example 46 with StatementTree

use of org.sonar.plugins.java.api.tree.StatementTree 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 47 with StatementTree

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

the class IfElseIfStatementEndsWithElseCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    IfStatementTree treeIfStmt = (IfStatementTree) tree;
    StatementTree elseStmt = treeIfStmt.elseStatement();
    if (elseStmt != null && elseStmt.is(Tree.Kind.IF_STATEMENT)) {
        IfStatementTree ifStmt = (IfStatementTree) elseStmt;
        if (ifStmt.elseStatement() == null) {
            reportIssue(treeIfStmt.elseKeyword(), ifStmt.ifKeyword(), "\"if ... else if\" constructs should end with \"else\" clauses.");
        }
    }
}
Also used : IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree)

Example 48 with StatementTree

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

the class IdenticalCasesInSwitchCheck method checkIfStatement.

protected static IfElseChain checkIfStatement(IfStatementTree node) {
    IfElseChain ifElseChain = new IfElseChain();
    ifElseChain.totalBranchCount = 1;
    List<StatementTree> allBranches = new ArrayList<>();
    allBranches.add(node.thenStatement());
    StatementTree elseStatement = node.elseStatement();
    while (elseStatement != null && elseStatement.is(Tree.Kind.IF_STATEMENT)) {
        IfStatementTree ifStatement = (IfStatementTree) elseStatement;
        allBranches.add(ifStatement.thenStatement());
        elseStatement = ifStatement.elseStatement();
    }
    if (elseStatement != null) {
        allBranches.add(elseStatement);
    }
    return collectIdenticalBranches(allBranches);
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) SwitchStatementTree(org.sonar.plugins.java.api.tree.SwitchStatementTree) ArrayList(java.util.ArrayList) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree)

Example 49 with StatementTree

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

the class DuplicateConditionIfElseIfCheck method visitIfStatement.

@Override
public void visitIfStatement(IfStatementTree tree) {
    ExpressionTree condition = tree.condition();
    StatementTree statement = tree.elseStatement();
    while (statement != null && statement.is(Tree.Kind.IF_STATEMENT)) {
        IfStatementTree ifStatement = (IfStatementTree) statement;
        if (SyntacticEquivalence.areEquivalent(condition, ifStatement.condition())) {
            context.reportIssue(this, ifStatement.condition(), "This branch can not be reached because the condition duplicates a previous condition in the same sequence of \"if/else if\" statements", ImmutableList.of(new JavaFileScannerContext.Location("Original", condition)), null);
        }
        statement = ifStatement.elseStatement();
    }
    super.visitIfStatement(tree);
}
Also used : IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree)

Example 50 with StatementTree

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

the class VariableDeclarationScopeCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    BlockTree block = (BlockTree) tree;
    List<StatementTree> body = block.body();
    int bodySize = body.size();
    for (int i = 0; i < bodySize; i++) {
        StatementTree statement = body.get(i);
        if (statement.is(Kind.VARIABLE)) {
            VariableTree variableTree = (VariableTree) statement;
            if (!variableTree.symbol().usages().isEmpty()) {
                check(variableTree, body, bodySize, i + 1);
            }
        }
    }
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

Aggregations

StatementTree (org.sonar.plugins.java.api.tree.StatementTree)53 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)27 Test (org.junit.Test)26 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)26 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)24 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)21 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)17 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)15 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)13 ForStatementTree (org.sonar.plugins.java.api.tree.ForStatementTree)9 WhileStatementTree (org.sonar.plugins.java.api.tree.WhileStatementTree)8 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)7 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)7 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)6 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)6 SwitchStatementTree (org.sonar.plugins.java.api.tree.SwitchStatementTree)6 ThrowStatementTree (org.sonar.plugins.java.api.tree.ThrowStatementTree)6 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)5 Tree (org.sonar.plugins.java.api.tree.Tree)5 TryStatementTree (org.sonar.plugins.java.api.tree.TryStatementTree)5