use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.
the class MutableMembersUsageCheck method assignementsOfMutableType.
private static boolean assignementsOfMutableType(List<IdentifierTree> usages) {
for (IdentifierTree usage : usages) {
Tree current = usage;
Tree parent = usage.parent();
do {
if (parent.is(Tree.Kind.ASSIGNMENT)) {
break;
}
current = parent;
parent = current.parent();
} while (parent != null);
if (parent != null) {
AssignmentExpressionTree assignment = (AssignmentExpressionTree) parent;
if (assignment.variable().equals(current) && isMutableType(assignment.expression())) {
return true;
}
}
}
return false;
}
use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.
the class OverwrittenKeyCheck method isArrayAssignment.
@CheckForNull
private static CollectionAndKey isArrayAssignment(StatementTree statementTree) {
if (statementTree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
ExpressionTree expression = ((ExpressionStatementTree) statementTree).expression();
if (expression.is(Tree.Kind.ASSIGNMENT)) {
AssignmentExpressionTree assignment = (AssignmentExpressionTree) expression;
ExpressionTree variable = assignment.variable();
if (variable.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
ArrayAccessExpressionTree aaet = (ArrayAccessExpressionTree) variable;
Symbol collection = symbolFromIdentifier(aaet.expression());
ExpressionTree keyTree = aaet.dimension().expression();
Object key = extractKey(keyTree);
if (collection != null && key != null) {
return new CollectionAndKey(collection, keyTree, key, true, assignment.expression());
}
}
}
}
return null;
}
use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.
the class ParameterReassignedToCheck method visitAssignmentExpression.
@Override
public void visitAssignmentExpression(AssignmentExpressionTree tree) {
ExpressionTree variable = tree.variable();
if (variable.is(Tree.Kind.IDENTIFIER)) {
IdentifierTree identifier = (IdentifierTree) variable;
Symbol reference = identifier.symbol();
if (reference.isVariableSymbol() && variables.contains(reference)) {
context.reportIssue(this, identifier, "Introduce a new variable instead of reusing the parameter \"" + identifier.name() + "\".");
}
}
}
use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.
the class AnnotationDefaultArgumentCheck method visitNode.
@Override
public void visitNode(Tree tree) {
AnnotationTree annotationTree = (AnnotationTree) tree;
TypeSymbol annotationSymbol = annotationTree.symbolType().symbol();
if (annotationSymbol.isUnknown()) {
return;
}
Map<String, Object> defaultValueByName = annotationSymbol.memberSymbols().stream().filter(Symbol::isMethodSymbol).map(s -> (JavaSymbol.MethodJavaSymbol) s).filter(s -> s.defaultValue() != null).collect(Collectors.toMap(Symbol::name, JavaSymbol.MethodJavaSymbol::defaultValue));
for (ExpressionTree argument : annotationTree.arguments()) {
Tree valueSet = argument;
// Single element annotation : JLS8 9.7.3 : one param must be named value.
String paramName = "value";
if (argument.is(Tree.Kind.ASSIGNMENT)) {
AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) argument;
IdentifierTree nameTree = (IdentifierTree) assignmentTree.variable();
paramName = nameTree.name();
valueSet = assignmentTree.expression();
}
if (setValueIsSameAsDefaultValue(defaultValueByName.get(paramName), valueSet)) {
reportIssue(argument, String.format("Remove this default value assigned to parameter \"%s\".", paramName));
}
}
}
use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.
the class DeadStoreCheck method handleAssignment.
private void handleAssignment(Set<Symbol> out, Set<Tree> assignmentLHS, AssignmentExpressionTree element) {
ExpressionTree lhs = ExpressionUtils.skipParentheses(element.variable());
if (lhs.is(Tree.Kind.IDENTIFIER)) {
Symbol symbol = ((IdentifierTree) lhs).symbol();
if (isLocalVariable(symbol) && !out.contains(symbol) && (element.is(Tree.Kind.ASSIGNMENT) || isParentExpressionStatement(element))) {
createIssue(element.operatorToken(), element.expression(), symbol);
}
assignmentLHS.add(lhs);
if (element.is(Tree.Kind.ASSIGNMENT)) {
out.remove(symbol);
} else {
out.add(symbol);
}
}
}
Aggregations