You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
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.
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.
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?
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.
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.
Activity
cushon commentedon Jun 12, 2026
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_MATCHit's now matching becauseDescriptionis 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.
msridhar commentedon Jun 12, 2026
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.cpovirk commentedon Jun 12, 2026
Yes, thanks for the report. I am planning to:
finalto Error Prone classes with onlyprivateconstructors (includingDescription) 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
privateconstructors doesn't overrideequals(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 overrideequals, 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 toequals, then we can tell people to use==without worry. Unfortunately, that leads to warnings like this one.Add `final` to classes with `private` constructors.
Add `final` to classes with `private` constructors.
Add `final` to classes with `private` constructors.
Add `final` to classes with `private` constructors.
Change `VisitorState` to be `final`.
auloin commentedon Jun 17, 2026
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)
cpovirk commentedon Jun 17, 2026
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 implementsequals, in which case the comparison should perhaps be usingequals. We are considering loosening the check for cases in which the field is directly assigned a value likenew Object()that we know is not of proper subtype ofObjector loosening the check for sentinel objects more generally.Allow `==` on private (and "privately constructible") classes that we…
cpovirk commentedon Jun 23, 2026
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.
Update ReferenceEquality docs for the changes of a116aaf.
cpovirk commentedon Jun 24, 2026
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.