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.");
}
}
}
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);
}
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);
}
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();
}
Aggregations