use of org.sonar.plugins.java.api.tree.StatementTree in project sonar-java by SonarSource.
the class ImmediatelyReturnedVariableCheck method visitBlock.
@Override
public void visitBlock(BlockTree tree) {
super.visitBlock(tree);
List<StatementTree> statements = tree.body();
int size = statements.size();
if (size < 2) {
return;
}
StatementTree butLastStatement = statements.get(size - 2);
if (butLastStatement.is(Kind.VARIABLE)) {
VariableTree variableTree = (VariableTree) butLastStatement;
if (!variableTree.modifiers().annotations().isEmpty()) {
return;
}
StatementTree lastStatement = statements.get(size - 1);
String lastStatementIdentifier = getReturnOrThrowIdentifier(lastStatement);
if (lastStatementIdentifier != null) {
String identifier = variableTree.simpleName().name();
if (StringUtils.equals(lastStatementIdentifier, identifier)) {
context.reportIssue(this, variableTree.initializer(), "Immediately " + lastTypeForMessage + " this expression instead of assigning it to the temporary variable \"" + identifier + "\".");
}
}
}
}
use of org.sonar.plugins.java.api.tree.StatementTree 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.StatementTree in project sonar-java by SonarSource.
the class JavaPropertiesHelperTest method firstExpression.
private ExpressionTree firstExpression(String code) {
CompilationUnitTree compilationUnitTree = (CompilationUnitTree) p.parse("class A { " + code + "}");
SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Collections.emptyList()));
ClassTree firstType = (ClassTree) compilationUnitTree.types().get(0);
StatementTree firstStatement = ((MethodTree) firstType.members().get(0)).block().body().get(0);
return ((ExpressionStatementTree) firstStatement).expression();
}
use of org.sonar.plugins.java.api.tree.StatementTree in project sonar-java by SonarSource.
the class ReassignmentFinderTest method outside_method.
@Test
public void outside_method() throws Exception {
String code = newCode("int b;", "int foo() {", " return b;", "}");
ClassTree classTree = classTree(code);
List<StatementTree> statements = ((MethodTree) classTree.members().get(1)).block().body();
ExpressionTree variableDeclaration = ((VariableTree) (classTree.members().get(0))).initializer();
assertThatLastReassignmentsOfReturnedVariableIsEqualTo(statements, variableDeclaration);
}
use of org.sonar.plugins.java.api.tree.StatementTree in project sonar-java by SonarSource.
the class ReassignmentFinderTest method parameter.
@Test
public void parameter() throws Exception {
String code = newCode("int foo(int a) {", " return a;", "}");
MethodTree method = methodTree(code);
List<StatementTree> statements = method.block().body();
assertThatLastReassignmentsOfReturnedVariableIsEqualTo(statements, null);
}
Aggregations