use of org.sonar.plugins.java.api.tree.IfStatementTree in project sonar-java by SonarSource.
the class IdenticalCasesInSwitchCheck method visitNode.
@Override
public void visitNode(Tree node) {
if (node.is(Tree.Kind.SWITCH_STATEMENT)) {
SwitchStatementTree switchStatement = (SwitchStatementTree) node;
Multimap<CaseGroupTree, CaseGroupTree> identicalBranches = checkSwitchStatement(switchStatement);
boolean allBranchesSame = allBranchesSame(identicalBranches, switchStatement.cases().size());
boolean allBranchesSameWithoutDefault = allBranchesSame && !hasDefaultClause(switchStatement);
if (!allBranchesSame || allBranchesSameWithoutDefault) {
identicalBranches.asMap().forEach((first, others) -> {
if (!isTrivialCase(first.body()) || allBranchesSameWithoutDefault) {
others.forEach(other -> createIssue(other, issueMessage("case", first), first));
}
});
}
} else if (node.is(Tree.Kind.IF_STATEMENT) && !node.parent().is(Tree.Kind.IF_STATEMENT)) {
IfStatementTree ifStatement = (IfStatementTree) node;
IfElseChain ifElseChain = checkIfStatement(ifStatement);
reportIdenticalIfChainBranches(ifElseChain.branches, ifElseChain.totalBranchCount, hasElseClause(ifStatement));
}
}
use of org.sonar.plugins.java.api.tree.IfStatementTree in project sonar-java by SonarSource.
the class CFG method buildIfStatement.
private void buildIfStatement(IfStatementTree ifStatementTree) {
Block next = currentBlock;
// process else-branch
Block elseBlock = next;
StatementTree elseStatement = ifStatementTree.elseStatement();
if (elseStatement != null) {
// if statement will create the required block.
if (!elseStatement.is(Tree.Kind.IF_STATEMENT)) {
currentBlock = createBlock(next);
}
build(elseStatement);
elseBlock = currentBlock;
}
// process then-branch
currentBlock = createBlock(next);
build(ifStatementTree.thenStatement());
Block thenBlock = currentBlock;
// process condition
currentBlock = createBranch(ifStatementTree, thenBlock, elseBlock);
buildCondition(ifStatementTree.condition(), thenBlock, elseBlock);
}
use of org.sonar.plugins.java.api.tree.IfStatementTree in project sonar-java by SonarSource.
the class MapComputeIfAbsentOrPresentCheck method isInsideIfStatementWithNullCheckWithoutElse.
private static boolean isInsideIfStatementWithNullCheckWithoutElse(MethodInvocationTree mit) {
Tree parent = mit.parent();
while (parent != null && !parent.is(Tree.Kind.IF_STATEMENT)) {
parent = parent.parent();
}
if (parent == null) {
return false;
}
IfStatementTree ifStatementTree = (IfStatementTree) parent;
return ifStatementTree.elseStatement() == null && isNullCheck(ExpressionUtils.skipParentheses(ifStatementTree.condition()));
}
use of org.sonar.plugins.java.api.tree.IfStatementTree in project sonar-java by SonarSource.
the class NestedIfStatementsCheck method visit.
private void visit(IfStatementTree tree) {
scan(tree.condition());
scan(tree.thenStatement());
StatementTree elseStatementTree = tree.elseStatement();
if (elseStatementTree != null && elseStatementTree.is(Tree.Kind.IF_STATEMENT)) {
visit((IfStatementTree) elseStatementTree);
} else {
scan(elseStatementTree);
}
}
use of org.sonar.plugins.java.api.tree.IfStatementTree in project sonar-java by SonarSource.
the class ReturnOfBooleanExpressionsCheck method getStatementTree.
private static StatementTree getStatementTree(IfStatementTree ifStatementTree) {
StatementTree elseStatementOrNextStatement = ifStatementTree.elseStatement();
if (elseStatementOrNextStatement == null) {
JavaTree parent = (JavaTree) ifStatementTree.parent();
List<Tree> children = parent.getChildren();
int indexOfIf = children.indexOf(ifStatementTree);
if (indexOfIf < children.size() - 1) {
// Defensive, this condition should always be true as if necessarily followed by a statement or a token.
Tree next = children.get(indexOfIf + 1);
if (!next.is(Kind.TOKEN)) {
elseStatementOrNextStatement = (StatementTree) next;
}
}
}
return elseStatementOrNextStatement;
}
Aggregations