use of org.sonar.plugins.java.api.tree.CaseGroupTree 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.CaseGroupTree in project sonar-java by SonarSource.
the class CFG method buildSwitchStatement.
private void buildSwitchStatement(SwitchStatementTree switchStatementTree) {
Block switchSuccessor = currentBlock;
// process condition
currentBlock = createBlock();
currentBlock.terminator = switchStatementTree;
switches.addLast(currentBlock);
build(switchStatementTree.expression());
Block conditionBlock = currentBlock;
// process body
currentBlock = createBlock(switchSuccessor);
breakTargets.addLast(switchSuccessor);
boolean hasDefaultCase = false;
if (!switchStatementTree.cases().isEmpty()) {
CaseGroupTree firstCase = switchStatementTree.cases().get(0);
for (CaseGroupTree caseGroupTree : Lists.reverse(switchStatementTree.cases())) {
build(caseGroupTree.body());
caseGroupTree.labels().forEach(l -> {
if (l.expression() != null) {
build(l.expression());
}
});
if (!hasDefaultCase) {
hasDefaultCase = containsDefaultCase(caseGroupTree.labels());
}
switches.getLast().addSuccessor(currentBlock);
if (!caseGroupTree.equals(firstCase)) {
// No block predecessing the first case group.
currentBlock = createBlock(currentBlock);
}
}
}
breakTargets.removeLast();
// process condition
currentBlock = switches.removeLast();
if (!hasDefaultCase) {
currentBlock.addSuccessor(switchSuccessor);
}
currentBlock = conditionBlock;
}
use of org.sonar.plugins.java.api.tree.CaseGroupTree in project sonar-java by SonarSource.
the class SwitchCaseWithoutBreakCheck method intentionalFallThrough.
private static boolean intentionalFallThrough(SwitchStatementTree switchStatement, CaseGroupTree caseGroup) {
FallThroughCommentVisitor visitor = new FallThroughCommentVisitor();
// Check first token of next case group when comment is last element of case group it is attached to next group.
CaseGroupTree nextCaseGroup = switchStatement.cases().get(switchStatement.cases().indexOf(caseGroup) + 1);
List<Tree> treesToScan = ImmutableList.<Tree>builder().addAll(caseGroup.body()).add(nextCaseGroup.firstToken()).build();
visitor.scan(treesToScan);
return visitor.hasComment;
}
use of org.sonar.plugins.java.api.tree.CaseGroupTree in project sonar-java by SonarSource.
the class SwitchAtLeastThreeCasesCheck method visitSwitchStatement.
@Override
public void visitSwitchStatement(SwitchStatementTree tree) {
int count = 0;
for (CaseGroupTree caseGroup : tree.cases()) {
count += caseGroup.labels().size();
}
if (count < 3) {
context.reportIssue(this, tree.switchKeyword(), "Replace this \"switch\" statement by \"if\" statements to increase readability.");
}
super.visitSwitchStatement(tree);
}
use of org.sonar.plugins.java.api.tree.CaseGroupTree in project sonar-java by SonarSource.
the class SwitchWithLabelsCheck method visitNode.
@Override
public void visitNode(Tree tree) {
CaseGroupTree cgt = (CaseGroupTree) tree;
for (StatementTree statementTree : cgt.body()) {
if (statementTree.is(LABELED_STATEMENT)) {
IdentifierTree label = ((LabeledStatementTree) statementTree).label();
reportIssue(label, "Remove this misleading \"" + label.name() + "\" label.");
}
}
}
Aggregations