use of org.sonar.plugins.java.api.tree.ForStatementTree 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.ForStatementTree in project sonar-java by SonarSource.
the class ForLoopIncrementAndUpdateCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
ForStatementTree forStatementTree = (ForStatementTree) tree;
if (forStatementTree.update().isEmpty() || forStatementTree.condition() == null) {
return;
}
Collection<Symbol> symbolsFromConditionsNotUpdated = symbolsFromConditionsNotUpdated(forStatementTree);
if (!symbolsFromConditionsNotUpdated.isEmpty()) {
Map<Symbol, Tree> updatesInBody = singleUpdatesInBody(forStatementTree.statement(), symbolsFromConditionsNotUpdated);
if (!updatesInBody.isEmpty()) {
reportIssue(forStatementTree.forKeyword(), getMessage(updatesInBody.keySet()), getSecondaries(updatesInBody.values()), null);
}
}
}
use of org.sonar.plugins.java.api.tree.ForStatementTree in project sonar-java by SonarSource.
the class UnusedLocalVariableCheck method leaveNode.
@Override
public void leaveNode(Tree tree) {
if (hasSemantic()) {
if (tree.is(Tree.Kind.BLOCK, Tree.Kind.STATIC_INITIALIZER)) {
BlockTree blockTree = (BlockTree) tree;
addVariables(blockTree.body());
} else if (tree.is(Tree.Kind.FOR_STATEMENT)) {
ForStatementTree forStatementTree = (ForStatementTree) tree;
addVariables(forStatementTree.initializer());
} else if (tree.is(Tree.Kind.FOR_EACH_STATEMENT)) {
ForEachStatement forEachStatement = (ForEachStatement) tree;
addVariable(forEachStatement.variable());
} else if (tree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
leaveExpressionStatement((ExpressionStatementTree) tree);
} else {
checkVariableUsages();
variables.clear();
assignments.clear();
}
}
}
Aggregations