Update rule S2116: "hashCode" and "toString" should not be called on array instances
This commit is contained in:
parent
7a609641dc
commit
02fb188965
1
rules/S2116/description.adoc
Normal file
1
rules/S2116/description.adoc
Normal 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.
|
@ -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"
|
||||
]
|
||||
}
|
||||
|
@ -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);
|
||||
----
|
||||
|
2
rules/S2116/kotlin/metadata.json
Normal file
2
rules/S2116/kotlin/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
39
rules/S2116/kotlin/rule.adoc
Normal file
39
rules/S2116/kotlin/rule.adoc
Normal 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[]
|
@ -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"
|
||||
]
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user