use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class IgnoredReturnValueCheck method visitNode.
@Override
public void visitNode(Tree tree) {
ExpressionTree expr = ((ExpressionStatementTree) tree).expression();
if (expr.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree mit = (MethodInvocationTree) expr;
if (isExcluded(mit)) {
return;
}
Symbol methodSymbol = mit.symbol();
if (!isVoidOrUnknown(mit.symbolType()) && isCheckedType(methodSymbol.owner().type()) && methodSymbol.isPublic() && !isConstructor(methodSymbol)) {
IdentifierTree methodName = ExpressionUtils.methodName(mit);
reportIssue(methodName, "The return value of \"" + methodName.name() + "\" must be used.");
}
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class HardcodedURICheck method isHardcodedURI.
private static boolean isHardcodedURI(ExpressionTree expr) {
ExpressionTree newExpr = ExpressionUtils.skipParentheses(expr);
if (!newExpr.is(Tree.Kind.STRING_LITERAL)) {
return false;
}
String stringLiteral = LiteralUtils.trimQuotes(((LiteralTree) newExpr).value());
return URI_PATTERN.matcher(stringLiteral).find();
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class EnumSetCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
VariableTree variableTree = (VariableTree) tree;
ExpressionTree initializer = variableTree.initializer();
if (initializer == null) {
return;
}
if (initializer.is(Kind.METHOD_INVOCATION)) {
MethodInvocationTree mit = (MethodInvocationTree) initializer;
if (COLLECTIONS_UNMODIFIABLE.matches(mit)) {
// check the collection used as parameter
initializer = mit.arguments().get(0);
} else if (!SET_CREATION_METHODS.anyMatch(mit) || "immutableEnumSet".equals(mit.symbol().name())) {
// but discard any other method invocations (killing the noise)
return;
}
}
checkIssue(initializer.symbolType(), initializer, variableTree.type());
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class ForLoopTerminationConditionCheck method visitForStatement.
@Override
public void visitForStatement(ForStatementTree forStatement) {
ExpressionTree condition = forStatement.condition();
if (condition == null || !condition.is(Tree.Kind.NOT_EQUAL_TO)) {
return;
}
BinaryExpressionTree inequalityCondition = (BinaryExpressionTree) condition;
IntInequality loopVarAndTerminalValue = IntInequality.of(inequalityCondition);
if (loopVarAndTerminalValue != null) {
IdentifierTree loopIdentifier = loopVarAndTerminalValue.identifier;
int terminationValue = loopVarAndTerminalValue.literalValue;
Integer initialValue = initialValue(loopIdentifier, forStatement);
if (initialValue != null && initialValue != terminationValue) {
checkIncrement(forStatement, loopIdentifier, initialValue < terminationValue);
}
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class IdenticalOperandOnBinaryExpressionCheck method checkEqualsMethods.
private void checkEqualsMethods(MethodInvocationTree mit) {
if (EQUALS_MATCHER.matches(mit)) {
if (mit.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
ExpressionTree leftOp = ((MemberSelectExpressionTree) mit.methodSelect()).expression();
ExpressionTree rightOp = mit.arguments().get(0);
if (SyntacticEquivalence.areEquivalent(leftOp, rightOp)) {
reportIssue(rightOp, "Correct one of the identical sub-expressions on both sides of equals.", ImmutableList.of(new JavaFileScannerContext.Location("", leftOp)), null);
}
}
} else if (DEEP_EQUALS_MATCHER.matches(mit) || OBJECTS_EQUALS_MATCHER.matches(mit)) {
ExpressionTree leftOp = mit.arguments().get(0);
ExpressionTree rightOp = mit.arguments().get(1);
if (SyntacticEquivalence.areEquivalent(leftOp, rightOp)) {
reportIssue(rightOp, "Correct one of the identical argument sub-expressions.", ImmutableList.of(new JavaFileScannerContext.Location("", leftOp)), null);
}
}
}
Aggregations