'equals()' called on itself
Reports calls to equals()
, compareTo()
or similar, that compare an object for equality with itself. The method contracts of these methods specify that such calls will always return true
for equals()
or 0
for compareTo()
. The inspection also checks calls to Objects.equals()
, Objects.deepEquals()
, Arrays.equals()
, Comparator.compare()
, assertEquals()
methods of test frameworks (JUnit, TestNG, AssertJ), Integer.compare()
, Integer.compareUnsigned()
and similar methods.
Note that in rare cases, the inspection may report equals()
calls that return false, because while the expressions on the both sides are the same, they produce separate objects, and comparison is performed on references, rather than on content. The simplest example is new Object().equals(new Object())
. In any case, such calls are suspicious, and likely something else was intended.
Example:
class Foo {
boolean foo(Object o) {
return o.equals(o); // warning
}
boolean bar(String[] ss) {
return Arrays.equals(ss, ss); // warning
}
}
- By ID
Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.
EqualsWithItself
Here you can find the description of settings available for the 'equals()' called on itself inspection, and the reference of their default values.
Inspection Details | |
---|---|
By default bundled with: | |
Can be installed with plugin: | Java, 243.23126 |
Thanks for your feedback!