Search in sources :

Example 41 with ExpressionTree

use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.

the class ReassignmentFinderTest method assignement_with_parenthesis.

@Test
public void assignement_with_parenthesis() throws Exception {
    String code = newCode("int foo() {", "  int a;", "  a = 0;", "  int b = ((a));", "  return a;", "}");
    List<StatementTree> statements = methodBody(code);
    ExpressionTree aAssignmentExpression = assignementExpressionFromStatement(statements.get(1));
    assertThatLastReassignmentsOfReturnedVariableIsEqualTo(statements, aAssignmentExpression);
}
Also used : IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) Test(org.junit.Test)

Example 42 with ExpressionTree

use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.

the class ReassignmentFinderTest method last_assignement_on_same_line.

@Test
public void last_assignement_on_same_line() throws Exception {
    String code = newCode("int foo() {", "  int a;", "  a = 0;", "  a = 1; return a;", "}");
    List<StatementTree> statements = methodBody(code);
    ExpressionTree secondAssignment = assignementExpressionFromStatement(statements.get(2));
    assertThatLastReassignmentsOfReturnedVariableIsEqualTo(statements, secondAssignment);
}
Also used : IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) Test(org.junit.Test)

Example 43 with ExpressionTree

use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.

the class ReassignmentFinderTest method assignement_with_other_variable.

@Test
public void assignement_with_other_variable() throws Exception {
    String code = newCode("int foo() {", "  int a;", "  int b;", "  a = 0;", "  b = 0;", "  return a;", "}");
    List<StatementTree> statements = methodBody(code);
    ExpressionTree aAssignmentExpression = assignementExpressionFromStatement(statements.get(2));
    assertThatLastReassignmentsOfReturnedVariableIsEqualTo(statements, aAssignmentExpression);
}
Also used : IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) Test(org.junit.Test)

Example 44 with ExpressionTree

use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.

the class ReassignmentFinderTest method last_assignement.

@Test
public void last_assignement() throws Exception {
    String code = newCode("int foo() {", "  int a;", "  a = 0;", "  a = 1;", "  return a;", "}");
    List<StatementTree> statements = methodBody(code);
    ExpressionTree secondAssignment = assignementExpressionFromStatement(statements.get(2));
    assertThatLastReassignmentsOfReturnedVariableIsEqualTo(statements, secondAssignment);
}
Also used : IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) Test(org.junit.Test)

Example 45 with ExpressionTree

use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.

the class ArrayForVarArgCheck method checkInvokedMethod.

private void checkInvokedMethod(JavaSymbol.MethodJavaSymbol methodSymbol, @Nullable MethodJavaType methodType, ExpressionTree lastArg) {
    if (methodSymbol.isVarArgs() && lastArg.is(Tree.Kind.NEW_ARRAY)) {
        if (lastParamHasSameType(methodSymbol, methodType, lastArg.symbolType())) {
            String message = "Remove this array creation";
            NewArrayTree newArrayTree = (NewArrayTree) lastArg;
            if (newArrayTree.openBraceToken() == null) {
                ExpressionTree expression = newArrayTree.dimensions().get(0).expression();
                Integer literalValue = LiteralUtils.intLiteralValue(expression);
                if (literalValue == null || literalValue != 0 || isCallingOverload(methodSymbol, lastArg)) {
                    return;
                }
            } else if (!newArrayTree.initializers().isEmpty()) {
                message += " and simply pass the elements";
            }
            reportIssue(lastArg, message + ".");
        } else {
            String type = ((Type.ArrayType) getLastParameterType(methodSymbol.parameterTypes())).elementType().name();
            reportIssue(lastArg, "Disambiguate this call by either casting as \"" + type + "\" or \"" + type + "[]\".");
        }
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaType(org.sonar.java.resolve.MethodJavaType) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)

Aggregations

ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)189 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)88 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)80 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)66 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)50 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)44 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)35 Symbol (org.sonar.plugins.java.api.semantic.Symbol)31 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)30 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)30 Test (org.junit.Test)25 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)24 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)24 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)23 Type (org.sonar.plugins.java.api.semantic.Type)21 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)20 Tree (org.sonar.plugins.java.api.tree.Tree)20 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)17 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)14 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)13