use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.
the class ReassignmentFinder method checkAssignment.
private static Optional<AssignmentExpressionTree> checkAssignment(IdentifierTree usage) {
Tree previousTree = usage;
Tree nonParenthesisParent = previousTree.parent();
while (nonParenthesisParent.is(Tree.Kind.PARENTHESIZED_EXPRESSION)) {
previousTree = nonParenthesisParent;
nonParenthesisParent = previousTree.parent();
}
if (nonParenthesisParent instanceof AssignmentExpressionTree) {
AssignmentExpressionTree assignment = (AssignmentExpressionTree) nonParenthesisParent;
if (assignment.variable().equals(previousTree)) {
return Optional.of(assignment);
}
}
return Optional.empty();
}
use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.
the class ReassignmentFinder method getClosestReassignment.
@CheckForNull
private static Tree getClosestReassignment(SyntaxToken startToken, List<AssignmentExpressionTree> reassignments) {
Tree result = null;
List<Tree> assignmentsBeforeStartToken = reassignments.stream().sorted(ReassignmentFinder::isBefore).filter(a -> isBefore(startToken, a) > 0).collect(Collectors.toList());
if (!assignmentsBeforeStartToken.isEmpty()) {
return assignmentsBeforeStartToken.get(assignmentsBeforeStartToken.size() - 1);
}
return result;
}
use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.
the class UselessIncrementCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
if (tree.is(Tree.Kind.RETURN_STATEMENT)) {
ExpressionTree returnExpression = ((ReturnStatementTree) tree).expression();
if (returnExpression != null && isPostfix(returnExpression)) {
UnaryExpressionTree unaryExpression = (UnaryExpressionTree) returnExpression;
ExpressionTree expression = ExpressionUtils.skipParentheses(unaryExpression.expression());
if (expression.is(Tree.Kind.IDENTIFIER) && ((IdentifierTree) expression).symbol().owner().isMethodSymbol()) {
reportIssue(unaryExpression);
}
}
} else {
AssignmentExpressionTree aet = (AssignmentExpressionTree) tree;
if (isPostfix(aet.expression())) {
UnaryExpressionTree postfix = (UnaryExpressionTree) aet.expression();
if (SyntacticEquivalence.areEquivalent(aet.variable(), postfix.expression())) {
reportIssue(postfix);
}
}
}
}
use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.
the class SerializableFieldInSerializableClassCheck method checkCollectionAssignments.
private void checkCollectionAssignments(List<IdentifierTree> usages) {
for (IdentifierTree usage : usages) {
Tree parentTree = usage.parent();
if (parentTree.is(Tree.Kind.ASSIGNMENT)) {
AssignmentExpressionTree assignment = (AssignmentExpressionTree) parentTree;
ExpressionTree expression = assignment.expression();
if (usage.equals(assignment.variable()) && !expression.is(Tree.Kind.NULL_LITERAL) && isUnserializableCollection(expression.symbolType())) {
reportIssue(usage);
}
}
}
}
use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.
the class WrongAssignmentOperatorCheck method visitNode.
@Override
public void visitNode(Tree tree) {
AssignmentExpressionTree aeTree = (AssignmentExpressionTree) tree;
SyntaxToken operatorToken = aeTree.operatorToken();
SyntaxToken expressionFirstToken = aeTree.expression().firstToken();
SyntaxToken variableLastToken = aeTree.variable().lastToken();
if (isSuspiciousToken(expressionFirstToken) && noSpacingBetween(operatorToken, expressionFirstToken) && !noSpacingBetween(variableLastToken, operatorToken)) {
reportIssue(operatorToken, expressionFirstToken, getMessage(expressionFirstToken, aeTree));
}
}
Aggregations