rspec/rules/S2161/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

51 lines
1.0 KiB
Plaintext

== Why is this an issue?
Non ``++final++`` classes shouldn't use a hardcoded class name in the ``++equals++`` method. Doing so breaks the method for subclasses. Instead, make the comparison dynamic.
=== Noncompliant code example
[source,java]
----
public class Fruit {
private Season ripe;
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (Fruit.class == obj.class) { // Noncompliant
return false;
}
// ...
----
=== Compliant solution
[source,java]
----
public class Fruit {
private Season ripe;
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (this.class == obj.class) { // will work for subclasses too
return false;
}
// ...
----
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
=== on 15 Oct 2014, 21:53:16 Freddy Mallet wrote:
@Ann, I would merge this rule with RSPEC-2162 as they both have exactly the same goal.
endif::env-github,rspecator-view[]