Skip to content

Commit

Permalink
SONARJAVA-4447 S2185: Rephrase issue message to not use "silly" (#4534)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaufco authored Nov 15, 2023
1 parent 15b708d commit fe38255
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package checks;

public class ConstantMathCheck {

public void method(int a, Integer integer, Double doubleParam) {
byte b = (byte) (a % 1); // Noncompliant [[sc=24;ec=25]] {{Remove this computation of % 1, which always evaluates to zero.}}
double v = 7.4d;
double remainder = v % 1; // compliant, remainder is ~0.4
b = (byte) (integer % 1); // Noncompliant
b = (byte) (doubleParam % 1); // Compliant, double
int c = a % 2; // Compliant
int d = a % a; // Compliant, currently not covered by this rule
short s = 0x7fff;

Math.abs((double)' '); // Noncompliant [[sc=5;ec=13]] {{Remove this unnecessary call to "Math.abs"}}
Math.abs((float) 0); // Noncompliant {{Remove this unnecessary call to "Math.abs"}}
Math.abs(0); // Noncompliant {{Remove this unnecessary call to "Math.abs"}}
Math.abs(0L); // Noncompliant {{Remove this unnecessary call to "Math.abs"}}
Math.abs(a); // Compliant
Math.abs((float) a); // Compliant
Math.ceil((double) new Object()); // Compliant

Math.ceil(a); // Noncompliant {{Remove this unnecessary call to "Math.ceil"}}
Math.ceil((int) a); // Noncompliant {{Remove this unnecessary call to "Math.ceil"}}
Math.ceil((double) b); // Noncompliant {{Remove this unnecessary call to "Math.ceil"}}
Math.ceil((double) ' '); // Noncompliant {{Remove this unnecessary call to "Math.ceil"}}
Math.ceil((double) 0); // Noncompliant {{Remove this unnecessary call to "Math.ceil"}}
Math.ceil((double) 0L); // Noncompliant {{Remove this unnecessary call to "Math.ceil"}}
Math.ceil((double) s); // Noncompliant {{Remove this unnecessary call to "Math.ceil"}}
Math.ceil((double) 0.0f); // Compliant
Math.ceil((double) 0.0d); // Compliant
Math.ceil((float) (byte) 0); // Noncompliant {{Remove this unnecessary call to "Math.ceil"}}
Math.floor((float) 0); // Noncompliant {{Remove this unnecessary call to "Math.floor"}}
Math.rint((float) 0); // Noncompliant {{Remove this unnecessary call to "Math.rint"}}
Math.round((float) 0); // Noncompliant {{Remove this unnecessary call to "Math.round"}}
Math.ceil(((double) ((double) (a)))); // Noncompliant {{Remove this unnecessary call to "Math.ceil"}}

float value = 3.14f;
Math.acos(value); // Compliant
Math.cos(value); // Compliant
Math.acos(2.0); // Compliant
Math.cos(1.0); // Compliant
Math.cos(2.0); // Compliant

Math.acos((0.0)); // Noncompliant {{Remove this unnecessary call to "Math.acos"}}
Math.acos((0.0F)); // Noncompliant {{Remove this unnecessary call to "Math.acos"}}
Math.acos((1.0)); // Noncompliant {{Remove this unnecessary call to "Math.acos"}}
Math.acos((1.0f)); // Noncompliant {{Remove this unnecessary call to "Math.acos"}}
Math.asin(0.0d); // Noncompliant {{Remove this unnecessary call to "Math.asin"}}
Math.asin(1.0d); // Noncompliant {{Remove this unnecessary call to "Math.asin"}}
Math.atan(0.0d); // Noncompliant {{Remove this unnecessary call to "Math.atan"}}
Math.atan(1.0d); // Noncompliant {{Remove this unnecessary call to "Math.atan"}}
Math.atan2(0.0D, value); // Noncompliant {{Remove this unnecessary call to "Math.atan2"}}
Math.cbrt(0.0d); // Noncompliant {{Remove this unnecessary call to "Math.cbrt"}}
Math.cbrt(1.0d); // Noncompliant {{Remove this unnecessary call to "Math.cbrt"}}
Math.cos((0.0d)); // Noncompliant {{Remove this unnecessary call to "Math.cos"}}
Math.cosh(0.0d); // Noncompliant {{Remove this unnecessary call to "Math.cosh"}}
Math.exp(0.0d); // Noncompliant {{Remove this unnecessary call to "Math.exp"}}
Math.exp(1.0d); // Noncompliant {{Remove this unnecessary call to "Math.exp"}}
Math.expm1(0.0d); // Noncompliant {{Remove this unnecessary call to "Math.expm1"}}
Math.log(0.0d); // Noncompliant {{Remove this unnecessary call to "Math.log"}}
Math.log(1.0d); // Noncompliant {{Remove this unnecessary call to "Math.log"}}
Math.log10(0.0d); // Noncompliant {{Remove this unnecessary call to "Math.log10"}}
Math.log10(1.0d); // Noncompliant {{Remove this unnecessary call to "Math.log10"}}
Math.sin(0.0d); // Noncompliant {{Remove this unnecessary call to "Math.sin"}}
Math.sinh(0.0d); // Noncompliant {{Remove this unnecessary call to "Math.sinh"}}
Math.sqrt(0.0d); // Noncompliant {{Remove this unnecessary call to "Math.sqrt"}}
Math.sqrt(1.0d); // Noncompliant {{Remove this unnecessary call to "Math.sqrt"}}
Math.tan(0.0d); // Noncompliant {{Remove this unnecessary call to "Math.tan"}}
Math.tanh(0.0d); // Noncompliant {{Remove this unnecessary call to "Math.tanh"}}
Math.toDegrees(0.0d); // Noncompliant {{Remove this unnecessary call to "Math.toDegrees"}}
Math.toDegrees(1.0d); // Noncompliant {{Remove this unnecessary call to "Math.toDegrees"}}
Math.toRadians(0.0d); // Noncompliant {{Remove this unnecessary call to "Math.toRadians"}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void visitNode(Tree tree) {
} else {
MethodInvocationTree mit = (MethodInvocationTree) tree;
if (isConstantWithLiteral(mit) || isTruncation(mit) || isConstantWithZero(mit) || isConstantWithZeroOrOne(mit)) {
reportIssue(mit.methodSelect(), String.format("Remove this silly call to \"Math.%s\"", mit.methodSymbol().name()));
reportIssue(mit.methodSelect(), String.format("Remove this unnecessary call to \"Math.%s\"", mit.methodSymbol().name()));
}
}
}
Expand Down
77 changes: 0 additions & 77 deletions java-checks/src/test/files/checks/ConstantMath.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;

import static org.sonar.java.checks.verifier.TestUtils.mainCodeSourcesPath;

class ConstantMathCheckTest {

@Test
void test() {
CheckVerifier.newVerifier()
.onFile("src/test/files/checks/ConstantMath.java")
.onFile(mainCodeSourcesPath("checks/ConstantMathCheck.java"))
.withCheck(new ConstantMathCheck())
.verifyIssues();
}
Expand Down

0 comments on commit fe38255

Please sign in to comment.