Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public void nonCompliantAssignments() {
staticValue = value + 1; // Noncompliant {{Make the enclosing method "static" or remove this set.}}
// ^^^^^^^^^^^
staticValue += value; // Noncompliant {{Make the enclosing method "static" or remove this set.}}
staticValue >>= value; // Noncompliant {{Make the enclosing method "static" or remove this set.}}
staticValue++; // Noncompliant {{Make the enclosing method "static" or remove this set.}}
++staticValue; // Noncompliant {{Make the enclosing method "static" or remove this set.}}
StaticFieldUpateCheckSample.staticValue++; // Noncompliant {{Make the enclosing method "static" or remove this set.}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,6 @@
@Rule(key = "S1121")
public class AssignmentInSubExpressionCheck extends BaseTreeVisitor implements JavaFileScanner {

private static final Kind[] ASSIGNMENT_EXPRESSIONS = new Kind[]{
Kind.AND_ASSIGNMENT,
Kind.ASSIGNMENT,
Kind.DIVIDE_ASSIGNMENT,
Kind.LEFT_SHIFT_ASSIGNMENT,
Kind.RIGHT_SHIFT_ASSIGNMENT,
Kind.MINUS_ASSIGNMENT,
Kind.MULTIPLY_ASSIGNMENT,
Kind.OR_ASSIGNMENT,
Kind.PLUS_ASSIGNMENT,
Kind.REMAINDER_ASSIGNMENT,
Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT,
Kind.XOR_ASSIGNMENT};

private JavaFileScannerContext context;

@Override
Expand All @@ -71,7 +57,7 @@ public void visitAnnotation(AnnotationTree annotationTree) {
@Override
public void visitLambdaExpression(LambdaExpressionTree lambdaExpressionTree) {
//skip lambda if body is an assignment
if(!lambdaExpressionTree.body().is(ASSIGNMENT_EXPRESSIONS)) {
if(!(lambdaExpressionTree.body() instanceof AssignmentExpressionTree)) {
super.visitLambdaExpression(lambdaExpressionTree);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void setContext(JavaFileScannerContext context) {

@Override
public List<Tree.Kind> nodesToVisit() {
return ListUtils.concat(Tree.Kind.CLASS_KINDS, List.of(
return ListUtils.concat(Tree.Kind.CLASS_KINDS, Tree.Kind.ASSIGNMENT_KINDS, List.of(
Comment thread
aurelien-coet-sonarsource marked this conversation as resolved.
Tree.Kind.POSTFIX_INCREMENT,
Tree.Kind.POSTFIX_DECREMENT,
Tree.Kind.PREFIX_INCREMENT,
Expand Down Expand Up @@ -92,18 +92,6 @@ public List<Tree.Kind> nodesToVisit() {
Tree.Kind.TYPE_CAST,
Tree.Kind.INSTANCE_OF,
Tree.Kind.PARENTHESIZED_EXPRESSION,
Tree.Kind.ASSIGNMENT,
Tree.Kind.MULTIPLY_ASSIGNMENT,
Tree.Kind.DIVIDE_ASSIGNMENT,
Tree.Kind.REMAINDER_ASSIGNMENT,
Tree.Kind.PLUS_ASSIGNMENT,
Tree.Kind.MINUS_ASSIGNMENT,
Tree.Kind.LEFT_SHIFT_ASSIGNMENT,
Tree.Kind.RIGHT_SHIFT_ASSIGNMENT,
Tree.Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT,
Tree.Kind.AND_ASSIGNMENT,
Tree.Kind.XOR_ASSIGNMENT,
Tree.Kind.OR_ASSIGNMENT,
Tree.Kind.INT_LITERAL,
Tree.Kind.LONG_LITERAL,
Tree.Kind.FLOAT_LITERAL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,6 @@
@Rule(key = "S2696")
public class StaticFieldUpateCheck extends AbstractInSynchronizeChecker {

private static final Kind[] ASSIGNMENT_EXPRESSIONS = new Kind[]{
Kind.AND_ASSIGNMENT,
Kind.ASSIGNMENT,
Kind.DIVIDE_ASSIGNMENT,
Kind.LEFT_SHIFT_ASSIGNMENT,
Kind.MINUS_ASSIGNMENT,
Kind.MULTIPLY_ASSIGNMENT,
Kind.OR_ASSIGNMENT,
Kind.PLUS_ASSIGNMENT,
Kind.REMAINDER_ASSIGNMENT,
Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT,
Kind.XOR_ASSIGNMENT};

private static final Kind[] UNARY_EXPRESSIONS = new Kind[]{
Kind.POSTFIX_DECREMENT,
Kind.POSTFIX_INCREMENT,
Expand All @@ -65,7 +52,7 @@ public class StaticFieldUpateCheck extends AbstractInSynchronizeChecker {
public List<Kind> nodesToVisit() {
ArrayList<Kind> nodesToVisit = new ArrayList<>(super.nodesToVisit());
nodesToVisit.add(Kind.STATIC_INITIALIZER);
nodesToVisit.addAll(Arrays.asList(ASSIGNMENT_EXPRESSIONS));
nodesToVisit.addAll(Kind.ASSIGNMENT_KINDS);
nodesToVisit.addAll(Arrays.asList(UNARY_EXPRESSIONS));
return nodesToVisit;
}
Expand All @@ -80,8 +67,8 @@ public void visitNode(Tree tree) {
} else if (tree.is(Kind.STATIC_INITIALIZER)) {
withinStaticMethod.push(true);
} else if (isInInstanceMethod() && !hasAnyParentStatic() && !hasAnyParentSync()) {
if (tree.is(ASSIGNMENT_EXPRESSIONS)) {
checkVariableModification(((AssignmentExpressionTree) tree).variable());
if (tree instanceof AssignmentExpressionTree assignment) {
checkVariableModification(assignment.variable());
} else if (tree.is(UNARY_EXPRESSIONS)) {
checkVariableModification(((UnaryExpressionTree) tree).expression());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.sonar.plugins.java.api.tree.Modifier;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.UnaryExpressionTree;
import org.sonarsource.analyzer.commons.collections.ListUtils;

@Rule(key = "S3078")
public class VolatileVariablesOperationsCheck extends IssuableSubscriptionVisitor {
Expand All @@ -45,24 +46,12 @@ public class VolatileVariablesOperationsCheck extends IssuableSubscriptionVisito

@Override
public List<Tree.Kind> nodesToVisit() {
return List.of(
return ListUtils.concat(Tree.Kind.ASSIGNMENT_KINDS, List.of(
Tree.Kind.PREFIX_DECREMENT,
Tree.Kind.PREFIX_INCREMENT,
Tree.Kind.POSTFIX_DECREMENT,
Tree.Kind.POSTFIX_INCREMENT,
Tree.Kind.ASSIGNMENT,
Tree.Kind.PLUS_ASSIGNMENT,
Tree.Kind.MINUS_ASSIGNMENT,
Tree.Kind.MULTIPLY_ASSIGNMENT,
Tree.Kind.DIVIDE_ASSIGNMENT,
Tree.Kind.REMAINDER_ASSIGNMENT,
Tree.Kind.LEFT_SHIFT_ASSIGNMENT,
Tree.Kind.RIGHT_SHIFT_ASSIGNMENT,
Tree.Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT,
Tree.Kind.AND_ASSIGNMENT,
Tree.Kind.XOR_ASSIGNMENT,
Tree.Kind.OR_ASSIGNMENT
);
Tree.Kind.POSTFIX_INCREMENT
));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,6 @@ public class UnusedPrivateFieldCheck extends IssuableSubscriptionVisitor {
"lombok.AllArgsConstructor"
);

private static final Tree.Kind[] ASSIGNMENT_KINDS = {
Tree.Kind.ASSIGNMENT,
Tree.Kind.MULTIPLY_ASSIGNMENT,
Tree.Kind.DIVIDE_ASSIGNMENT,
Tree.Kind.REMAINDER_ASSIGNMENT,
Tree.Kind.PLUS_ASSIGNMENT,
Tree.Kind.MINUS_ASSIGNMENT,
Tree.Kind.LEFT_SHIFT_ASSIGNMENT,
Tree.Kind.RIGHT_SHIFT_ASSIGNMENT,
Tree.Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT,
Tree.Kind.AND_ASSIGNMENT,
Tree.Kind.XOR_ASSIGNMENT,
Tree.Kind.OR_ASSIGNMENT};

private final List<ClassTree> classes = new ArrayList<>();
private final Map<Symbol, List<AssignmentExpressionTree>> assignments = new HashMap<>();
private final Set<String> unknownIdentifiers = new HashSet<>();
Expand Down Expand Up @@ -248,9 +234,8 @@ private Set<String> getIgnoredAnnotations() {
}

private void collectAssignment(ExpressionTree expressionTree) {
if (expressionTree.is(ASSIGNMENT_KINDS)) {
AssignmentExpressionTree assignmentExpressionTree = (AssignmentExpressionTree) expressionTree;
ExpressionTree variable = (assignmentExpressionTree).variable();
if (expressionTree instanceof AssignmentExpressionTree assignmentExpressionTree) {
ExpressionTree variable = assignmentExpressionTree.variable();
IdentifierTree identifier = null;
if (variable.is(Tree.Kind.IDENTIFIER)) {
identifier = (IdentifierTree) variable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,29 @@ enum Kind implements GrammarRuleKey {
IMPLICIT_CLASS
);

/**
* The {@link Kind}s of all assignment expressions: the simple {@link Kind#ASSIGNMENT} and the eleven compound assignment
* operators. All of these are backed by {@link AssignmentExpressionTree}.
*
* <p>Use this as the single source of truth when a visitor or predicate must handle every assignment structure, instead of
* hand-listing the kinds. In particular, it can be returned directly from {@code nodesToVisit()} of a subscription visitor that
* needs to visit all assignments.</p>
*/
public static final List<Kind> ASSIGNMENT_KINDS = List.of(
ASSIGNMENT,
MULTIPLY_ASSIGNMENT,
DIVIDE_ASSIGNMENT,
REMAINDER_ASSIGNMENT,
PLUS_ASSIGNMENT,
MINUS_ASSIGNMENT,
LEFT_SHIFT_ASSIGNMENT,
RIGHT_SHIFT_ASSIGNMENT,
UNSIGNED_RIGHT_SHIFT_ASSIGNMENT,
AND_ASSIGNMENT,
XOR_ASSIGNMENT,
OR_ASSIGNMENT
);

final Class<? extends Tree> associatedInterface;

Kind(Class<? extends Tree> associatedInterface) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ void class_kinds_contains_exactly_all_kinds_backed_by_class_tree() {
assertThat(Tree.Kind.CLASS_KINDS).containsExactlyInAnyOrder(expected);
}

@Test
void assignment_kinds_contains_exactly_all_kinds_backed_by_assignment_expression_tree() {
Tree.Kind[] expected = Arrays.stream(Tree.Kind.values())
.filter(kind -> kind.getAssociatedInterface() == AssignmentExpressionTree.class)
.toArray(Tree.Kind[]::new);
assertThat(Tree.Kind.ASSIGNMENT_KINDS).containsExactlyInAnyOrder(expected);
}

}
Loading