equals compares a string or box with a value of another type, which is never equal

tx0/java:equals-unrelated-typedefaultwarningbug

What it finds

name.equals(5), count.equals(0L) with count an Integer: String and the boxes of primitives are final, and their equals is true only for an object of the same class. Called with a value of another of these classes, or a primitive that boxes to one, it is always false. Usually the argument was meant to be converted first, as in count.equals(0) or name.equals(String.valueOf(5)).

Both types must follow from this file: from literals, declarations, casts and operators. Calls among the arguments of an assertion are left alone, since tests check this very contract with them.

From the rule's source, equals_unrelated_type.rs.

Example from the rule's tests
class Checks {  boolean f(String name, Integer count, Long total, Object any, int n, Character c) {    if (name.equals(5)) {tx0 reports: name.equals is always false here: String and Integer objects are never equal      return true;    }    if (count.equals(0L)) {tx0 reports: Integer and Long objects      return true;    }    if (total.equals(n)) {tx0 reports: Long and Integer objects      return true;    }    if (c.equals("c")) {tx0 reports: Character and String objects      return true;    }    assertFalse(name.equals(1));    return name.equals("5") || count.equals(0) || total.equals(0L) || any.equals(5) || name.equals(any)        || c.equals('c');  }   static void assertFalse(boolean value) {}}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/java:equals-unrelated-type" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:equals-unrelated-type] reason="..."

tx0 explain tx0/java:equals-unrelated-type shows the same in the terminal. Found a false positive? Report it.