Skip to content

False positive in ReferenceEquality check? #5865

Closed

Description

@msridhar

To reproduce, clone https://github.com/uber/NullAway, check out the ep-2.50.0 branch, and run ./gradlew :nullaway:compileJava. One of the reported warnings is:

nullaway/src/main/java/com/uber/nullaway/NullAway.java:1179: warning: [ReferenceEquality] Comparison using reference equality instead of value equality
    if (description != Description.NO_MATCH) {

The ReferenceEquality docs say the check should only warn for types that override equals() or inherit equals() from a type other than java.lang.Object. But neither seems to be true for Description:

https://github.com/google/error-prone/blob/4525e7919cbb578e92eb81d86e615424524f2efb/check_api/src/main/java/com/google/errorprone/matchers/Description.java

Is this warning intended?

Activity

  1. cushon commented on Jun 12, 2026

    @cushon
    Collaborator

    The scope of ReferenceEquality was deliberately expanded in a116aaf, but it's helpful to get feedback on real world examples where the findings weren't valuable.

    For Description.NO_MATCH it's now matching because Description is non-final, maybe it should be. There aren't any subclasses of it internally, I'm not sure if any users of Error Prone would be affected by that.

    There was also some more discussion about adjusting the heuristics in the check for sentinel comparisons, although using reference equality for sentinel comparisons isn't necessary correct.

  2. msridhar commented on Jun 12, 2026

    @msridhar
    ContributorAuthor

    Ah, ok, interesting. Maybe the docs should be updated about the additional case of reference equality on non-final classes? I think with that, the expansion of the check is probably fine. For the case above, I can switch to using equals() to be safe.

  3. self-assigned this
    on Jun 12, 2026
  4. cpovirk commented on Jun 12, 2026

    @cpovirk
    Member

    Yes, thanks for the report. I am planning to:

    • Fix the docs to describe the new behavior.
    • Propose adding final to Error Prone classes with only private constructors (including Description) wherever I can get away with it.

    Incidentally, on that latter front: I got to wondering whether we might be able to recognize automatically that a class with only private constructors doesn't override equals (as long as it's not extended by any classes in the file). In Turbine terms, that would mean "a class that appears to have no constructors," since the private ones get stripped :) But that leads me to another point, which is that Turbine nowadays strips private classes where possible. As a result, we wouldn't be able to see whether there are any private classes that override equals, so my plan doesn't work.

    OK, maybe we could fall back to recognizing only cases in which we have source code (or maybe even cases in which we can tell that private members are available in the class file). But I worry about that, since it would result in different behavior if a compilation is run to rebuild only some files or if compilation is otherwise sharded. The safe move would be to apply that heuristic only to classes declared in the current file. I might try that out....

    Also, as background: Ironically, our goal with the recent changes is to make == a more viable option: If we can say that Error Prone produces a warning for any == usage that isn't provably equivalent to equals, then we can tell people to use == without worry. Unfortunately, that leads to warnings like this one.

  5. auloin commented on Jun 17, 2026

    @auloin

    Hi @cushon, @cpovirk, I've encountered the same issue. What I find strange is that it also fails for a new Object() constant. Do you have an explanation for that behavior?

    (see https://github.com/jobrunr/jobrunr/blob/master/core/src/main/java/org/jobrunr/jobs/details/instructions/AbstractJVMInstruction.java#L19 for practical use case)

  6. cpovirk commented on Jun 17, 2026

    @cpovirk
    Member

    Thanks for the additional report.

    The reason that this happens is that we currently look only at the declared type of the field and, since that type is Object, we worry that its value might be of a type that implements equals, in which case the comparison should perhaps be using equals. We are considering loosening the check for cases in which the field is directly assigned a value like new Object() that we know is not of proper subtype of Object or loosening the check for sentinel objects more generally.

  7. cpovirk commented on Jun 23, 2026

    @cpovirk
    Member

    We are considering loosening the check for cases in which the field is directly assigned a value like new Object()

    This happened in #5895 (though of course it's not in a release yet).

    I still have a docs change in flight, and that will be at least all that I have in mind for the moment.

  8. cpovirk commented on Jun 24, 2026

    @cpovirk
    Member

    I am now going to look at #5900, too :) But then I expect to set this down.

    Please do report other undesired errors, especially if you can propose a mechanically identifiable subset of classes for which an exception might be especially suitable. (As in the original report here, a good thing to try is to see if you can make the involved class final.) I suspect that we won't make a ton more exceptions, but we might be able to find some that make sense, and maaaaybe we could introduce a flag for users who find themselves hitting enough trouble with the recent changes but who would really like to keep ReferenceEquality on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions