From fe38255f1cf6813b1935168b19cb7a3b84b08b3e Mon Sep 17 00:00:00 2001 From: Marco Kaufmann <83189575+kaufco@users.noreply.github.com> Date: Wed, 15 Nov 2023 15:57:47 +0100 Subject: [PATCH] SONARJAVA-4447 S2185: Rephrase issue message to not use "silly" (#4534) --- .../main/java/checks/ConstantMathCheck.java | 75 ++++++++++++++++++ .../sonar/java/checks/ConstantMathCheck.java | 2 +- .../src/test/files/checks/ConstantMath.java | 77 ------------------- .../java/checks/ConstantMathCheckTest.java | 4 +- 4 files changed, 79 insertions(+), 79 deletions(-) create mode 100644 java-checks-test-sources/src/main/java/checks/ConstantMathCheck.java delete mode 100644 java-checks/src/test/files/checks/ConstantMath.java diff --git a/java-checks-test-sources/src/main/java/checks/ConstantMathCheck.java b/java-checks-test-sources/src/main/java/checks/ConstantMathCheck.java new file mode 100644 index 00000000000..f374c6ed9ef --- /dev/null +++ b/java-checks-test-sources/src/main/java/checks/ConstantMathCheck.java @@ -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"}} + } +} diff --git a/java-checks/src/main/java/org/sonar/java/checks/ConstantMathCheck.java b/java-checks/src/main/java/org/sonar/java/checks/ConstantMathCheck.java index 38eb8892036..fbd833f1f27 100644 --- a/java-checks/src/main/java/org/sonar/java/checks/ConstantMathCheck.java +++ b/java-checks/src/main/java/org/sonar/java/checks/ConstantMathCheck.java @@ -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())); } } } diff --git a/java-checks/src/test/files/checks/ConstantMath.java b/java-checks/src/test/files/checks/ConstantMath.java deleted file mode 100644 index 2086a47c00a..00000000000 --- a/java-checks/src/test/files/checks/ConstantMath.java +++ /dev/null @@ -1,77 +0,0 @@ -public class TestClass { - - public void method(int a, Integer integer, Double doubleParam) { - byte b = a % 1; // Noncompliant [[sc=16;ec=17]] {{Remove this computation of % 1, which always evaluates to zero.}} - double v = 7.4d; - double remainder = v % 1; // compliant, remainder is ~0.4 - b = integer % 1; // Noncompliant - b = doubleParam % 1; // Compliant, double - int c = a % 2; // Compliant - int d = a % a; // Compliant, currently not covered by this rule - short s; - - Math.abs((double)' '); // Noncompliant [[sc=5;ec=13]] {{Remove this silly call to "Math.abs"}} - Math.abs((float) 0); // Noncompliant {{Remove this silly call to "Math.abs"}} - Math.abs(0); // Noncompliant {{Remove this silly call to "Math.abs"}} - Math.abs(0L); // Noncompliant {{Remove this silly call to "Math.abs"}} - Math.abs(a); // Compliant - Math.abs((float) a); // Compliant - - // the following does not compile, but is checked anyway from a type perspective. - Math.ceil((double) true); // Noncompliant {{Remove this silly call to "Math.ceil"}} - Math.ceil((double) new Object()); // Compliant - - Math.ceil(a); // Noncompliant {{Remove this silly call to "Math.ceil"}} - Math.ceil((int) a); // Noncompliant {{Remove this silly call to "Math.ceil"}} - Math.ceil((double) b); // Noncompliant {{Remove this silly call to "Math.ceil"}} - Math.ceil((double) ' '); // Noncompliant {{Remove this silly call to "Math.ceil"}} - Math.ceil((double) 0); // Noncompliant {{Remove this silly call to "Math.ceil"}} - Math.ceil((double) 0L); // Noncompliant {{Remove this silly call to "Math.ceil"}} - Math.ceil((double) s); // Noncompliant {{Remove this silly call to "Math.ceil"}} - Math.ceil((double) 0.0f); // Compliant - Math.ceil((double) 0.0d); // Compliant - Math.ceil((float) (byte) 0); // Noncompliant {{Remove this silly call to "Math.ceil"}} - Math.floor((float) 0); // Noncompliant {{Remove this silly call to "Math.floor"}} - Math.rint((float) 0); // Noncompliant {{Remove this silly call to "Math.rint"}} - Math.round((float) 0); // Noncompliant {{Remove this silly call to "Math.round"}} - Math.ceil(((double) ((double) (a)))); // Noncompliant {{Remove this silly call to "Math.ceil"}} - - float value; - 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 silly call to "Math.acos"}} - Math.acos((0.0F)); // Noncompliant {{Remove this silly call to "Math.acos"}} - Math.acos((1.0)); // Noncompliant {{Remove this silly call to "Math.acos"}} - Math.acos((1.0f)); // Noncompliant {{Remove this silly call to "Math.acos"}} - Math.asin(0.0d); // Noncompliant {{Remove this silly call to "Math.asin"}} - Math.asin(1.0d); // Noncompliant {{Remove this silly call to "Math.asin"}} - Math.atan(0.0d); // Noncompliant {{Remove this silly call to "Math.atan"}} - Math.atan(1.0d); // Noncompliant {{Remove this silly call to "Math.atan"}} - Math.atan2(0.0D, value); // Noncompliant {{Remove this silly call to "Math.atan2"}} - Math.cbrt(0.0d); // Noncompliant {{Remove this silly call to "Math.cbrt"}} - Math.cbrt(1.0d); // Noncompliant {{Remove this silly call to "Math.cbrt"}} - Math.cos((0.0d)); // Noncompliant {{Remove this silly call to "Math.cos"}} - Math.cosh(0.0d); // Noncompliant {{Remove this silly call to "Math.cosh"}} - Math.exp(0.0d); // Noncompliant {{Remove this silly call to "Math.exp"}} - Math.exp(1.0d); // Noncompliant {{Remove this silly call to "Math.exp"}} - Math.expm1(0.0d); // Noncompliant {{Remove this silly call to "Math.expm1"}} - Math.log(0.0d); // Noncompliant {{Remove this silly call to "Math.log"}} - Math.log(1.0d); // Noncompliant {{Remove this silly call to "Math.log"}} - Math.log10(0.0d); // Noncompliant {{Remove this silly call to "Math.log10"}} - Math.log10(1.0d); // Noncompliant {{Remove this silly call to "Math.log10"}} - Math.sin(0.0d); // Noncompliant {{Remove this silly call to "Math.sin"}} - Math.sinh(0.0d); // Noncompliant {{Remove this silly call to "Math.sinh"}} - Math.sqrt(0.0d); // Noncompliant {{Remove this silly call to "Math.sqrt"}} - Math.sqrt(1.0d); // Noncompliant {{Remove this silly call to "Math.sqrt"}} - Math.tan(0.0d); // Noncompliant {{Remove this silly call to "Math.tan"}} - Math.tanh(0.0d); // Noncompliant {{Remove this silly call to "Math.tanh"}} - Math.toDegrees(0.0d); // Noncompliant {{Remove this silly call to "Math.toDegrees"}} - Math.toDegrees(1.0d); // Noncompliant {{Remove this silly call to "Math.toDegrees"}} - Math.toRadians(0.0d); // Noncompliant {{Remove this silly call to "Math.toRadians"}} - } - -} diff --git a/java-checks/src/test/java/org/sonar/java/checks/ConstantMathCheckTest.java b/java-checks/src/test/java/org/sonar/java/checks/ConstantMathCheckTest.java index 3e7c6025508..b4825f7daf6 100644 --- a/java-checks/src/test/java/org/sonar/java/checks/ConstantMathCheckTest.java +++ b/java-checks/src/test/java/org/sonar/java/checks/ConstantMathCheckTest.java @@ -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(); }