Skip to content

Commit

Permalink
SONARJAVA-4233 Constants accessed with member-select are not supporte…
Browse files Browse the repository at this point in the history
…d by MinMaxRangeCheck
  • Loading branch information
alban-auzeill authored and dorian-burihabwa-sonarsource committed May 25, 2023
1 parent a630731 commit 48fa169
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public double doRangeCheckNOK9(long num) {
return Math.max(result, upper); // Noncompliant
}

public int doRangeCheckNOK10(int num) {
int result = Math.min(MinMaxRangeCheck.LOWER_INT, num);
return Math.max(MinMaxRangeCheck.UPPER_INT, result); // Noncompliant {{Change these chained max/min methods invocations, as final results will always be the upper bound.}}
}

public int doRangeCheckOK1(int num) { // Let's say num = 12
int result = Math.min(UPPER_INT, num); // result = 12
return Math.max(LOWER_INT, result); // Compliant; result is still 12
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonar.java.se.checks;


import java.util.Collections;
import java.util.List;
import java.util.Set;
Expand All @@ -40,6 +41,7 @@
import org.sonar.plugins.java.api.semantic.Type;
import org.sonar.plugins.java.api.tree.ExpressionTree;
import org.sonar.plugins.java.api.tree.IdentifierTree;
import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree;
import org.sonar.plugins.java.api.tree.MethodInvocationTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.UnaryExpressionTree;
Expand Down Expand Up @@ -152,6 +154,8 @@ public ProgramState checkPostStatement(CheckerContext context, Tree syntaxNode)
return handleNumericalLiteral(context, (UnaryExpressionTree) syntaxNode);
case IDENTIFIER:
return handleNumericalConstant(context, (IdentifierTree) syntaxNode);
case MEMBER_SELECT:
return handleNumericalConstant(context, ((MemberSelectExpressionTree) syntaxNode).identifier());
case METHOD_INVOCATION:
return handleMinMaxInvocation(context, (MethodInvocationTree) syntaxNode);
default:
Expand Down

0 comments on commit 48fa169

Please sign in to comment.