Inspectopedia Help

'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.

Example:

class Foo { boolean foo(Object o) { return o.equals(o); // warning } boolean bar(String[] ss) { return Arrays.equals(ss, ss); // warning } }

Locating this inspection

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
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Java | Probable bugs

Use the option to report test assertions report only on non-extendable library classes (like String) and primitive types. This option can be useful, when testing equals() methods.

Inspection options

Here you can find the description of settings available for the 'equals()' called on itself inspection, and the reference of their default values.

Warn about test assertions only on final library class types or primitives

Not selected

Availability

By default bundled with

IntelliJ IDEA 2024.1, Qodana for JVM 2024.1,

Can be installed with plugin

Java, 241.18072

Last modified: 18 June 2024