-
Notifications
You must be signed in to change notification settings - Fork 724
SONARJAVA-6825: Implement rule S9360 - Constant expressions and comparisons should be simplified #6036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
SONARJAVA-6825: Implement rule S9360 - Constant expressions and comparisons should be simplified #6036
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5a4b3d4
SONARJAVA-6825: import metadata
romainbrenguier 36d0e82
SONARJAVA-6825: Implemented rule S9360 Constant expressions and compa…
romainbrenguier afab0cb
SONARJAVA-6825: Add constant Math call detection to S9360
romainbrenguier 1546ee8
SONARJAVA-6825: Fix compilation error in YodaConditionCheckSample
romainbrenguier d305216
Update ruling results for PR #6036 (#6037)
github-actions[bot] 1eecee6
SONARJAVA-6825: Fix S9360 review findings and improve rule quality
romainbrenguier 8abb0d2
Update ruling results
github-actions[bot] b918b7d
SONARJAVA-6825: Address remaining review findings for S9360
romainbrenguier 676de5f
Update ruling results for guava after S9360 behavior changes
romainbrenguier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
its/ruling/src/test/resources/commons-beanutils/java-S9360.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "commons-beanutils:commons-beanutils:src/main/java/org/apache/commons/beanutils2/ConstructorUtils.java": [ | ||
| 106, | ||
| 154, | ||
| 218, | ||
| 267 | ||
| ] | ||
| } |
59 changes: 59 additions & 0 deletions
59
its/ruling/src/test/resources/eclipse-jetty-similar-to-main/java-S9360.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| { | ||
| "org.eclipse.jetty:jetty-project:jetty-http/src/main/java/org/eclipse/jetty/http/HttpStatus.java": [ | ||
| 343, | ||
| 358, | ||
| 373, | ||
| 388, | ||
| 403 | ||
| ], | ||
| "org.eclipse.jetty:jetty-project:jetty-http/src/main/java/org/eclipse/jetty/http/HttpURI.java": [ | ||
| 1028 | ||
| ], | ||
| "org.eclipse.jetty:jetty-project:jetty-http/src/main/java/org/eclipse/jetty/http/MimeTypes.java": [ | ||
| 431, | ||
| 436, | ||
| 445, | ||
| 453, | ||
| 455, | ||
| 459, | ||
| 465, | ||
| 471, | ||
| 477, | ||
| 483, | ||
| 489, | ||
| 495, | ||
| 497, | ||
| 501, | ||
| 503, | ||
| 514, | ||
| 514, | ||
| 515, | ||
| 575, | ||
| 615, | ||
| 617, | ||
| 622, | ||
| 624, | ||
| 628, | ||
| 634, | ||
| 640, | ||
| 646, | ||
| 652, | ||
| 658, | ||
| 664, | ||
| 666, | ||
| 670, | ||
| 677, | ||
| 684 | ||
| ], | ||
| "org.eclipse.jetty:jetty-project:jetty-http/src/main/java/org/eclipse/jetty/http/pathmap/ServletPathSpec.java": [ | ||
| 93, | ||
| 414 | ||
| ], | ||
| "org.eclipse.jetty:jetty-project:jetty-io/src/test/java/org/eclipse/jetty/io/ArrayByteBufferPoolTest.java": [ | ||
| 94 | ||
| ], | ||
| "org.eclipse.jetty:jetty-project:jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java": [ | ||
| 744, | ||
| 835 | ||
| ] | ||
| } |
75 changes: 75 additions & 0 deletions
75
its/ruling/src/test/resources/eclipse-jetty/java-S9360.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| { | ||
| "org.eclipse.jetty:jetty-project:jetty-http/src/main/java/org/eclipse/jetty/http/HttpStatus.java": [ | ||
| 343, | ||
| 358, | ||
| 373, | ||
| 388, | ||
| 403 | ||
| ], | ||
| "org.eclipse.jetty:jetty-project:jetty-http/src/main/java/org/eclipse/jetty/http/HttpURI.java": [ | ||
| 1028 | ||
| ], | ||
| "org.eclipse.jetty:jetty-project:jetty-http/src/main/java/org/eclipse/jetty/http/MimeTypes.java": [ | ||
| 431, | ||
| 436, | ||
| 445, | ||
| 453, | ||
| 455, | ||
| 459, | ||
| 465, | ||
| 471, | ||
| 477, | ||
| 483, | ||
| 489, | ||
| 495, | ||
| 497, | ||
| 501, | ||
| 503, | ||
| 514, | ||
| 514, | ||
| 515, | ||
| 575, | ||
| 615, | ||
| 617, | ||
| 622, | ||
| 624, | ||
| 628, | ||
| 634, | ||
| 640, | ||
| 646, | ||
| 652, | ||
| 658, | ||
| 664, | ||
| 666, | ||
| 670, | ||
| 677, | ||
| 684 | ||
| ], | ||
| "org.eclipse.jetty:jetty-project:jetty-http/src/main/java/org/eclipse/jetty/http/pathmap/ServletPathSpec.java": [ | ||
| 93, | ||
| 414 | ||
| ], | ||
| "org.eclipse.jetty:jetty-project:jetty-io/src/test/java/org/eclipse/jetty/io/ArrayByteBufferPoolTest.java": [ | ||
| 94 | ||
| ], | ||
| "org.eclipse.jetty:jetty-project:jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java": [ | ||
| 744, | ||
| 835 | ||
| ], | ||
| "org.eclipse.jetty:jetty-project:jetty-util-ajax/src/main/java/org/eclipse/jetty/util/ajax/AsyncJSON.java": [ | ||
| 1275 | ||
| ], | ||
| "org.eclipse.jetty:jetty-project:jetty-util/src/main/java/org/eclipse/jetty/util/StringUtil.java": [ | ||
| 901, | ||
| 907, | ||
| 925, | ||
| 942, | ||
| 957, | ||
| 962, | ||
| 978 | ||
| ], | ||
| "org.eclipse.jetty:jetty-project:jetty-util/src/main/java/org/eclipse/jetty/util/security/Password.java": [ | ||
| 117, | ||
| 132 | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "com.google.guava:guava:src/com/google/common/base/SmallCharMatcher.java": [ | ||
| 61 | ||
| ], | ||
| "com.google.guava:guava:src/com/google/common/io/LittleEndianDataInputStream.java": [ | ||
| 82, | ||
| 225 | ||
| ], | ||
| "com.google.guava:guava:src/com/google/common/math/LongMath.java": [ | ||
| 234 | ||
| ], | ||
| "com.google.guava:guava:src/com/google/common/net/MediaType.java": [ | ||
| 628, | ||
| 631, | ||
| 632 | ||
| ] | ||
| } |
10 changes: 10 additions & 0 deletions
10
...ks-test-sources/default/src/main/files/non-compiling/checks/YodaConditionCheckSample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package checks; | ||
|
|
||
| class YodaConditionCheckSample { | ||
|
|
||
| void unknownLiteralType() { | ||
| Object x = new Object(); | ||
| if (UNKNOWN_LITERAL == x) { } // Compliant - UNKNOWN_LITERAL is not a valid literal | ||
| } | ||
|
|
||
| } |
193 changes: 193 additions & 0 deletions
193
java-checks-test-sources/default/src/main/java/checks/YodaConditionCheckSample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| package checks; | ||
|
|
||
| class YodaConditionCheckSample { | ||
|
|
||
| void testIntLiteral() { | ||
| int count = 0; | ||
| int x = 5; | ||
| if (0 == count) { } // Noncompliant {{Put the variable on the left side of this comparison.}} | ||
| // ^ | ||
| if (5 != x) { } // Noncompliant {{Put the variable on the left side of this comparison.}} | ||
| // ^ | ||
| if (count == 0) { } // Compliant | ||
| if (x != 5) { } // Compliant | ||
| } | ||
|
|
||
| void testNullLiteral() { | ||
| Object obj = null; | ||
| Object myObject = null; | ||
| if (null == obj) { } // Noncompliant {{Put the variable on the left side of this comparison.}} | ||
| // ^^^^ | ||
| if (null != myObject) { } // Noncompliant {{Put the variable on the left side of this comparison.}} | ||
| // ^^^^ | ||
| if (obj == null) { } // Compliant | ||
| if (myObject != null) { } // Compliant | ||
| if (null == null) { } // Compliant | ||
| } | ||
|
|
||
| void testBooleanLiteral() { | ||
| boolean flag = true; | ||
| boolean result = false; | ||
| if (true == flag) { } // Compliant - boolean literal comparisons handled by S1125 | ||
| if (false != result) { } // Compliant - boolean literal comparisons handled by S1125 | ||
| if (flag == true) { } // Compliant | ||
| if (result != false) { } // Compliant | ||
| } | ||
|
|
||
| void testStringLiteral() { | ||
| String str = "hello"; | ||
| String value = ""; | ||
| if ("hello" == str) { } // Noncompliant {{Put the variable on the left side of this comparison.}} | ||
| // ^^^^^^^ | ||
| if ("" != value) { } // Noncompliant {{Put the variable on the left side of this comparison.}} | ||
| // ^^ | ||
| if (str == "hello") { } // Compliant | ||
| if (value != "") { } // Compliant | ||
| } | ||
|
|
||
| void testCharLiteral() { | ||
| char ch = 'a'; | ||
| if ('a' == ch) { } // Noncompliant {{Put the variable on the left side of this comparison.}} | ||
| // ^^^ | ||
| if (ch == 'a') { } // Compliant | ||
| } | ||
|
|
||
| void testFloatingPointLiteral() { | ||
| double doubleValue = 0.0; | ||
| if (0.0 == doubleValue) { } // Noncompliant {{Put the variable on the left side of this comparison.}} | ||
| // ^^^ | ||
| if (doubleValue == 0.0) { } // Compliant | ||
| } | ||
|
|
||
| void testNestedParentheses() { | ||
| int count = 0; | ||
| Object obj = new Object(); | ||
| if ((0) == count) { } // Noncompliant {{Put the variable on the left side of this comparison.}} | ||
| if (((null)) == obj) { } // Noncompliant {{Put the variable on the left side of this comparison.}} | ||
| // ^^^^ | ||
| } | ||
|
|
||
| void testLessThanGreaterThan() { | ||
| int count = 0; | ||
| int x = 5; | ||
| if (0 < count) { } // Noncompliant {{Put the variable on the left side of this comparison and invert the operator.}} | ||
| // ^ | ||
| if (5 > x) { } // Noncompliant {{Put the variable on the left side of this comparison and invert the operator.}} | ||
| // ^ | ||
| if (count > 0) { } // Compliant | ||
| if (x < 5) { } // Compliant | ||
| } | ||
|
|
||
| void testLessThanOrEqualGreaterThanOrEqual() { | ||
| int count = 0; | ||
| int x = 5; | ||
| if (0 <= count) { } // Noncompliant {{Put the variable on the left side of this comparison and invert the operator.}} | ||
| // ^ | ||
| if (5 >= x) { } // Noncompliant {{Put the variable on the left side of this comparison and invert the operator.}} | ||
| // ^ | ||
| if (count >= 0) { } // Compliant | ||
| if (x <= 5) { } // Compliant | ||
| } | ||
|
|
||
| void testNonComparisonContexts() { | ||
| int count = 0; | ||
| int a = 1; | ||
| int b = 2; | ||
| count = 0; // Compliant - assignment | ||
| int sum = a + 5; // Compliant - arithmetic | ||
| int product = 5 * b; // Compliant - arithmetic | ||
| } | ||
|
|
||
| void testTernaryOperator() { | ||
| boolean condition = true; | ||
| int result = condition ? 5 : 10; // Compliant | ||
| if (condition) { } // Compliant | ||
| } | ||
|
|
||
| void testArrayAccess() { | ||
| int[] array = {1, 2, 3}; | ||
| if (0 == array[0]) { } // Noncompliant {{Put the variable on the left side of this comparison.}} | ||
| // ^ | ||
| if (array[0] == 0) { } // Compliant | ||
| } | ||
|
|
||
| void testUnaryMinusPlusYoda() { | ||
| int index = 0; | ||
| if (-1 == index) { } // Noncompliant {{Put the variable on the left side of this comparison.}} | ||
| // ^^ | ||
| if (+1 == index) { } // Noncompliant {{Put the variable on the left side of this comparison.}} | ||
| // ^^ | ||
| if (index == -1) { } // Compliant | ||
| if (index == +1) { } // Compliant | ||
| } | ||
|
|
||
| void testBothLiterals() { | ||
| if (0 == 0) { } // Compliant - both sides are literals | ||
| if (5 != 10) { } // Compliant - both sides are literals | ||
| if (true == false) { } // Compliant - both sides are literals | ||
| if (0 == -1) { } // Compliant - both sides are literals | ||
| if (-1 == 0) { } // Compliant - both sides are literals | ||
| if (-1 == -2) { } // Compliant - both sides are literals | ||
| } | ||
|
|
||
| void testBothVariables() { | ||
| int count = 0; | ||
| int otherCount = 0; | ||
| Object obj1 = null; | ||
| Object obj2 = null; | ||
| if (count == otherCount) { } // Compliant - both are variables | ||
| if (obj1 == obj2) { } // Compliant - both are variables | ||
| } | ||
|
|
||
| void testConstantMathCalls() { | ||
| int max = Math.max(5, 10); // Noncompliant {{Replace this call to "max" with the precomputed constant value.}} | ||
| // ^^^^^^^^ | ||
| int min = Math.min(3, 7); // Noncompliant {{Replace this call to "min" with the precomputed constant value.}} | ||
| // ^^^^^^^^ | ||
| double sqrt = Math.sqrt(16.0); // Noncompliant {{Replace this call to "sqrt" with the precomputed constant value.}} | ||
| // ^^^^^^^^^ | ||
| int abs = Math.abs(-5); // Noncompliant {{Replace this call to "abs" with the precomputed constant value.}} | ||
| // ^^^^^^^^ | ||
| long rounded = Math.round(3.14); // Noncompliant {{Replace this call to "round" with the precomputed constant value.}} | ||
| // ^^^^^^^^^^ | ||
| double floor = Math.floor(3.7); // Noncompliant {{Replace this call to "floor" with the precomputed constant value.}} | ||
| // ^^^^^^^^^^ | ||
| double ceil = Math.ceil(3.2); // Noncompliant {{Replace this call to "ceil" with the precomputed constant value.}} | ||
| // ^^^^^^^^^ | ||
| } | ||
|
|
||
| void testConstantMathCallsCompliant() { | ||
| int x = 5; | ||
| int y = 10; | ||
| int max = Math.max(x, 10); // Compliant - x is not a literal | ||
| int min = Math.min(3, y); // Compliant - y is not a literal | ||
| double sqrt = Math.sqrt(x); // Compliant - x is not a literal | ||
| int abs = Math.abs(x); // Compliant - x is not a literal | ||
| int maxVar = Math.max(x, y); // Compliant - neither is a literal | ||
| } | ||
|
|
||
| void testConstantMathCallsWithUnaryMinus() { | ||
| int abs = Math.abs(-10); // Noncompliant {{Replace this call to "abs" with the precomputed constant value.}} | ||
| // ^^^^^^^^ | ||
| int max = Math.max(-5, -3); // Noncompliant {{Replace this call to "max" with the precomputed constant value.}} | ||
| // ^^^^^^^^ | ||
| double sqrt = Math.sqrt(+4.0); // Noncompliant {{Replace this call to "sqrt" with the precomputed constant value.}} | ||
| // ^^^^^^^^^ | ||
| } | ||
|
|
||
| void testTranscendentalMathCallsCompliant() { | ||
| double pow = Math.pow(2.0, 3.0); // Compliant - transcendental functions excluded (result may vary across JVMs) | ||
| double sin = Math.sin(0.5); // Compliant | ||
| double cos = Math.cos(0.5); // Compliant | ||
| double log = Math.log(2.0); // Compliant | ||
| double exp = Math.exp(1.0); // Compliant | ||
| double cbrt = Math.cbrt(8.0); // Compliant - not exactly specified | ||
| double deg = Math.toDegrees(1.0); // Compliant - not exactly specified | ||
| double rad = Math.toRadians(90.0); // Compliant - not exactly specified | ||
| } | ||
|
|
||
| void testNonMathMethodCalls() { | ||
| String result = String.valueOf(5); // Compliant - not a Math method | ||
| int hash = Integer.hashCode(42); // Compliant - not a Math method | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.