Search in sources :

Example 16 with IfStatementTree

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

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

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

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

the class DoubleCheckedLockingCheck method isIfFieldEqNull.

private static Optional<IfFieldEqNull> isIfFieldEqNull(Tree tree) {
    if (!tree.is(IF_STATEMENT)) {
        return Optional.empty();
    }
    IfStatementTree ifTree = (IfStatementTree) tree;
    if (!ifTree.condition().is(EQUAL_TO)) {
        return Optional.empty();
    }
    BinaryExpressionTree eqRelation = (BinaryExpressionTree) ifTree.condition();
    if (eqRelation.rightOperand().is(NULL_LITERAL)) {
        return isField(eqRelation.leftOperand()).map(f -> new IfFieldEqNull(ifTree, f));
    }
    if (eqRelation.leftOperand().is(NULL_LITERAL)) {
        return isField(eqRelation.rightOperand()).map(f -> new IfFieldEqNull(ifTree, f));
    }
    return Optional.empty();
}
Also used : BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree)

Aggregations

IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)19 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)11 ForStatementTree (org.sonar.plugins.java.api.tree.ForStatementTree)5 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)5 SwitchStatementTree (org.sonar.plugins.java.api.tree.SwitchStatementTree)5 WhileStatementTree (org.sonar.plugins.java.api.tree.WhileStatementTree)5 DoWhileStatementTree (org.sonar.plugins.java.api.tree.DoWhileStatementTree)4 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)4 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)3 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)3 Tree (org.sonar.plugins.java.api.tree.Tree)3 TryStatementTree (org.sonar.plugins.java.api.tree.TryStatementTree)3 Test (org.junit.Test)2 JavaTree (org.sonar.java.model.JavaTree)2 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)2 CaseGroupTree (org.sonar.plugins.java.api.tree.CaseGroupTree)2 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)2 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)2 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)2 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)2