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);
}
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);
}
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);
}
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);
}
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 + "[]\".");
}
}
}
Aggregations