30 lines
632 B
Plaintext
30 lines
632 B
Plaintext
== Why is this an issue?
|
|
|
|
As the `equals` method's parameter is typed as generic `Any?`, any type of object may be passed to it. The method implementation should not assume it will only be used to test objects of its own class type. It must instead check the parameter's type.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,kotlin]
|
|
----
|
|
override fun equals(other: Any?): Boolean { // Noncompliant
|
|
val mc = other as MyClass
|
|
// ...
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,kotlin]
|
|
----
|
|
override fun equals(other: Any?): Boolean {
|
|
if (other?.javaClass != this.javaClass)
|
|
return false
|
|
|
|
val mc = other as MyClass
|
|
// ...
|
|
}
|
|
----
|
|
|