Skip to content

Avoid calling checkNotNull on nullable values except during actual precondition checks. - #7090

Merged
1 commit merged into
masterfrom
test_612937549
Mar 9, 2024
Merged

1 commit merged into
masterfrom
test_612937549

Conversation

@copybara-service

Copy link
Copy Markdown
Contributor

Avoid calling checkNotNull on nullable values except during actual precondition checks.

It's not that we're not going to make such calls illegal, I promise :) I mean, we certainly aren't going to in general, but I am tempted for com.google.common, as discussed on cl/372346107 :) (It would have caught the problem of cl/612591080!) I'm testing what would happen if we did do it for com.google.common in case it shakes out any more bugs.

It does reveal that I didn't complete the cleanup of cl/612591080. And it reveals a few places where we'd normally use requireNonNull, since the checks aren't "preconditions" in the sense of "the caller did something wrong" (from cl/15376243 and cl/526930990). I've made those changes. (I would have made some more changes if I had tried to address more of com.google.common. But I stuck to the "main" packages, and I didn't even fix enough errors to see full results.)

Honestly, the more interesting thing that this exercise revealed was that there are more cases in which I'm especially sympathetic to calling checkNotNull on nullable values:

  • DummyProxy is making an InvocationHandler perform automatic precondition tests based on annotations on the interface it's implementing.
  • EqualsTester and Truth have permissive signatures because they're test utilities, as documented in cl/578260904 and discussed during the Truth CLs.

And the yet more interesting thing that it revealed is that we may want to use @NonNull here in the future, similar to what we've discussed in #6824.

RELNOTES=n/a

…precondition checks.

It's not that we're not going to make such calls illegal, I promise :) I mean, we certainly aren't going to _in general_, but I am tempted for `com.google.common`, as discussed on cl/372346107 :) (It would have caught the problem of cl/612591080!) I'm testing what would happen if we did do it for `com.google.common` in case it shakes out any more bugs.

It does reveal that I didn't complete the cleanup of cl/612591080. And it reveals a few places where we'd normally use `requireNonNull`, since the checks aren't "preconditions" in the sense of "the caller did something wrong" (from cl/15376243 and cl/526930990). I've made those changes. (I would have made some more changes if I had tried to address more of `com.google.common`. But I stuck to the "main" packages, and I didn't even fix enough errors to see full results.)

Honestly, the more interesting thing that this exercise revealed was that there are more cases in which I'm especially sympathetic to calling `checkNotNull` on nullable values:
- `DummyProxy` is making an `InvocationHandler` perform automatic precondition tests based on annotations on the interface it's implementing.
- `EqualsTester` and Truth have permissive signatures because they're test utilities, as documented in cl/578260904 and discussed during the Truth CLs.

And the yet more interesting thing that it revealed is that we may want to use `@NonNull` here in the future, similar to what we've discussed in #6824.

RELNOTES=n/a
PiperOrigin-RevId: 614074533
@copybara-service copybara-service Bot closed this Mar 9, 2024
@copybara-service copybara-service Bot closed this pull request by merging all changes into master in 52a62b2 Mar 9, 2024
@copybara-service
copybara-service Bot deleted the test_612937549 branch March 9, 2024 00:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant