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