Update rule S2116: "hashCode" and "toString" should not be called on array instances

This commit is contained in:
Johann Beleites 2021-09-15 08:36:49 +02:00
parent 7a609641dc
commit 02fb188965
6 changed files with 73 additions and 30 deletions

View File

@ -0,0 +1 @@
While `hashCode` and `toString` are available on arrays, they are largely useless. `hashCode` returns the array's "identity hash code", and `toString` returns nearly the same value. Neither method's output actually reflects the array's contents.

View File

@ -1,27 +1,2 @@
{
"title": "\"hashCode\" and \"toString\" should not be called on array instances",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
],
"extra": {
"coveredLanguages": [
"Java"
],
"replacementRules": [
]
},
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-2116",
"sqKey": "S2116",
"scope": "All",
"defaultQualityProfiles": [
"Sonar way"
]
}

View File

@ -1,11 +1,12 @@
While ``++hashCode++`` and ``++toString++`` are available on arrays, they are largely useless. ``++hashCode++`` returns the array's "identity hash code", and ``++toString++`` returns nearly the same value. Neither method's output actually reflects the array's contents. Instead, you should pass the array to the relevant static ``++Arrays++`` method.
include::../description.adoc[]
Instead, you should pass the array to the relevant static ``++Arrays++`` method.
== Noncompliant Code Example
----
public static void main( String[] args )
{
public static void main( String[] args ) {
String argStr = args.toString(); // Noncompliant
int argHash = args.hashCode(); // Noncompliant
----
@ -14,8 +15,7 @@ public static void main( String[] args )
== Compliant Solution
----
public static void main( String[] args )
{
public static void main( String[] args ) {
String argStr = Arrays.toString(args);
int argHash = Arrays.hashCode(args);
----

View File

@ -0,0 +1,2 @@
{
}

View File

@ -0,0 +1,39 @@
include::../description.adoc[]
Instead, you should use the corresponding `content` and `contentDeep` methods:
- Use `contentEquals` or `contentDeepEquals` instead of `equals` or `==`
- Use `contentHashCode` or `contentDeepHashCode` instead of `hashCode`
- Use `contentToString` or `contentDeepToString` instead of `toString`
== Noncompliant Code Example
----
fun main(vararg args: String) {
val argStr = args.toString() // Noncompliant
val argHash = args.hashCode() // Noncompliant
// ...
}
----
== Compliant Solution
----
fun main(vararg args: String) {
val argStr = args.contentToString()
val argHash = args.contentHashCode()
// ...
}
----
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]

View File

@ -1,2 +1,28 @@
{
"title": "\"hashCode\" and \"toString\" should not be called on array instances",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
],
"extra": {
"coveredLanguages": [
"Java",
"Kotlin"
],
"replacementRules": [
]
},
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-2116",
"sqKey": "S2116",
"scope": "All",
"defaultQualityProfiles": [
"Sonar way"
]
}